From 865a792dda051f5bb8603108fb08ff44290bdf38 Mon Sep 17 00:00:00 2001 From: Anton Bershanskiy Date: Fri, 22 Mar 2019 02:10:03 -0500 Subject: [PATCH] rewrite manipulator unit tests --- js/src/dom/manipulator.spec.js | 143 +++++++++++++++++++++++++++++++ js/tests/unit/dom/manipulator.js | 138 ----------------------------- 2 files changed, 143 insertions(+), 138 deletions(-) create mode 100644 js/src/dom/manipulator.spec.js delete mode 100644 js/tests/unit/dom/manipulator.js diff --git a/js/src/dom/manipulator.spec.js b/js/src/dom/manipulator.spec.js new file mode 100644 index 000000000000..d4b0292ba962 --- /dev/null +++ b/js/src/dom/manipulator.spec.js @@ -0,0 +1,143 @@ +import Manipulator from './manipulator' + +/** Test helpers */ +import { getFixture, clearFixture } from '../../tests/helpers/fixture' + +describe('Manipulator', () => { + let fixtureEl + + beforeAll(() => { + fixtureEl = getFixture() + }) + + afterEach(() => { + clearFixture() + }) + + describe('setDataAttribute', () => { + it('should set data attribute', () => { + fixtureEl.innerHTML = '
' + + const div = fixtureEl.querySelector('div') + + Manipulator.setDataAttribute(div, 'key', 'value') + expect(div.getAttribute('data-key')).toEqual('value') + }) + + it('should set data attribute in lower case', () => { + fixtureEl.innerHTML = '
' + + const div = fixtureEl.querySelector('div') + + Manipulator.setDataAttribute(div, 'tEsT', 'value') + expect(div.getAttribute('data-test')).toEqual('value') + }) + }) + + describe('removeDataAttribute', () => { + it('should remove data attribute', () => { + fixtureEl.innerHTML = '
' + + const div = fixtureEl.querySelector('div') + + Manipulator.removeDataAttribute(div, 'key') + expect(div.getAttribute('data-key')).toBeNull() + }) + + it('should remove data attribute in lower case', () => { + fixtureEl.innerHTML = '
' + + const div = fixtureEl.querySelector('div') + + Manipulator.removeDataAttribute(div, 'tEStKeY') + expect(div.getAttribute('data-testkey')).toBeNull() + }) + }) + + describe('getDataAttributes', () => { + it('should return empty object for null', () => { + expect(Manipulator.getDataAttributes(null), {}) + }) + + it('should get all data attributes', () => { + fixtureEl.innerHTML = '
' + + const div = fixtureEl.querySelector('div') + + expect(Manipulator.getDataAttributes(div)).toEqual({ + test: 'js', + test2: 'js2' + }) + }) + }) + + describe('getDataAttribute', () => { + it('should get data attribute', () => { + fixtureEl.innerHTML = '
' + + const div = fixtureEl.querySelector('div') + + expect(Manipulator.getDataAttribute(div, 'test')).toBeNull() + }) + + it('should get data attribute in lower case', () => { + fixtureEl.innerHTML = '
' + + const div = fixtureEl.querySelector('div') + + expect(Manipulator.getDataAttribute(div, 'tEsT')).toEqual('value') + }) + }) + + describe('offset', () => { + it('should return object with two properties top and left, both numbers', () => { + fixtureEl.innerHTML = '
' + + const div = fixtureEl.querySelector('div') + const offset = Manipulator.offset(div) + + expect(offset).toBeDefined() + expect(offset.top).toEqual(jasmine.any(Number)) + expect(offset.left).toEqual(jasmine.any(Number)) + }) + }) + + describe('position', () => { + it('should return object with two properties top and left, both numbers', () => { + fixtureEl.innerHTML = '
' + + const div = fixtureEl.querySelector('div') + const position = Manipulator.position(div) + + expect(position).toBeDefined() + expect(position.top).toEqual(jasmine.any(Number)) + expect(position.left).toEqual(jasmine.any(Number)) + }) + }) + + describe('toggleClass', () => { + it('should not error out if element is null or undefined', () => { + Manipulator.toggleClass(null, 'test') + Manipulator.toggleClass(undefined, 'test') + expect().nothing() + }) + + it('should add class if it is missing', () => { + fixtureEl.innerHTML = '
' + + const div = fixtureEl.querySelector('div') + + Manipulator.toggleClass(div, 'test') + expect(div.classList.contains('test')).toEqual(true) + }) + + it('should remove class if it is set', () => { + fixtureEl.innerHTML = '
' + + const div = fixtureEl.querySelector('div') + + Manipulator.toggleClass(div, 'test') + expect(div.classList.contains('test')).toEqual(false) + }) + }) +}) diff --git a/js/tests/unit/dom/manipulator.js b/js/tests/unit/dom/manipulator.js deleted file mode 100644 index 4b20529d4176..000000000000 --- a/js/tests/unit/dom/manipulator.js +++ /dev/null @@ -1,138 +0,0 @@ -$(function () { - 'use strict' - - QUnit.module('manipulator') - - QUnit.test('should be defined', function (assert) { - assert.expect(1) - assert.ok(Manipulator, 'Manipulator is defined') - }) - - QUnit.test('should set data attribute', function (assert) { - assert.expect(1) - - var $div = $('
').appendTo('#qunit-fixture') - - Manipulator.setDataAttribute($div[0], 'test', 'test') - - assert.strictEqual($div[0].getAttribute('data-test'), 'test') - }) - - QUnit.test('should set data attribute in lower case', function (assert) { - assert.expect(1) - - var $div = $('
').appendTo('#qunit-fixture') - - Manipulator.setDataAttribute($div[0], 'tEsT', 'test') - - assert.strictEqual($div[0].getAttribute('data-test'), 'test') - }) - - QUnit.test('should get data attribute', function (assert) { - assert.expect(2) - - var $div = $('
').appendTo('#qunit-fixture') - - assert.strictEqual(Manipulator.getDataAttribute($div[0], 'test'), null) - - var $div2 = $('
').appendTo('#qunit-fixture') - - assert.strictEqual(Manipulator.getDataAttribute($div2[0], 'tEst2'), 'js') - }) - - QUnit.test('should get data attributes', function (assert) { - assert.expect(4) - - var $div = $('
').appendTo('#qunit-fixture') - var $div2 = $('
').appendTo('#qunit-fixture') - var $div3 = $('
').appendTo('#qunit-fixture') - - assert.propEqual(Manipulator.getDataAttributes($div[0]), { - test: 'js', - test2: 'js2' - }) - - assert.propEqual(Manipulator.getDataAttributes(null), {}) - - var stub = sinon - .stub(Object, 'getOwnPropertyDescriptor') - .callsFake(function () { - return false - }) - - assert.propEqual(Manipulator.getDataAttributes($div2[0]), { - test3: 'js', - test4: 'js2' - }) - - assert.propEqual(Manipulator.getDataAttributes($div3[0]), {}) - - stub.restore() - }) - - QUnit.test('should remove data attribute', function (assert) { - assert.expect(2) - - var $div = $('
').appendTo('#qunit-fixture') - - Manipulator.setDataAttribute($div[0], 'test', 'test') - - assert.strictEqual($div[0].getAttribute('data-test'), 'test') - - Manipulator.removeDataAttribute($div[0], 'test') - - assert.strictEqual($div[0].getAttribute('data-test'), null) - }) - - QUnit.test('should remove data attribute in lower case', function (assert) { - assert.expect(2) - - var $div = $('
').appendTo('#qunit-fixture') - - Manipulator.setDataAttribute($div[0], 'test', 'test') - - assert.strictEqual($div[0].getAttribute('data-test'), 'test') - - Manipulator.removeDataAttribute($div[0], 'tESt') - - assert.strictEqual($div[0].getAttribute('data-test'), null) - }) - - QUnit.test('should return element offsets', function (assert) { - assert.expect(2) - - var $div = $('
').appendTo('#qunit-fixture') - - var offset = Manipulator.offset($div[0]) - - assert.ok(typeof offset.top === 'number') - assert.ok(typeof offset.left === 'number') - }) - - QUnit.test('should return element position', function (assert) { - assert.expect(2) - - var $div = $('
').appendTo('#qunit-fixture') - - var offset = Manipulator.position($div[0]) - - assert.ok(typeof offset.top === 'number') - assert.ok(typeof offset.left === 'number') - }) - - QUnit.test('should toggle class', function (assert) { - assert.expect(2) - - var $div = $('
').appendTo('#qunit-fixture') - - Manipulator.toggleClass($div[0], 'test') - - assert.ok(!$div.hasClass('test')) - - Manipulator.toggleClass($div[0], 'test') - - assert.ok($div.hasClass('test')) - - Manipulator.toggleClass(null) - }) -})