2016-08-04 17:49:32 +02:00
|
|
|
/* eslint-env qunit */
|
2017-01-19 22:30:47 +02:00
|
|
|
import window from 'global/window';
|
2015-03-26 06:43:41 +02:00
|
|
|
import Component from '../../src/js/component.js';
|
2015-05-04 01:12:38 +02:00
|
|
|
import * as Dom from '../../src/js/utils/dom.js';
|
2019-08-01 20:26:59 +02:00
|
|
|
import DomData from '../../src/js/utils/dom-data';
|
2015-05-04 01:12:38 +02:00
|
|
|
import * as Events from '../../src/js/utils/events.js';
|
2017-01-27 05:04:34 +02:00
|
|
|
import * as Obj from '../../src/js/utils/obj';
|
2015-05-04 01:12:38 +02:00
|
|
|
import * as browser from '../../src/js/utils/browser.js';
|
2015-03-26 06:43:41 +02:00
|
|
|
import document from 'global/document';
|
2016-08-04 17:49:32 +02:00
|
|
|
import sinon from 'sinon';
|
2015-05-17 00:59:46 +02:00
|
|
|
import TestHelpers from './test-helpers.js';
|
2015-03-26 06:43:41 +02:00
|
|
|
|
2015-11-06 23:42:19 +02:00
|
|
|
class TestComponent1 extends Component {}
|
|
|
|
class TestComponent2 extends Component {}
|
|
|
|
class TestComponent3 extends Component {}
|
|
|
|
class TestComponent4 extends Component {}
|
2019-04-29 18:01:37 +02:00
|
|
|
|
2015-11-06 23:42:19 +02:00
|
|
|
TestComponent1.prototype.options_ = {
|
|
|
|
children: [
|
|
|
|
'testComponent2',
|
|
|
|
'testComponent3'
|
|
|
|
]
|
|
|
|
};
|
|
|
|
|
2016-08-04 17:49:32 +02:00
|
|
|
QUnit.module('Component', {
|
2019-04-29 18:01:37 +02:00
|
|
|
before() {
|
|
|
|
Component.registerComponent('TestComponent1', TestComponent1);
|
|
|
|
Component.registerComponent('TestComponent2', TestComponent2);
|
|
|
|
Component.registerComponent('TestComponent3', TestComponent3);
|
|
|
|
Component.registerComponent('TestComponent4', TestComponent4);
|
|
|
|
},
|
2016-08-12 19:51:31 +02:00
|
|
|
beforeEach() {
|
2014-12-03 21:31:39 +02:00
|
|
|
this.clock = sinon.useFakeTimers();
|
2020-11-11 01:09:37 +02:00
|
|
|
this.player = TestHelpers.makePlayer();
|
2014-12-03 21:31:39 +02:00
|
|
|
},
|
2016-08-12 19:51:31 +02:00
|
|
|
afterEach() {
|
2020-11-11 01:09:37 +02:00
|
|
|
this.player.dispose();
|
2014-12-03 21:31:39 +02:00
|
|
|
this.clock.restore();
|
2019-04-29 18:01:37 +02:00
|
|
|
},
|
|
|
|
after() {
|
|
|
|
delete Component.components_.TestComponent1;
|
|
|
|
delete Component.components_.TestComponent2;
|
|
|
|
delete Component.components_.TestComponent3;
|
|
|
|
delete Component.components_.TestComponent4;
|
2014-12-03 21:31:39 +02:00
|
|
|
}
|
|
|
|
});
|
2012-12-11 03:40:12 +03:00
|
|
|
|
2017-01-18 07:46:43 +02:00
|
|
|
QUnit.test('registerComponent() throws with bad arguments', function(assert) {
|
|
|
|
assert.throws(
|
|
|
|
function() {
|
|
|
|
Component.registerComponent(null);
|
|
|
|
},
|
|
|
|
new Error('Illegal component name, "null"; must be a non-empty string.'),
|
|
|
|
'component names must be non-empty strings'
|
|
|
|
);
|
|
|
|
|
|
|
|
assert.throws(
|
|
|
|
function() {
|
|
|
|
Component.registerComponent('');
|
|
|
|
},
|
|
|
|
new Error('Illegal component name, ""; must be a non-empty string.'),
|
|
|
|
'component names must be non-empty strings'
|
|
|
|
);
|
|
|
|
|
|
|
|
assert.throws(
|
|
|
|
function() {
|
|
|
|
Component.registerComponent('TestComponent5', function() {});
|
|
|
|
},
|
|
|
|
new Error('Illegal component, "TestComponent5"; must be a Component subclass.'),
|
|
|
|
'components must be subclasses of Component'
|
|
|
|
);
|
|
|
|
|
|
|
|
assert.throws(
|
|
|
|
function() {
|
|
|
|
const Tech = Component.getComponent('Tech');
|
|
|
|
|
|
|
|
class DummyTech extends Tech {}
|
|
|
|
|
|
|
|
Component.registerComponent('TestComponent5', DummyTech);
|
|
|
|
},
|
|
|
|
new Error('Illegal component, "TestComponent5"; techs must be registered using Tech.registerTech().'),
|
|
|
|
'components must be subclasses of Component'
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
QUnit.test('should create an element', function(assert) {
|
2020-11-11 01:09:37 +02:00
|
|
|
const comp = new Component(this.player, {});
|
2012-12-11 03:40:12 +03:00
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.ok(comp.el().nodeName);
|
2018-01-30 20:26:21 +02:00
|
|
|
|
|
|
|
comp.dispose();
|
2012-12-11 03:40:12 +03:00
|
|
|
});
|
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
QUnit.test('should add a child component', function(assert) {
|
2020-11-11 01:09:37 +02:00
|
|
|
const comp = new Component(this.player);
|
2012-12-31 08:45:50 +03:00
|
|
|
|
2016-08-04 17:49:32 +02:00
|
|
|
const child = comp.addChild('component');
|
2012-12-31 08:45:50 +03:00
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.ok(comp.children().length === 1);
|
|
|
|
assert.ok(comp.children()[0] === child);
|
|
|
|
assert.ok(comp.el().childNodes[0] === child.el());
|
|
|
|
assert.ok(comp.getChild('component') === child);
|
|
|
|
assert.ok(comp.getChildById(child.id()) === child);
|
2018-01-30 20:26:21 +02:00
|
|
|
|
|
|
|
comp.dispose();
|
2012-12-31 08:45:50 +03:00
|
|
|
});
|
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
QUnit.test('should add a child component to an index', function(assert) {
|
2020-11-11 01:09:37 +02:00
|
|
|
const comp = new Component(this.player);
|
2016-08-04 17:49:32 +02:00
|
|
|
|
|
|
|
const child = comp.addChild('component');
|
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.ok(comp.children().length === 1);
|
|
|
|
assert.ok(comp.children()[0] === child);
|
2016-08-04 17:49:32 +02:00
|
|
|
|
|
|
|
const child0 = comp.addChild('component', {}, 0);
|
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.ok(comp.children().length === 2);
|
|
|
|
assert.ok(comp.children()[0] === child0);
|
|
|
|
assert.ok(comp.children()[1] === child);
|
2016-02-04 20:42:49 +02:00
|
|
|
|
2016-08-04 17:49:32 +02:00
|
|
|
const child1 = comp.addChild('component', {}, '2');
|
2016-02-04 20:42:49 +02:00
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.ok(comp.children().length === 3);
|
|
|
|
assert.ok(comp.children()[2] === child1);
|
2016-02-04 20:42:49 +02:00
|
|
|
|
2016-08-04 17:49:32 +02:00
|
|
|
const child2 = comp.addChild('component', {}, undefined);
|
2016-02-04 20:42:49 +02:00
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.ok(comp.children().length === 4);
|
|
|
|
assert.ok(comp.children()[3] === child2);
|
2016-02-04 20:42:49 +02:00
|
|
|
|
2016-08-04 17:49:32 +02:00
|
|
|
const child3 = comp.addChild('component', {}, -1);
|
2016-02-04 20:42:49 +02:00
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.ok(comp.children().length === 5);
|
|
|
|
assert.ok(comp.children()[3] === child3);
|
|
|
|
assert.ok(comp.children()[4] === child2);
|
2018-01-30 20:26:21 +02:00
|
|
|
|
|
|
|
comp.dispose();
|
2016-02-04 20:42:49 +02:00
|
|
|
});
|
|
|
|
|
2019-11-14 22:22:42 +02:00
|
|
|
QUnit.test('should insert element relative to the element of the component to insert before', function(assert) {
|
|
|
|
|
|
|
|
// for legibility of the test itself:
|
|
|
|
/* eslint-disable no-unused-vars */
|
|
|
|
|
2020-11-11 01:09:37 +02:00
|
|
|
const comp = new Component(this.player);
|
2019-11-14 22:22:42 +02:00
|
|
|
|
|
|
|
const child0 = comp.addChild('component', {el: Dom.createEl('div', {}, {class: 'c0'})});
|
|
|
|
const child1 = comp.addChild('component', {createEl: false});
|
|
|
|
const child2 = comp.addChild('component', {el: Dom.createEl('div', {}, {class: 'c2'})});
|
|
|
|
const child3 = comp.addChild('component', {el: Dom.createEl('div', {}, {class: 'c3'})});
|
|
|
|
const child4 = comp.addChild('component', {el: Dom.createEl('div', {}, {class: 'c4'})}, comp.children_.indexOf(child2));
|
|
|
|
|
|
|
|
assert.ok(child2.el_.previousSibling === child4.el_, 'addChild should insert el before its next sibling\'s element');
|
|
|
|
|
|
|
|
/* eslint-enable no-unused-vars */
|
|
|
|
});
|
|
|
|
|
2020-05-26 23:00:23 +02:00
|
|
|
QUnit.test('should allow for children that are elements', function(assert) {
|
|
|
|
|
|
|
|
// for legibility of the test itself:
|
|
|
|
/* eslint-disable no-unused-vars */
|
|
|
|
|
2020-11-11 01:09:37 +02:00
|
|
|
const comp = new Component(this.player);
|
2020-05-26 23:00:23 +02:00
|
|
|
const testEl = Dom.createEl('div');
|
|
|
|
|
|
|
|
// Add element as video el gets added to player
|
|
|
|
comp.el().appendChild(testEl);
|
|
|
|
comp.children_.unshift(testEl);
|
|
|
|
|
|
|
|
const child1 = comp.addChild('component', {el: Dom.createEl('div', {}, {class: 'c1'})});
|
|
|
|
const child2 = comp.addChild('component', {el: Dom.createEl('div', {}, {class: 'c4'})}, 0);
|
|
|
|
|
|
|
|
assert.ok(child2.el_.nextSibling === testEl, 'addChild should insert el before a sibling that is an element');
|
|
|
|
|
|
|
|
/* eslint-enable no-unused-vars */
|
|
|
|
});
|
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
QUnit.test('addChild should throw if the child does not exist', function(assert) {
|
2020-11-11 01:09:37 +02:00
|
|
|
const comp = new Component(this.player);
|
2015-11-06 23:42:19 +02:00
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.throws(function() {
|
2016-08-04 17:49:32 +02:00
|
|
|
comp.addChild('non-existent-child');
|
2015-11-06 23:42:19 +02:00
|
|
|
}, new Error('Component Non-existent-child does not exist'), 'addChild threw');
|
2016-02-04 20:42:49 +02:00
|
|
|
|
2018-01-30 20:26:21 +02:00
|
|
|
comp.dispose();
|
2015-11-06 23:42:19 +02:00
|
|
|
});
|
|
|
|
|
2017-02-22 21:00:15 +02:00
|
|
|
QUnit.test('addChild with instance should allow getting child correctly', function(assert) {
|
2020-11-11 01:09:37 +02:00
|
|
|
const comp = new Component(this.player);
|
|
|
|
const comp2 = new Component(this.player);
|
2017-02-22 21:00:15 +02:00
|
|
|
|
|
|
|
comp2.name = function() {
|
|
|
|
return 'foo';
|
|
|
|
};
|
|
|
|
|
|
|
|
comp.addChild(comp2);
|
|
|
|
assert.ok(comp.getChild('foo'), 'we can get child with camelCase');
|
|
|
|
assert.ok(comp.getChild('Foo'), 'we can get child with TitleCase');
|
2018-01-30 20:26:21 +02:00
|
|
|
|
|
|
|
comp.dispose();
|
2017-02-22 21:00:15 +02:00
|
|
|
});
|
|
|
|
|
2016-11-03 21:40:35 +02:00
|
|
|
QUnit.test('should add a child component with title case name', function(assert) {
|
2020-11-11 01:09:37 +02:00
|
|
|
const comp = new Component(this.player);
|
2016-11-03 21:40:35 +02:00
|
|
|
|
|
|
|
const child = comp.addChild('Component');
|
|
|
|
|
|
|
|
assert.ok(comp.children().length === 1);
|
|
|
|
assert.ok(comp.children()[0] === child);
|
|
|
|
assert.ok(comp.el().childNodes[0] === child.el());
|
|
|
|
assert.ok(comp.getChild('Component') === child);
|
|
|
|
assert.ok(comp.getChildById(child.id()) === child);
|
2018-01-30 20:26:21 +02:00
|
|
|
|
|
|
|
comp.dispose();
|
2016-11-03 21:40:35 +02:00
|
|
|
});
|
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
QUnit.test('should init child components from options', function(assert) {
|
2020-11-11 01:09:37 +02:00
|
|
|
const comp = new Component(this.player, {
|
2013-01-11 00:06:12 +03:00
|
|
|
children: {
|
2016-08-04 17:49:32 +02:00
|
|
|
component: {}
|
2013-01-11 00:06:12 +03:00
|
|
|
}
|
|
|
|
});
|
2012-12-31 08:45:50 +03:00
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.ok(comp.children().length === 1);
|
|
|
|
assert.ok(comp.el().childNodes.length === 1);
|
2018-01-30 20:26:21 +02:00
|
|
|
|
|
|
|
comp.dispose();
|
2013-01-11 00:06:12 +03:00
|
|
|
});
|
2012-12-31 08:45:50 +03:00
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
QUnit.test('should init child components from simple children array', function(assert) {
|
2020-11-11 01:09:37 +02:00
|
|
|
const comp = new Component(this.player, {
|
2014-03-18 22:49:59 +03:00
|
|
|
children: [
|
|
|
|
'component',
|
|
|
|
'component',
|
|
|
|
'component'
|
|
|
|
]
|
|
|
|
});
|
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.ok(comp.children().length === 3);
|
|
|
|
assert.ok(comp.el().childNodes.length === 3);
|
2018-01-30 20:26:21 +02:00
|
|
|
|
|
|
|
comp.dispose();
|
2014-03-18 22:49:59 +03:00
|
|
|
});
|
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
QUnit.test('should init child components from children array of objects', function(assert) {
|
2020-11-11 01:09:37 +02:00
|
|
|
const comp = new Component(this.player, {
|
2014-03-18 22:49:59 +03:00
|
|
|
children: [
|
2016-08-04 17:49:32 +02:00
|
|
|
{ name: 'component' },
|
|
|
|
{ name: 'component' },
|
|
|
|
{ name: 'component' }
|
2014-03-18 22:49:59 +03:00
|
|
|
]
|
|
|
|
});
|
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.ok(comp.children().length === 3);
|
|
|
|
assert.ok(comp.el().childNodes.length === 3);
|
2018-01-30 20:26:21 +02:00
|
|
|
|
|
|
|
comp.dispose();
|
2014-03-18 22:49:59 +03:00
|
|
|
});
|
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
QUnit.test('should do a deep merge of child options', function(assert) {
|
2013-01-26 04:36:40 +03:00
|
|
|
// Create a default option for component
|
2019-04-29 18:01:37 +02:00
|
|
|
const oldOptions = Component.prototype.options_;
|
|
|
|
|
2015-03-11 03:01:11 +02:00
|
|
|
Component.prototype.options_ = {
|
2016-08-04 17:49:32 +02:00
|
|
|
example: {
|
|
|
|
childOne: { foo: 'bar', asdf: 'fdsa' },
|
|
|
|
childTwo: {},
|
|
|
|
childThree: {}
|
2013-01-16 19:09:56 +03:00
|
|
|
}
|
2013-02-09 01:14:36 +03:00
|
|
|
};
|
2013-01-16 19:09:56 +03:00
|
|
|
|
2020-11-11 01:09:37 +02:00
|
|
|
const comp = new Component(this.player, {
|
2016-08-04 17:49:32 +02:00
|
|
|
example: {
|
|
|
|
childOne: { foo: 'baz', abc: '123' },
|
|
|
|
childThree: false,
|
|
|
|
childFour: {}
|
2013-01-16 19:09:56 +03:00
|
|
|
}
|
2013-01-26 04:36:40 +03:00
|
|
|
});
|
2013-01-16 19:09:56 +03:00
|
|
|
|
2016-08-04 17:49:32 +02:00
|
|
|
const mergedOptions = comp.options_;
|
|
|
|
const children = mergedOptions.example;
|
2013-01-16 19:09:56 +03:00
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.strictEqual(children.childOne.foo, 'baz', 'value three levels deep overridden');
|
|
|
|
assert.strictEqual(children.childOne.asdf, 'fdsa', 'value three levels deep maintained');
|
|
|
|
assert.strictEqual(children.childOne.abc, '123', 'value three levels deep added');
|
|
|
|
assert.ok(children.childTwo, 'object two levels deep maintained');
|
|
|
|
assert.strictEqual(children.childThree, false, 'object two levels deep removed');
|
|
|
|
assert.ok(children.childFour, 'object two levels deep added');
|
2013-01-26 04:36:40 +03:00
|
|
|
|
2018-09-28 20:58:15 +02:00
|
|
|
assert.strictEqual(
|
|
|
|
Component.prototype.options_.example.childOne.foo,
|
|
|
|
'bar',
|
|
|
|
'prototype options were not overridden'
|
|
|
|
);
|
2013-01-26 04:36:40 +03:00
|
|
|
|
2019-04-29 18:01:37 +02:00
|
|
|
// Reset default component options
|
|
|
|
Component.prototype.options_ = oldOptions;
|
2018-01-30 20:26:21 +02:00
|
|
|
comp.dispose();
|
2013-01-16 19:09:56 +03:00
|
|
|
});
|
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
QUnit.test('should init child components from component options', function(assert) {
|
2019-03-18 21:49:48 +02:00
|
|
|
const player = TestHelpers.makePlayer();
|
|
|
|
const testComp = new TestComponent1(player, {
|
2015-11-06 23:42:19 +02:00
|
|
|
testComponent2: false,
|
|
|
|
testComponent4: {}
|
|
|
|
});
|
|
|
|
|
2016-11-03 21:40:35 +02:00
|
|
|
assert.ok(!testComp.childNameIndex_.TestComponent2, 'we do not have testComponent2');
|
|
|
|
assert.ok(testComp.childNameIndex_.TestComponent4, 'we have a testComponent4');
|
2018-01-30 20:26:21 +02:00
|
|
|
|
2019-03-18 21:49:48 +02:00
|
|
|
player.dispose();
|
2018-01-30 20:26:21 +02:00
|
|
|
testComp.dispose();
|
2015-11-06 23:42:19 +02:00
|
|
|
});
|
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
QUnit.test('should allows setting child options at the parent options level', function(assert) {
|
2016-08-04 17:49:32 +02:00
|
|
|
let parent;
|
2014-10-28 20:45:32 +02:00
|
|
|
|
2014-10-30 22:18:18 +02:00
|
|
|
// using children array
|
2016-08-04 17:49:32 +02:00
|
|
|
let options = {
|
|
|
|
children: [
|
2014-10-30 22:18:18 +02:00
|
|
|
'component',
|
|
|
|
'nullComponent'
|
2014-10-28 20:45:32 +02:00
|
|
|
],
|
|
|
|
// parent-level option for child
|
2016-08-04 17:49:32 +02:00
|
|
|
component: {
|
|
|
|
foo: true
|
2014-10-30 22:18:18 +02:00
|
|
|
},
|
2016-08-04 17:49:32 +02:00
|
|
|
nullComponent: false
|
2014-10-30 22:18:18 +02:00
|
|
|
};
|
2014-10-28 20:45:32 +02:00
|
|
|
|
2014-10-30 22:18:18 +02:00
|
|
|
try {
|
2020-11-11 01:09:37 +02:00
|
|
|
parent = new Component(this.player, options);
|
2016-08-04 17:49:32 +02:00
|
|
|
} catch (err) {
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.ok(false, 'Child with `false` option was initialized');
|
2014-10-30 22:18:18 +02:00
|
|
|
}
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.equal(parent.children()[0].options_.foo, true, 'child options set when children array is used');
|
|
|
|
assert.equal(parent.children().length, 1, 'we should only have one child');
|
2018-01-30 20:26:21 +02:00
|
|
|
parent.dispose();
|
2014-10-28 20:45:32 +02:00
|
|
|
|
2014-10-30 22:18:18 +02:00
|
|
|
// using children object
|
|
|
|
options = {
|
2016-08-04 17:49:32 +02:00
|
|
|
children: {
|
|
|
|
component: {
|
|
|
|
foo: false
|
2014-10-30 22:18:18 +02:00
|
|
|
},
|
2016-08-04 17:49:32 +02:00
|
|
|
nullComponent: {}
|
2014-10-28 20:45:32 +02:00
|
|
|
},
|
|
|
|
// parent-level option for child
|
2016-08-04 17:49:32 +02:00
|
|
|
component: {
|
|
|
|
foo: true
|
2014-10-30 22:18:18 +02:00
|
|
|
},
|
2016-08-04 17:49:32 +02:00
|
|
|
nullComponent: false
|
2014-10-30 22:18:18 +02:00
|
|
|
};
|
2014-10-28 20:45:32 +02:00
|
|
|
|
2014-10-30 22:18:18 +02:00
|
|
|
try {
|
2020-11-11 01:09:37 +02:00
|
|
|
parent = new Component(this.player, options);
|
2016-08-04 17:49:32 +02:00
|
|
|
} catch (err) {
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.ok(false, 'Child with `false` option was initialized');
|
2014-10-30 22:18:18 +02:00
|
|
|
}
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.equal(parent.children()[0].options_.foo, true, 'child options set when children object is used');
|
|
|
|
assert.equal(parent.children().length, 1, 'we should only have one child');
|
2018-01-30 20:26:21 +02:00
|
|
|
parent.dispose();
|
2014-10-28 20:45:32 +02:00
|
|
|
});
|
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
QUnit.test('should dispose of component and children', function(assert) {
|
2020-11-11 01:09:37 +02:00
|
|
|
const comp = new Component(this.player);
|
2012-12-31 08:45:50 +03:00
|
|
|
|
2013-01-11 00:06:12 +03:00
|
|
|
// Add a child
|
2016-08-04 17:49:32 +02:00
|
|
|
const child = comp.addChild('Component');
|
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.ok(comp.children().length === 1);
|
2019-08-30 00:42:16 +02:00
|
|
|
assert.notOk(comp.isDisposed(), 'the component reports that it is not disposed');
|
2012-12-31 08:45:50 +03:00
|
|
|
|
2013-01-11 00:06:12 +03:00
|
|
|
// Add a listener
|
2016-08-04 17:49:32 +02:00
|
|
|
comp.on('click', function() {
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
const el = comp.el();
|
2019-08-01 20:26:59 +02:00
|
|
|
const data = DomData.get(el);
|
2016-08-04 17:49:32 +02:00
|
|
|
|
|
|
|
let hasDisposed = false;
|
|
|
|
let bubbles = null;
|
2012-12-11 03:40:12 +03:00
|
|
|
|
2016-08-04 17:49:32 +02:00
|
|
|
comp.on('dispose', function(event) {
|
2014-02-07 04:11:33 +03:00
|
|
|
hasDisposed = true;
|
|
|
|
bubbles = event.bubbles;
|
|
|
|
});
|
2013-07-19 00:39:14 +03:00
|
|
|
|
2013-01-11 00:06:12 +03:00
|
|
|
comp.dispose();
|
2019-03-18 21:49:48 +02:00
|
|
|
child.dispose();
|
2012-12-11 03:40:12 +03:00
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.ok(hasDisposed, 'component fired dispose event');
|
|
|
|
assert.ok(bubbles === false, 'dispose event does not bubble');
|
|
|
|
assert.ok(!comp.children(), 'component children were deleted');
|
|
|
|
assert.ok(!comp.el(), 'component element was deleted');
|
|
|
|
assert.ok(!child.children(), 'child children were deleted');
|
|
|
|
assert.ok(!child.el(), 'child element was deleted');
|
2019-08-01 20:26:59 +02:00
|
|
|
assert.ok(!DomData.has(el), 'listener data nulled');
|
2018-09-28 20:58:15 +02:00
|
|
|
assert.ok(
|
|
|
|
!Object.getOwnPropertyNames(data).length,
|
|
|
|
'original listener data object was emptied'
|
|
|
|
);
|
2019-08-30 00:42:16 +02:00
|
|
|
assert.ok(comp.isDisposed(), 'the component reports that it is disposed');
|
2012-12-11 03:40:12 +03:00
|
|
|
});
|
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
QUnit.test('should add and remove event listeners to element', function(assert) {
|
2020-11-11 01:09:37 +02:00
|
|
|
const comp = new Component(this.player, {});
|
2012-12-11 03:40:12 +03:00
|
|
|
|
|
|
|
// No need to make this async because we're triggering events inline.
|
|
|
|
// We're going to trigger the event after removing the listener,
|
|
|
|
// So if we get extra asserts that's a problem.
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.expect(2);
|
2012-12-11 03:40:12 +03:00
|
|
|
|
2016-08-04 17:49:32 +02:00
|
|
|
const testListener = function() {
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.ok(true, 'fired event once');
|
|
|
|
assert.ok(this === comp, 'listener has the component as context');
|
2012-12-11 03:40:12 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
comp.on('test-event', testListener);
|
|
|
|
comp.trigger('test-event');
|
|
|
|
comp.off('test-event', testListener);
|
|
|
|
comp.trigger('test-event');
|
2018-01-30 20:26:21 +02:00
|
|
|
|
|
|
|
comp.dispose();
|
2012-12-11 03:40:12 +03:00
|
|
|
});
|
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
QUnit.test('should trigger a listener once using one()', function(assert) {
|
2020-11-11 01:09:37 +02:00
|
|
|
const comp = new Component(this.player, {});
|
2012-12-11 03:40:12 +03:00
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.expect(1);
|
2012-12-11 03:40:12 +03:00
|
|
|
|
2016-08-04 17:49:32 +02:00
|
|
|
const testListener = function() {
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.ok(true, 'fired event once');
|
2012-12-11 03:40:12 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
comp.one('test-event', testListener);
|
|
|
|
comp.trigger('test-event');
|
|
|
|
comp.trigger('test-event');
|
2018-01-30 20:26:21 +02:00
|
|
|
|
|
|
|
comp.dispose();
|
2012-12-31 08:45:50 +03:00
|
|
|
});
|
2013-01-11 00:06:12 +03:00
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
QUnit.test('should be possible to pass data when you trigger an event', function(assert) {
|
2020-11-11 01:09:37 +02:00
|
|
|
const comp = new Component(this.player, {});
|
2016-08-04 17:49:32 +02:00
|
|
|
const data1 = 'Data1';
|
|
|
|
const data2 = {txt: 'Data2'};
|
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.expect(3);
|
2015-06-05 19:36:59 +02:00
|
|
|
|
2016-08-04 17:49:32 +02:00
|
|
|
const testListener = function(evt, hash) {
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.ok(true, 'fired event once');
|
|
|
|
assert.deepEqual(hash.d1, data1);
|
|
|
|
assert.deepEqual(hash.d2, data2);
|
2015-06-05 19:36:59 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
comp.one('test-event', testListener);
|
|
|
|
comp.trigger('test-event', {d1: data1, d2: data2});
|
|
|
|
comp.trigger('test-event');
|
2018-01-30 20:26:21 +02:00
|
|
|
|
|
|
|
comp.dispose();
|
2015-06-05 19:36:59 +02:00
|
|
|
});
|
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
QUnit.test('should add listeners to other components and remove them', function(assert) {
|
2020-11-11 01:09:37 +02:00
|
|
|
const player = this.player;
|
2016-08-04 17:49:32 +02:00
|
|
|
const comp1 = new Component(player);
|
|
|
|
const comp2 = new Component(player);
|
|
|
|
let listenerFired = 0;
|
2014-10-28 20:16:56 +02:00
|
|
|
|
2016-08-04 17:49:32 +02:00
|
|
|
const testListener = function() {
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.equal(this, comp1, 'listener has the first component as context');
|
2014-10-28 20:16:56 +02:00
|
|
|
listenerFired++;
|
|
|
|
};
|
|
|
|
|
|
|
|
comp1.on(comp2, 'test-event', testListener);
|
|
|
|
comp2.trigger('test-event');
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.equal(listenerFired, 1, 'listener was fired once');
|
2014-10-28 20:16:56 +02:00
|
|
|
|
|
|
|
listenerFired = 0;
|
|
|
|
comp1.off(comp2, 'test-event', testListener);
|
|
|
|
comp2.trigger('test-event');
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.equal(listenerFired, 0, 'listener was not fired after being removed');
|
2014-10-28 20:16:56 +02:00
|
|
|
|
|
|
|
// this component is disposed first
|
|
|
|
listenerFired = 0;
|
|
|
|
comp1.on(comp2, 'test-event', testListener);
|
|
|
|
comp1.dispose();
|
|
|
|
comp2.trigger('test-event');
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.equal(listenerFired, 0, 'listener was removed when this component was disposed first');
|
2016-08-04 17:49:32 +02:00
|
|
|
comp1.off = function() {
|
|
|
|
throw new Error('Comp1 off called');
|
|
|
|
};
|
2014-10-28 20:16:56 +02:00
|
|
|
comp2.dispose();
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.ok(true, 'this component removed dispose listeners from other component');
|
2014-10-28 20:16:56 +02:00
|
|
|
});
|
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
QUnit.test('should add listeners to other components and remove when them other component is disposed', function(assert) {
|
2020-11-11 01:09:37 +02:00
|
|
|
const player = this.player;
|
2016-08-04 17:49:32 +02:00
|
|
|
const comp1 = new Component(player);
|
|
|
|
const comp2 = new Component(player);
|
2014-10-28 20:16:56 +02:00
|
|
|
|
2016-08-04 17:49:32 +02:00
|
|
|
const testListener = function() {
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.equal(this, comp1, 'listener has the first component as context');
|
2014-10-28 20:16:56 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
comp1.on(comp2, 'test-event', testListener);
|
|
|
|
comp2.dispose();
|
2016-08-04 17:49:32 +02:00
|
|
|
comp2.off = function() {
|
|
|
|
throw new Error('Comp2 off called');
|
|
|
|
};
|
2014-10-28 20:16:56 +02:00
|
|
|
comp1.dispose();
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.ok(true, 'this component removed dispose listener from this component that referenced other component');
|
2014-10-28 20:16:56 +02:00
|
|
|
});
|
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
QUnit.test('should add listeners to other components that are fired once', function(assert) {
|
2020-11-11 01:09:37 +02:00
|
|
|
const player = this.player;
|
2016-08-04 17:49:32 +02:00
|
|
|
const comp1 = new Component(player);
|
|
|
|
const comp2 = new Component(player);
|
|
|
|
let listenerFired = 0;
|
2014-10-28 20:16:56 +02:00
|
|
|
|
2016-08-04 17:49:32 +02:00
|
|
|
const testListener = function() {
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.equal(this, comp1, 'listener has the first component as context');
|
2014-10-28 20:16:56 +02:00
|
|
|
listenerFired++;
|
|
|
|
};
|
|
|
|
|
|
|
|
comp1.one(comp2, 'test-event', testListener);
|
|
|
|
comp2.trigger('test-event');
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.equal(listenerFired, 1, 'listener was executed once');
|
2014-10-28 20:16:56 +02:00
|
|
|
comp2.trigger('test-event');
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.equal(listenerFired, 1, 'listener was executed only once');
|
2018-01-30 20:26:21 +02:00
|
|
|
|
|
|
|
comp1.dispose();
|
|
|
|
comp2.dispose();
|
2014-10-28 20:16:56 +02:00
|
|
|
});
|
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
QUnit.test('should add listeners to other element and remove them', function(assert) {
|
2020-11-11 01:09:37 +02:00
|
|
|
const player = this.player;
|
2016-08-04 17:49:32 +02:00
|
|
|
const comp1 = new Component(player);
|
|
|
|
const el = document.createElement('div');
|
|
|
|
let listenerFired = 0;
|
2014-10-28 20:16:56 +02:00
|
|
|
|
2016-08-04 17:49:32 +02:00
|
|
|
const testListener = function() {
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.equal(this, comp1, 'listener has the first component as context');
|
2014-10-28 20:16:56 +02:00
|
|
|
listenerFired++;
|
|
|
|
};
|
|
|
|
|
|
|
|
comp1.on(el, 'test-event', testListener);
|
2015-03-26 06:43:41 +02:00
|
|
|
Events.trigger(el, 'test-event');
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.equal(listenerFired, 1, 'listener was fired once');
|
2014-10-28 20:16:56 +02:00
|
|
|
|
|
|
|
listenerFired = 0;
|
|
|
|
comp1.off(el, 'test-event', testListener);
|
2015-03-26 06:43:41 +02:00
|
|
|
Events.trigger(el, 'test-event');
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.equal(listenerFired, 0, 'listener was not fired after being removed from other element');
|
2014-10-28 20:16:56 +02:00
|
|
|
|
|
|
|
// this component is disposed first
|
|
|
|
listenerFired = 0;
|
|
|
|
comp1.on(el, 'test-event', testListener);
|
|
|
|
comp1.dispose();
|
2015-03-26 06:43:41 +02:00
|
|
|
Events.trigger(el, 'test-event');
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.equal(listenerFired, 0, 'listener was removed when this component was disposed first');
|
2016-08-04 17:49:32 +02:00
|
|
|
comp1.off = function() {
|
|
|
|
throw new Error('Comp1 off called');
|
|
|
|
};
|
|
|
|
|
2014-10-28 20:16:56 +02:00
|
|
|
try {
|
2015-03-26 06:43:41 +02:00
|
|
|
Events.trigger(el, 'dispose');
|
2016-08-04 17:49:32 +02:00
|
|
|
} catch (e) {
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.ok(false, 'listener was not removed from other element');
|
2014-10-28 20:16:56 +02:00
|
|
|
}
|
2015-03-26 06:43:41 +02:00
|
|
|
Events.trigger(el, 'dispose');
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.ok(true, 'this component removed dispose listeners from other element');
|
2018-01-30 20:26:21 +02:00
|
|
|
|
|
|
|
comp1.dispose();
|
2014-10-28 20:16:56 +02:00
|
|
|
});
|
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
QUnit.test('should add listeners to other components that are fired once', function(assert) {
|
2020-11-11 01:09:37 +02:00
|
|
|
const player = this.player;
|
2016-08-04 17:49:32 +02:00
|
|
|
const comp1 = new Component(player);
|
|
|
|
const el = document.createElement('div');
|
|
|
|
let listenerFired = 0;
|
2014-10-28 20:16:56 +02:00
|
|
|
|
2016-08-04 17:49:32 +02:00
|
|
|
const testListener = function() {
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.equal(this, comp1, 'listener has the first component as context');
|
2014-10-28 20:16:56 +02:00
|
|
|
listenerFired++;
|
|
|
|
};
|
|
|
|
|
|
|
|
comp1.one(el, 'test-event', testListener);
|
2015-03-26 06:43:41 +02:00
|
|
|
Events.trigger(el, 'test-event');
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.equal(listenerFired, 1, 'listener was executed once');
|
2015-03-26 06:43:41 +02:00
|
|
|
Events.trigger(el, 'test-event');
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.equal(listenerFired, 1, 'listener was executed only once');
|
2018-01-30 20:26:21 +02:00
|
|
|
|
|
|
|
comp1.dispose();
|
2014-10-28 20:16:56 +02:00
|
|
|
});
|
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
QUnit.test('should trigger a listener when ready', function(assert) {
|
2015-07-30 21:38:01 +02:00
|
|
|
let initListenerFired;
|
|
|
|
let methodListenerFired;
|
|
|
|
let syncListenerFired;
|
2013-01-11 00:06:12 +03:00
|
|
|
|
2020-11-11 01:09:37 +02:00
|
|
|
const comp = new Component(this.player, {}, function() {
|
2015-07-30 21:38:01 +02:00
|
|
|
initListenerFired = true;
|
|
|
|
});
|
2013-01-11 00:06:12 +03:00
|
|
|
|
2016-08-04 17:49:32 +02:00
|
|
|
comp.ready(function() {
|
2015-07-30 21:38:01 +02:00
|
|
|
methodListenerFired = true;
|
|
|
|
});
|
2013-01-11 00:06:12 +03:00
|
|
|
|
|
|
|
comp.triggerReady();
|
|
|
|
|
2016-08-04 17:49:32 +02:00
|
|
|
comp.ready(function() {
|
2015-07-30 21:38:01 +02:00
|
|
|
syncListenerFired = true;
|
|
|
|
}, true);
|
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.ok(!initListenerFired, 'init listener should NOT fire synchronously');
|
|
|
|
assert.ok(!methodListenerFired, 'method listener should NOT fire synchronously');
|
|
|
|
assert.ok(syncListenerFired, 'sync listener SHOULD fire synchronously if after ready');
|
2015-07-30 21:38:01 +02:00
|
|
|
|
2015-05-22 06:46:36 +02:00
|
|
|
this.clock.tick(1);
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.ok(initListenerFired, 'init listener should fire asynchronously');
|
|
|
|
assert.ok(methodListenerFired, 'method listener should fire asynchronously');
|
2015-07-30 21:38:01 +02:00
|
|
|
|
|
|
|
// Listeners should only be fired once and then removed
|
|
|
|
initListenerFired = false;
|
|
|
|
methodListenerFired = false;
|
|
|
|
syncListenerFired = false;
|
2013-01-11 00:06:12 +03:00
|
|
|
|
|
|
|
comp.triggerReady();
|
2015-05-22 06:46:36 +02:00
|
|
|
this.clock.tick(1);
|
2015-07-30 21:38:01 +02:00
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.ok(!initListenerFired, 'init listener should be removed');
|
|
|
|
assert.ok(!methodListenerFired, 'method listener should be removed');
|
|
|
|
assert.ok(!syncListenerFired, 'sync listener should be removed');
|
2018-01-30 20:26:21 +02:00
|
|
|
|
|
|
|
comp.dispose();
|
2013-01-11 00:06:12 +03:00
|
|
|
});
|
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
QUnit.test('should not retrigger a listener when the listener calls triggerReady', function(assert) {
|
2016-08-04 17:49:32 +02:00
|
|
|
let timesCalled = 0;
|
|
|
|
let selfTriggered = false;
|
2020-11-11 01:09:37 +02:00
|
|
|
const comp = new Component(this.player, {});
|
2015-07-23 17:28:34 +02:00
|
|
|
|
2016-08-04 17:49:32 +02:00
|
|
|
const readyListener = function() {
|
2015-07-23 17:28:34 +02:00
|
|
|
timesCalled++;
|
|
|
|
|
|
|
|
// Don't bother calling again if we have
|
|
|
|
// already failed
|
|
|
|
if (!selfTriggered) {
|
|
|
|
selfTriggered = true;
|
|
|
|
comp.triggerReady();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
comp.ready(readyListener);
|
|
|
|
comp.triggerReady();
|
|
|
|
|
2015-09-29 18:28:02 +02:00
|
|
|
this.clock.tick(100);
|
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.equal(timesCalled, 1, 'triggerReady from inside a ready handler does not result in an infinite loop');
|
2018-01-30 20:26:21 +02:00
|
|
|
|
|
|
|
comp.dispose();
|
2015-07-23 17:28:34 +02:00
|
|
|
});
|
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
QUnit.test('should add and remove a CSS class', function(assert) {
|
2020-11-11 01:09:37 +02:00
|
|
|
const comp = new Component(this.player, {});
|
2013-01-11 00:06:12 +03:00
|
|
|
|
|
|
|
comp.addClass('test-class');
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.ok(comp.el().className.indexOf('test-class') !== -1);
|
2013-01-11 00:06:12 +03:00
|
|
|
comp.removeClass('test-class');
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.ok(comp.el().className.indexOf('test-class') === -1);
|
2015-11-10 00:43:17 +02:00
|
|
|
comp.toggleClass('test-class');
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.ok(comp.el().className.indexOf('test-class') !== -1);
|
2015-11-10 00:43:17 +02:00
|
|
|
comp.toggleClass('test-class');
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.ok(comp.el().className.indexOf('test-class') === -1);
|
2018-01-30 20:26:21 +02:00
|
|
|
|
|
|
|
comp.dispose();
|
2013-01-11 00:06:12 +03:00
|
|
|
});
|
|
|
|
|
2022-08-08 17:14:06 +02:00
|
|
|
QUnit.test('should add and remove CSS classes', function(assert) {
|
|
|
|
const comp = new Component(this.player, {});
|
|
|
|
|
|
|
|
comp.addClass('first-class', 'second-class');
|
|
|
|
assert.ok(comp.el().className.indexOf('first-class') !== -1);
|
|
|
|
assert.ok(comp.el().className.indexOf('second-class') !== -1);
|
|
|
|
comp.removeClass('first-class', 'second-class');
|
|
|
|
assert.ok(comp.el().className.indexOf('first-class') === -1);
|
|
|
|
assert.ok(comp.el().className.indexOf('second-class') === -1);
|
|
|
|
|
|
|
|
comp.addClass('first-class second-class');
|
|
|
|
assert.ok(comp.el().className.indexOf('first-class') !== -1);
|
|
|
|
assert.ok(comp.el().className.indexOf('second-class') !== -1);
|
|
|
|
comp.removeClass('first-class second-class');
|
|
|
|
assert.ok(comp.el().className.indexOf('first-class') === -1);
|
|
|
|
assert.ok(comp.el().className.indexOf('second-class') === -1);
|
|
|
|
|
|
|
|
comp.addClass('be cool', 'scooby', 'doo');
|
|
|
|
assert.ok(comp.el().className.indexOf('be cool scooby doo') !== -1);
|
|
|
|
comp.removeClass('be cool', 'scooby', 'doo');
|
|
|
|
assert.ok(comp.el().className.indexOf('be cool scooby doo') === -1);
|
|
|
|
|
|
|
|
comp.addClass('multiple spaces between words');
|
|
|
|
assert.ok(comp.el().className.indexOf('multiple spaces between words') !== -1);
|
|
|
|
comp.removeClass('multiple spaces between words');
|
|
|
|
assert.ok(comp.el().className.indexOf('multiple spaces between words') === -1);
|
|
|
|
|
|
|
|
comp.toggleClass('first-class second-class');
|
|
|
|
assert.ok(comp.el().className.indexOf('first-class second-class') !== -1);
|
|
|
|
comp.toggleClass('first-class second-class');
|
|
|
|
assert.ok(comp.el().className.indexOf('first-class second-class') === -1);
|
|
|
|
|
|
|
|
comp.dispose();
|
|
|
|
});
|
|
|
|
|
2022-03-21 18:21:26 +02:00
|
|
|
QUnit.test('should add CSS class passed in options', function(assert) {
|
|
|
|
const comp = new Component(this.player, {className: 'class1 class2'});
|
|
|
|
|
|
|
|
assert.ok(comp.el().className.indexOf('class1') !== -1, 'first of multiple classes added');
|
|
|
|
assert.ok(comp.el().className.indexOf('class2') !== -1, 'second of multiple classes added');
|
|
|
|
|
|
|
|
comp.dispose();
|
|
|
|
|
|
|
|
const comp2 = new Component(this.player, {className: 'class1'});
|
|
|
|
|
|
|
|
assert.ok(comp2.el().className.indexOf('class1') !== -1, 'singe class added');
|
|
|
|
|
|
|
|
comp2.dispose();
|
|
|
|
});
|
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
QUnit.test('should show and hide an element', function(assert) {
|
2020-11-11 01:09:37 +02:00
|
|
|
const comp = new Component(this.player, {});
|
2013-01-11 00:06:12 +03:00
|
|
|
|
|
|
|
comp.hide();
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.ok(comp.hasClass('vjs-hidden') === true);
|
2013-01-11 00:06:12 +03:00
|
|
|
comp.show();
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.ok(comp.hasClass('vjs-hidden') === false);
|
2018-01-30 20:26:21 +02:00
|
|
|
|
|
|
|
comp.dispose();
|
2013-01-11 00:06:12 +03:00
|
|
|
});
|
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
QUnit.test('dimension() should treat NaN and null as zero', function(assert) {
|
2016-08-04 17:49:32 +02:00
|
|
|
let newWidth;
|
2014-08-26 01:41:33 +03:00
|
|
|
|
2016-08-04 17:49:32 +02:00
|
|
|
const width = 300;
|
|
|
|
const height = 150;
|
|
|
|
|
2020-11-11 01:09:37 +02:00
|
|
|
const comp = new Component(this.player, {});
|
2014-08-26 01:41:33 +03:00
|
|
|
// set component dimension
|
|
|
|
|
|
|
|
comp.dimensions(width, height);
|
|
|
|
|
|
|
|
newWidth = comp.dimension('width', null);
|
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.notEqual(newWidth, width, 'new width and old width are not the same');
|
2017-01-18 08:50:22 +02:00
|
|
|
assert.equal(newWidth, undefined, 'we set a value, so, return value is undefined');
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.equal(comp.width(), 0, 'the new width is zero');
|
2014-08-26 01:41:33 +03:00
|
|
|
|
2016-08-04 17:49:32 +02:00
|
|
|
const newHeight = comp.dimension('height', NaN);
|
2014-08-26 01:41:33 +03:00
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.notEqual(newHeight, height, 'new height and old height are not the same');
|
2017-01-18 08:50:22 +02:00
|
|
|
assert.equal(newHeight, undefined, 'we set a value, so, return value is undefined');
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.equal(comp.height(), 0, 'the new height is zero');
|
2014-08-26 01:41:33 +03:00
|
|
|
|
2014-09-02 21:37:42 +03:00
|
|
|
comp.width(width);
|
2014-08-26 01:41:33 +03:00
|
|
|
newWidth = comp.dimension('width', undefined);
|
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.equal(newWidth, width, 'we did not set the width with undefined');
|
2018-01-30 20:26:21 +02:00
|
|
|
|
|
|
|
comp.dispose();
|
2014-08-26 01:41:33 +03:00
|
|
|
});
|
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
QUnit.test('should change the width and height of a component', function(assert) {
|
2016-08-04 17:49:32 +02:00
|
|
|
const container = document.createElement('div');
|
2020-11-11 01:09:37 +02:00
|
|
|
const comp = new Component(this.player, {});
|
2016-08-04 17:49:32 +02:00
|
|
|
const el = comp.el();
|
|
|
|
const fixture = document.getElementById('qunit-fixture');
|
2013-01-11 00:06:12 +03:00
|
|
|
|
|
|
|
fixture.appendChild(container);
|
|
|
|
container.appendChild(el);
|
|
|
|
// Container of el needs dimensions or the component won't have dimensions
|
2013-02-09 01:14:36 +03:00
|
|
|
container.style.width = '1000px';
|
|
|
|
container.style.height = '1000px';
|
2013-01-11 00:06:12 +03:00
|
|
|
|
|
|
|
comp.width('50%');
|
|
|
|
comp.height('123px');
|
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.ok(comp.width() === 500, 'percent values working');
|
2016-08-04 17:49:32 +02:00
|
|
|
const compStyle = TestHelpers.getComputedStyle(el, 'width');
|
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.ok(compStyle === comp.width() + 'px', 'matches computed style');
|
|
|
|
assert.ok(comp.height() === 123, 'px values working');
|
2013-01-11 00:06:12 +03:00
|
|
|
|
|
|
|
comp.width(321);
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.ok(comp.width() === 321, 'integer values working');
|
2013-03-09 11:39:28 +03:00
|
|
|
|
|
|
|
comp.width('auto');
|
|
|
|
comp.height('auto');
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.ok(comp.width() === 1000, 'forced width was removed');
|
|
|
|
assert.ok(comp.height() === 0, 'forced height was removed');
|
2018-01-30 20:26:21 +02:00
|
|
|
|
|
|
|
comp.dispose();
|
2013-01-11 00:06:12 +03:00
|
|
|
});
|
2013-05-01 03:27:36 +03:00
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
QUnit.test('should get the computed dimensions', function(assert) {
|
2016-03-25 21:12:53 +02:00
|
|
|
const container = document.createElement('div');
|
2020-11-11 01:09:37 +02:00
|
|
|
const comp = new Component(this.player, {});
|
2016-03-25 21:12:53 +02:00
|
|
|
const el = comp.el();
|
|
|
|
const fixture = document.getElementById('qunit-fixture');
|
|
|
|
|
|
|
|
const computedWidth = '500px';
|
|
|
|
const computedHeight = '500px';
|
|
|
|
|
|
|
|
fixture.appendChild(container);
|
|
|
|
container.appendChild(el);
|
|
|
|
// Container of el needs dimensions or the component won't have dimensions
|
|
|
|
container.style.width = '1000px';
|
|
|
|
container.style.height = '1000px';
|
|
|
|
|
|
|
|
comp.width('50%');
|
|
|
|
comp.height('50%');
|
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.equal(comp.currentWidth() + 'px', computedWidth, 'matches computed width');
|
|
|
|
assert.equal(comp.currentHeight() + 'px', computedHeight, 'matches computed height');
|
2016-03-25 21:12:53 +02:00
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.equal(comp.currentDimension('width') + 'px', computedWidth, 'matches computed width');
|
|
|
|
assert.equal(comp.currentDimension('height') + 'px', computedHeight, 'matches computed height');
|
2016-03-25 21:12:53 +02:00
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.equal(comp.currentDimensions().width + 'px', computedWidth, 'matches computed width');
|
|
|
|
assert.equal(comp.currentDimensions().height + 'px', computedHeight, 'matches computed width');
|
2016-03-25 21:12:53 +02:00
|
|
|
|
2018-01-30 20:26:21 +02:00
|
|
|
comp.dispose();
|
2016-03-25 21:12:53 +02:00
|
|
|
});
|
2013-05-01 03:27:36 +03:00
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
QUnit.test('should use a defined content el for appending children', function(assert) {
|
2019-04-29 18:01:37 +02:00
|
|
|
class CompWithContent extends Component {
|
|
|
|
createEl() {
|
|
|
|
// Create the main component element
|
|
|
|
const el = Dom.createEl('div');
|
|
|
|
|
|
|
|
// Create the element where children will be appended
|
|
|
|
this.contentEl_ = Dom.createEl('div', { id: 'contentEl' });
|
|
|
|
el.appendChild(this.contentEl_);
|
|
|
|
return el;
|
|
|
|
}
|
|
|
|
}
|
2013-05-01 03:27:36 +03:00
|
|
|
|
2020-11-11 01:09:37 +02:00
|
|
|
const comp = new CompWithContent(this.player);
|
2016-08-04 17:49:32 +02:00
|
|
|
const child = comp.addChild('component');
|
2013-05-01 03:27:36 +03:00
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.ok(comp.children().length === 1);
|
|
|
|
assert.ok(comp.el().childNodes[0].id === 'contentEl');
|
|
|
|
assert.ok(comp.el().childNodes[0].childNodes[0] === child.el());
|
2013-05-01 03:27:36 +03:00
|
|
|
|
|
|
|
comp.removeChild(child);
|
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.ok(comp.children().length === 0, 'Length should now be zero');
|
|
|
|
assert.ok(comp.el().childNodes[0].id === 'contentEl', 'Content El should still exist');
|
2018-09-28 20:58:15 +02:00
|
|
|
assert.ok(
|
|
|
|
comp.el().childNodes[0].childNodes[0] !== child.el(),
|
|
|
|
'Child el should be removed.'
|
|
|
|
);
|
2018-01-30 20:26:21 +02:00
|
|
|
|
2019-03-18 21:49:48 +02:00
|
|
|
child.dispose();
|
2018-01-30 20:26:21 +02:00
|
|
|
comp.dispose();
|
2013-05-01 03:27:36 +03:00
|
|
|
});
|
2013-08-10 00:29:22 +03:00
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
QUnit.test('should emit a tap event', function(assert) {
|
2020-11-11 01:09:37 +02:00
|
|
|
const comp = new Component(this.player);
|
2016-08-04 17:49:32 +02:00
|
|
|
let singleTouch = {};
|
|
|
|
const origTouch = browser.TOUCH_ENABLED;
|
2013-08-10 00:29:22 +03:00
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.expect(3);
|
2013-08-10 00:29:22 +03:00
|
|
|
// Fake touch support. Real touch support isn't needed for this test.
|
2019-08-30 20:56:41 +02:00
|
|
|
browser.stub_TOUCH_ENABLED(true);
|
2013-08-10 00:29:22 +03:00
|
|
|
|
|
|
|
comp.emitTapEvents();
|
2016-08-04 17:49:32 +02:00
|
|
|
comp.on('tap', function() {
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.ok(true, 'Tap event emitted');
|
2013-08-10 00:29:22 +03:00
|
|
|
});
|
2014-05-07 03:22:29 +03:00
|
|
|
|
|
|
|
// A touchstart followed by touchend should trigger a tap
|
2015-03-26 06:43:41 +02:00
|
|
|
Events.trigger(comp.el(), {type: 'touchstart', touches: [{}]});
|
2014-05-07 03:22:29 +03:00
|
|
|
comp.trigger('touchend');
|
|
|
|
|
|
|
|
// A touchmove with a lot of movement should not trigger a tap
|
2015-03-26 06:43:41 +02:00
|
|
|
Events.trigger(comp.el(), {type: 'touchstart', touches: [
|
2014-05-07 03:22:29 +03:00
|
|
|
{ pageX: 0, pageY: 0 }
|
|
|
|
]});
|
2015-03-26 06:43:41 +02:00
|
|
|
Events.trigger(comp.el(), {type: 'touchmove', touches: [
|
2014-05-07 03:22:29 +03:00
|
|
|
{ pageX: 100, pageY: 100 }
|
|
|
|
]});
|
2013-08-10 00:29:22 +03:00
|
|
|
comp.trigger('touchend');
|
|
|
|
|
2014-05-07 03:22:29 +03:00
|
|
|
// A touchmove with not much movement should still allow a tap
|
2015-03-26 06:43:41 +02:00
|
|
|
Events.trigger(comp.el(), {type: 'touchstart', touches: [
|
2014-05-07 03:22:29 +03:00
|
|
|
{ pageX: 0, pageY: 0 }
|
|
|
|
]});
|
2015-03-26 06:43:41 +02:00
|
|
|
Events.trigger(comp.el(), {type: 'touchmove', touches: [
|
2015-01-27 22:45:33 +02:00
|
|
|
{ pageX: 7, pageY: 7 }
|
2014-05-07 03:22:29 +03:00
|
|
|
]});
|
2013-08-10 00:29:22 +03:00
|
|
|
comp.trigger('touchend');
|
|
|
|
|
2015-02-12 22:01:20 +02:00
|
|
|
// A touchmove with a lot of movement by modifying the exisiting touch object
|
|
|
|
// should not trigger a tap
|
|
|
|
singleTouch = { pageX: 0, pageY: 0 };
|
2015-03-26 06:43:41 +02:00
|
|
|
Events.trigger(comp.el(), {type: 'touchstart', touches: [singleTouch]});
|
2015-02-12 22:01:20 +02:00
|
|
|
singleTouch.pageX = 100;
|
|
|
|
singleTouch.pageY = 100;
|
2015-03-26 06:43:41 +02:00
|
|
|
Events.trigger(comp.el(), {type: 'touchmove', touches: [singleTouch]});
|
2015-02-12 22:01:20 +02:00
|
|
|
comp.trigger('touchend');
|
|
|
|
|
|
|
|
// A touchmove with not much movement by modifying the exisiting touch object
|
|
|
|
// should still allow a tap
|
|
|
|
singleTouch = { pageX: 0, pageY: 0 };
|
2015-03-26 06:43:41 +02:00
|
|
|
Events.trigger(comp.el(), {type: 'touchstart', touches: [singleTouch]});
|
2015-01-27 22:45:33 +02:00
|
|
|
singleTouch.pageX = 7;
|
|
|
|
singleTouch.pageY = 7;
|
2015-03-26 06:43:41 +02:00
|
|
|
Events.trigger(comp.el(), {type: 'touchmove', touches: [singleTouch]});
|
2015-02-12 22:01:20 +02:00
|
|
|
comp.trigger('touchend');
|
|
|
|
|
2013-08-10 00:29:22 +03:00
|
|
|
// Reset to orignial value
|
2019-08-30 20:56:41 +02:00
|
|
|
browser.stub_TOUCH_ENABLED(origTouch);
|
2018-01-30 20:26:21 +02:00
|
|
|
comp.dispose();
|
2013-08-10 00:29:22 +03:00
|
|
|
});
|
2014-12-03 21:31:39 +02:00
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
QUnit.test('should provide timeout methods that automatically get cleared on component disposal', function(assert) {
|
2020-11-11 01:09:37 +02:00
|
|
|
const comp = new Component(this.player);
|
2016-08-04 17:49:32 +02:00
|
|
|
let timeoutsFired = 0;
|
|
|
|
const timeoutToClear = comp.setTimeout(function() {
|
|
|
|
timeoutsFired++;
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.ok(false, 'Timeout should have been manually cleared');
|
2016-08-04 17:49:32 +02:00
|
|
|
}, 500);
|
2014-12-03 21:31:39 +02:00
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.expect(4);
|
2014-12-03 21:31:39 +02:00
|
|
|
|
|
|
|
comp.setTimeout(function() {
|
|
|
|
timeoutsFired++;
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.equal(this, comp, 'Timeout fn has the component as its context');
|
|
|
|
assert.ok(true, 'Timeout created and fired.');
|
2014-12-03 21:31:39 +02:00
|
|
|
}, 100);
|
|
|
|
|
|
|
|
comp.setTimeout(function() {
|
|
|
|
timeoutsFired++;
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.ok(false, 'Timeout should have been disposed');
|
2014-12-03 21:31:39 +02:00
|
|
|
}, 1000);
|
|
|
|
|
|
|
|
this.clock.tick(100);
|
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.ok(timeoutsFired === 1, 'One timeout should have fired by this point');
|
2014-12-03 21:31:39 +02:00
|
|
|
|
|
|
|
comp.clearTimeout(timeoutToClear);
|
|
|
|
|
|
|
|
this.clock.tick(500);
|
|
|
|
|
|
|
|
comp.dispose();
|
|
|
|
|
|
|
|
this.clock.tick(1000);
|
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.ok(timeoutsFired === 1, 'One timeout should have fired overall');
|
2014-12-03 21:31:39 +02:00
|
|
|
});
|
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
QUnit.test('should provide interval methods that automatically get cleared on component disposal', function(assert) {
|
2020-11-11 01:09:37 +02:00
|
|
|
const comp = new Component(this.player);
|
2014-12-03 21:31:39 +02:00
|
|
|
|
2016-08-04 17:49:32 +02:00
|
|
|
let intervalsFired = 0;
|
2014-12-03 21:31:39 +02:00
|
|
|
|
2016-08-04 17:49:32 +02:00
|
|
|
const interval = comp.setInterval(function() {
|
2014-12-03 21:31:39 +02:00
|
|
|
intervalsFired++;
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.equal(this, comp, 'Interval fn has the component as its context');
|
|
|
|
assert.ok(true, 'Interval created and fired.');
|
2014-12-03 21:31:39 +02:00
|
|
|
}, 100);
|
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.expect(13);
|
2016-08-04 17:49:32 +02:00
|
|
|
|
2014-12-03 21:31:39 +02:00
|
|
|
comp.setInterval(function() {
|
|
|
|
intervalsFired++;
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.ok(false, 'Interval should have been disposed');
|
2014-12-03 21:31:39 +02:00
|
|
|
}, 1200);
|
|
|
|
|
|
|
|
this.clock.tick(500);
|
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.ok(intervalsFired === 5, 'Component interval fired 5 times');
|
2014-12-03 21:31:39 +02:00
|
|
|
|
|
|
|
comp.clearInterval(interval);
|
|
|
|
|
|
|
|
this.clock.tick(600);
|
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.ok(intervalsFired === 5, 'Interval was manually cleared');
|
2014-12-03 21:31:39 +02:00
|
|
|
|
|
|
|
comp.dispose();
|
|
|
|
|
|
|
|
this.clock.tick(1200);
|
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.ok(intervalsFired === 5, 'Interval was cleared when component was disposed');
|
2014-12-03 21:31:39 +02:00
|
|
|
});
|
2015-11-10 00:43:17 +02:00
|
|
|
|
2020-06-19 20:22:04 +02:00
|
|
|
QUnit.test('should provide a requestAnimationFrame method that is cleared on disposal', function(assert) {
|
2020-11-11 01:09:37 +02:00
|
|
|
const comp = new Component(this.player);
|
2017-01-19 22:30:47 +02:00
|
|
|
const oldRAF = window.requestAnimationFrame;
|
|
|
|
const oldCAF = window.cancelAnimationFrame;
|
|
|
|
|
|
|
|
// Stub the window.*AnimationFrame methods with window.setTimeout methods
|
|
|
|
// so we can control when the callbacks are called via sinon's timer stubs.
|
|
|
|
window.requestAnimationFrame = (fn) => window.setTimeout(fn, 1);
|
|
|
|
window.cancelAnimationFrame = (id) => window.clearTimeout(id);
|
|
|
|
|
|
|
|
const spyRAF = sinon.spy();
|
|
|
|
|
2020-06-19 20:22:04 +02:00
|
|
|
comp.requestNamedAnimationFrame('testing', spyRAF);
|
2017-01-19 22:30:47 +02:00
|
|
|
|
|
|
|
assert.strictEqual(spyRAF.callCount, 0, 'rAF callback was not called immediately');
|
|
|
|
this.clock.tick(1);
|
|
|
|
assert.strictEqual(spyRAF.callCount, 1, 'rAF callback was called after a "repaint"');
|
|
|
|
this.clock.tick(1);
|
|
|
|
assert.strictEqual(spyRAF.callCount, 1, 'rAF callback was not called after a second "repaint"');
|
|
|
|
|
2020-06-19 20:22:04 +02:00
|
|
|
comp.cancelNamedAnimationFrame(comp.requestNamedAnimationFrame('testing', spyRAF));
|
2017-01-19 22:30:47 +02:00
|
|
|
this.clock.tick(1);
|
|
|
|
assert.strictEqual(spyRAF.callCount, 1, 'second rAF callback was not called because it was cancelled');
|
|
|
|
|
2020-06-19 20:22:04 +02:00
|
|
|
comp.requestNamedAnimationFrame('testing', spyRAF);
|
|
|
|
comp.dispose();
|
|
|
|
this.clock.tick(1);
|
|
|
|
assert.strictEqual(spyRAF.callCount, 1, 'third rAF callback was not called because the component was disposed');
|
|
|
|
|
|
|
|
window.requestAnimationFrame = oldRAF;
|
|
|
|
window.cancelAnimationFrame = oldCAF;
|
|
|
|
});
|
|
|
|
|
|
|
|
QUnit.test('should provide a requestNamedAnimationFrame method that is cleared on disposal', function(assert) {
|
2020-11-11 01:09:37 +02:00
|
|
|
const comp = new Component(this.player);
|
2020-06-19 20:22:04 +02:00
|
|
|
const oldRAF = window.requestAnimationFrame;
|
|
|
|
const oldCAF = window.cancelAnimationFrame;
|
|
|
|
|
|
|
|
// Stub the window.*AnimationFrame methods with window.setTimeout methods
|
|
|
|
// so we can control when the callbacks are called via sinon's timer stubs.
|
|
|
|
window.requestAnimationFrame = (fn) => window.setTimeout(fn, 1);
|
|
|
|
window.cancelAnimationFrame = (id) => window.clearTimeout(id);
|
|
|
|
|
|
|
|
const spyRAF = sinon.spy();
|
|
|
|
|
|
|
|
comp.requestNamedAnimationFrame('testing', spyRAF);
|
|
|
|
|
|
|
|
assert.strictEqual(spyRAF.callCount, 0, 'rAF callback was not called immediately');
|
|
|
|
this.clock.tick(1);
|
|
|
|
assert.strictEqual(spyRAF.callCount, 1, 'rAF callback was called after a "repaint"');
|
|
|
|
this.clock.tick(1);
|
|
|
|
assert.strictEqual(spyRAF.callCount, 1, 'rAF callback was not called after a second "repaint"');
|
|
|
|
|
|
|
|
comp.cancelNamedAnimationFrame(comp.requestNamedAnimationFrame('testing', spyRAF));
|
|
|
|
this.clock.tick(1);
|
|
|
|
assert.strictEqual(spyRAF.callCount, 1, 'second rAF callback was not called because it was cancelled');
|
|
|
|
|
|
|
|
comp.requestNamedAnimationFrame('testing', spyRAF);
|
2017-01-19 22:30:47 +02:00
|
|
|
comp.dispose();
|
|
|
|
this.clock.tick(1);
|
|
|
|
assert.strictEqual(spyRAF.callCount, 1, 'third rAF callback was not called because the component was disposed');
|
|
|
|
|
|
|
|
window.requestAnimationFrame = oldRAF;
|
|
|
|
window.cancelAnimationFrame = oldCAF;
|
|
|
|
});
|
|
|
|
|
2018-07-05 22:29:03 +02:00
|
|
|
QUnit.test('setTimeout should remove dispose handler on trigger', function(assert) {
|
2020-11-11 01:09:37 +02:00
|
|
|
const comp = new Component(this.player);
|
2018-07-05 22:29:03 +02:00
|
|
|
|
|
|
|
comp.setTimeout(() => {}, 1);
|
|
|
|
|
2019-08-07 22:11:24 +02:00
|
|
|
assert.equal(comp.setTimeoutIds_.size, 1, 'we removed our dispose handle');
|
2018-07-05 22:29:03 +02:00
|
|
|
|
|
|
|
this.clock.tick(1);
|
|
|
|
|
2019-08-07 22:11:24 +02:00
|
|
|
assert.equal(comp.setTimeoutIds_.size, 0, 'we removed our dispose handle');
|
2018-07-05 22:29:03 +02:00
|
|
|
|
|
|
|
comp.dispose();
|
|
|
|
});
|
|
|
|
|
2020-06-19 20:22:04 +02:00
|
|
|
QUnit.test('requestNamedAnimationFrame should remove dispose handler on trigger', function(assert) {
|
2020-11-11 01:09:37 +02:00
|
|
|
const comp = new Component(this.player);
|
2018-07-05 22:29:03 +02:00
|
|
|
const oldRAF = window.requestAnimationFrame;
|
|
|
|
const oldCAF = window.cancelAnimationFrame;
|
|
|
|
|
|
|
|
// Stub the window.*AnimationFrame methods with window.setTimeout methods
|
|
|
|
// so we can control when the callbacks are called via sinon's timer stubs.
|
|
|
|
window.requestAnimationFrame = (fn) => window.setTimeout(fn, 1);
|
|
|
|
window.cancelAnimationFrame = (id) => window.clearTimeout(id);
|
|
|
|
|
|
|
|
const spyRAF = sinon.spy();
|
|
|
|
|
2020-06-19 20:22:04 +02:00
|
|
|
comp.requestNamedAnimationFrame('testFrame', spyRAF);
|
2018-07-05 22:29:03 +02:00
|
|
|
|
2020-06-19 20:22:04 +02:00
|
|
|
assert.equal(comp.rafIds_.size, 1, 'we got a new raf dispose handler');
|
|
|
|
assert.equal(comp.namedRafs_.size, 1, 'we got a new named raf dispose handler');
|
2019-08-07 22:11:24 +02:00
|
|
|
|
|
|
|
this.clock.tick(1);
|
|
|
|
|
2020-06-19 20:22:04 +02:00
|
|
|
assert.equal(comp.rafIds_.size, 0, 'we removed our raf dispose handle');
|
|
|
|
assert.equal(comp.namedRafs_.size, 0, 'we removed our named raf dispose handle');
|
2019-08-07 22:11:24 +02:00
|
|
|
|
|
|
|
comp.dispose();
|
|
|
|
|
|
|
|
window.requestAnimationFrame = oldRAF;
|
|
|
|
window.cancelAnimationFrame = oldCAF;
|
|
|
|
});
|
|
|
|
|
|
|
|
QUnit.test('requestAnimationFrame should remove dispose handler on trigger', function(assert) {
|
2020-11-11 01:09:37 +02:00
|
|
|
const comp = new Component(this.player);
|
2019-08-07 22:11:24 +02:00
|
|
|
const oldRAF = window.requestAnimationFrame;
|
|
|
|
const oldCAF = window.cancelAnimationFrame;
|
|
|
|
|
|
|
|
// Stub the window.*AnimationFrame methods with window.setTimeout methods
|
|
|
|
// so we can control when the callbacks are called via sinon's timer stubs.
|
|
|
|
window.requestAnimationFrame = (fn) => window.setTimeout(fn, 1);
|
|
|
|
window.cancelAnimationFrame = (id) => window.clearTimeout(id);
|
|
|
|
|
|
|
|
const spyRAF = sinon.spy();
|
|
|
|
|
|
|
|
comp.requestAnimationFrame(spyRAF);
|
|
|
|
|
|
|
|
assert.equal(comp.rafIds_.size, 1, 'we got a new dispose handler');
|
2018-07-05 22:29:03 +02:00
|
|
|
|
|
|
|
this.clock.tick(1);
|
|
|
|
|
2019-08-07 22:11:24 +02:00
|
|
|
assert.equal(comp.rafIds_.size, 0, 'we removed our dispose handle');
|
2018-07-05 22:29:03 +02:00
|
|
|
|
|
|
|
comp.dispose();
|
|
|
|
|
|
|
|
window.requestAnimationFrame = oldRAF;
|
|
|
|
window.cancelAnimationFrame = oldCAF;
|
2017-01-19 22:30:47 +02:00
|
|
|
});
|
|
|
|
|
2019-08-07 22:11:24 +02:00
|
|
|
QUnit.test('setTimeout should be canceled on dispose', function(assert) {
|
2020-11-11 01:09:37 +02:00
|
|
|
const comp = new Component(this.player);
|
2019-08-07 22:11:24 +02:00
|
|
|
let called = false;
|
|
|
|
let clearId;
|
|
|
|
const setId = comp.setTimeout(() => {
|
|
|
|
called = true;
|
|
|
|
}, 1);
|
|
|
|
|
|
|
|
const clearTimeout = comp.clearTimeout;
|
|
|
|
|
|
|
|
comp.clearTimeout = (id) => {
|
|
|
|
clearId = id;
|
|
|
|
return clearTimeout.call(comp, id);
|
|
|
|
};
|
|
|
|
|
|
|
|
assert.equal(comp.setTimeoutIds_.size, 1, 'we added a timeout id');
|
|
|
|
|
|
|
|
comp.dispose();
|
|
|
|
|
|
|
|
assert.equal(comp.setTimeoutIds_.size, 0, 'we removed our timeout id');
|
|
|
|
assert.equal(clearId, setId, 'clearTimeout was called');
|
|
|
|
|
|
|
|
this.clock.tick(1);
|
|
|
|
|
|
|
|
assert.equal(called, false, 'setTimeout was never called');
|
|
|
|
});
|
|
|
|
|
|
|
|
QUnit.test('requestAnimationFrame should be canceled on dispose', function(assert) {
|
2020-11-11 01:09:37 +02:00
|
|
|
const comp = new Component(this.player);
|
2019-08-07 22:11:24 +02:00
|
|
|
let called = false;
|
|
|
|
let clearId;
|
|
|
|
const setId = comp.requestAnimationFrame(() => {
|
|
|
|
called = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
const cancelAnimationFrame = comp.cancelAnimationFrame;
|
|
|
|
|
|
|
|
comp.cancelAnimationFrame = (id) => {
|
|
|
|
clearId = id;
|
|
|
|
return cancelAnimationFrame.call(comp, id);
|
|
|
|
};
|
|
|
|
|
|
|
|
assert.equal(comp.rafIds_.size, 1, 'we added a raf id');
|
|
|
|
|
|
|
|
comp.dispose();
|
|
|
|
|
|
|
|
assert.equal(comp.rafIds_.size, 0, 'we removed a raf id');
|
|
|
|
assert.equal(clearId, setId, 'clearAnimationFrame was called');
|
|
|
|
|
|
|
|
this.clock.tick(1);
|
|
|
|
|
|
|
|
assert.equal(called, false, 'requestAnimationFrame was never called');
|
|
|
|
});
|
|
|
|
|
|
|
|
QUnit.test('setInterval should be canceled on dispose', function(assert) {
|
2020-11-11 01:09:37 +02:00
|
|
|
const comp = new Component(this.player);
|
2019-08-07 22:11:24 +02:00
|
|
|
let called = false;
|
|
|
|
let clearId;
|
|
|
|
const setId = comp.setInterval(() => {
|
|
|
|
called = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
const clearInterval = comp.clearInterval;
|
|
|
|
|
|
|
|
comp.clearInterval = (id) => {
|
|
|
|
clearId = id;
|
|
|
|
return clearInterval.call(comp, id);
|
|
|
|
};
|
|
|
|
|
|
|
|
assert.equal(comp.setIntervalIds_.size, 1, 'we added an interval id');
|
|
|
|
|
|
|
|
comp.dispose();
|
|
|
|
|
|
|
|
assert.equal(comp.setIntervalIds_.size, 0, 'we removed a raf id');
|
|
|
|
assert.equal(clearId, setId, 'clearInterval was called');
|
|
|
|
|
|
|
|
this.clock.tick(1);
|
|
|
|
|
|
|
|
assert.equal(called, false, 'setInterval was never called');
|
|
|
|
});
|
|
|
|
|
2020-06-19 20:22:04 +02:00
|
|
|
QUnit.test('requestNamedAnimationFrame should be canceled on dispose', function(assert) {
|
2020-11-11 01:09:37 +02:00
|
|
|
const comp = new Component(this.player);
|
2020-06-19 20:22:04 +02:00
|
|
|
let called = false;
|
|
|
|
let clearName;
|
|
|
|
const setName = comp.requestNamedAnimationFrame('testing', () => {
|
|
|
|
called = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
const cancelNamedAnimationFrame = comp.cancelNamedAnimationFrame;
|
|
|
|
|
|
|
|
comp.cancelNamedAnimationFrame = (name) => {
|
|
|
|
clearName = name;
|
|
|
|
return cancelNamedAnimationFrame.call(comp, name);
|
|
|
|
};
|
|
|
|
|
|
|
|
assert.equal(comp.namedRafs_.size, 1, 'we added a named raf');
|
|
|
|
assert.equal(comp.rafIds_.size, 1, 'we added a raf id');
|
|
|
|
|
|
|
|
comp.dispose();
|
|
|
|
|
|
|
|
assert.equal(comp.namedRafs_.size, 0, 'we removed a named raf');
|
|
|
|
assert.equal(comp.rafIds_.size, 0, 'we removed a raf id');
|
|
|
|
assert.equal(clearName, setName, 'cancelNamedAnimationFrame was called');
|
|
|
|
|
|
|
|
this.clock.tick(1);
|
|
|
|
|
|
|
|
assert.equal(called, false, 'requestNamedAnimationFrame was never called');
|
|
|
|
});
|
|
|
|
|
|
|
|
QUnit.test('requestNamedAnimationFrame should only allow one raf of a specific name at a time', function(assert) {
|
2020-11-11 01:09:37 +02:00
|
|
|
const comp = new Component(this.player);
|
2020-06-19 20:22:04 +02:00
|
|
|
const calls = {
|
|
|
|
one: 0,
|
|
|
|
two: 0,
|
|
|
|
three: 0
|
|
|
|
};
|
|
|
|
const cancelNames = [];
|
|
|
|
const name = 'testing';
|
|
|
|
const handlerOne = () => {
|
|
|
|
assert.equal(comp.namedRafs_.size, 1, 'named raf still exists while function runs');
|
|
|
|
assert.equal(comp.rafIds_.size, 0, 'raf id does not exist during run');
|
|
|
|
|
|
|
|
calls.one++;
|
|
|
|
};
|
|
|
|
const handlerTwo = () => {
|
|
|
|
assert.equal(comp.namedRafs_.size, 1, 'named raf still exists while function runs');
|
|
|
|
assert.equal(comp.rafIds_.size, 0, 'raf id does not exist during run');
|
|
|
|
calls.two++;
|
|
|
|
};
|
|
|
|
const handlerThree = () => {
|
|
|
|
assert.equal(comp.namedRafs_.size, 1, 'named raf still exists while function runs');
|
|
|
|
assert.equal(comp.rafIds_.size, 0, 'raf id does not exist during run');
|
|
|
|
calls.three++;
|
|
|
|
};
|
|
|
|
|
|
|
|
const oldRAF = window.requestAnimationFrame;
|
|
|
|
const oldCAF = window.cancelAnimationFrame;
|
|
|
|
|
|
|
|
// Stub the window.*AnimationFrame methods with window.setTimeout methods
|
|
|
|
// so we can control when the callbacks are called via sinon's timer stubs.
|
|
|
|
window.requestAnimationFrame = (fn) => window.setTimeout(fn, 1);
|
|
|
|
window.cancelAnimationFrame = (id) => window.clearTimeout(id);
|
|
|
|
|
|
|
|
const cancelNamedAnimationFrame = comp.cancelNamedAnimationFrame;
|
|
|
|
|
|
|
|
comp.cancelNamedAnimationFrame = (_name) => {
|
|
|
|
cancelNames.push(_name);
|
|
|
|
return cancelNamedAnimationFrame.call(comp, _name);
|
|
|
|
};
|
|
|
|
|
|
|
|
comp.requestNamedAnimationFrame(name, handlerOne);
|
|
|
|
|
|
|
|
assert.equal(comp.namedRafs_.size, 1, 'we added a named raf');
|
|
|
|
assert.equal(comp.rafIds_.size, 1, 'we added a raf id');
|
|
|
|
|
|
|
|
comp.requestNamedAnimationFrame(name, handlerTwo);
|
|
|
|
|
|
|
|
assert.deepEqual(cancelNames, [], 'no named cancels');
|
|
|
|
assert.equal(comp.namedRafs_.size, 1, 'still only one named raf');
|
|
|
|
assert.equal(comp.rafIds_.size, 1, 'still only one raf id');
|
|
|
|
|
|
|
|
this.clock.tick(1);
|
|
|
|
|
|
|
|
assert.equal(comp.namedRafs_.size, 0, 'we removed a named raf');
|
|
|
|
assert.equal(comp.rafIds_.size, 0, 'we removed a raf id');
|
|
|
|
assert.deepEqual(calls, {
|
|
|
|
one: 1,
|
|
|
|
two: 0,
|
|
|
|
three: 0
|
|
|
|
}, 'only handlerOne was called');
|
|
|
|
|
|
|
|
comp.requestNamedAnimationFrame(name, handlerOne);
|
|
|
|
comp.requestNamedAnimationFrame(name, handlerTwo);
|
|
|
|
comp.requestNamedAnimationFrame(name, handlerThree);
|
|
|
|
|
|
|
|
assert.deepEqual(cancelNames, [], 'no named cancels for testing');
|
|
|
|
assert.equal(comp.namedRafs_.size, 1, 'only added one named raf');
|
|
|
|
assert.equal(comp.rafIds_.size, 1, 'only added one named raf');
|
|
|
|
|
|
|
|
this.clock.tick(1);
|
|
|
|
|
|
|
|
assert.equal(comp.namedRafs_.size, 0, 'we removed a named raf');
|
|
|
|
assert.equal(comp.rafIds_.size, 0, 'we removed a raf id');
|
|
|
|
assert.deepEqual(calls, {
|
|
|
|
one: 2,
|
|
|
|
two: 0,
|
|
|
|
three: 0
|
|
|
|
}, 'only the handlerOne called');
|
|
|
|
|
|
|
|
window.requestAnimationFrame = oldRAF;
|
|
|
|
window.cancelAnimationFrame = oldCAF;
|
|
|
|
});
|
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
QUnit.test('$ and $$ functions', function(assert) {
|
2020-11-11 01:09:37 +02:00
|
|
|
const comp = new Component(this.player);
|
2016-08-04 17:49:32 +02:00
|
|
|
const contentEl = document.createElement('div');
|
|
|
|
const children = [
|
2015-11-10 00:43:17 +02:00
|
|
|
document.createElement('div'),
|
|
|
|
document.createElement('div')
|
|
|
|
];
|
|
|
|
|
|
|
|
comp.contentEl_ = contentEl;
|
|
|
|
children.forEach(child => contentEl.appendChild(child));
|
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.strictEqual(comp.$('div'), children[0], '$ defaults to contentEl as scope');
|
|
|
|
assert.strictEqual(comp.$$('div').length, children.length, '$$ defaults to contentEl as scope');
|
2018-01-30 20:26:21 +02:00
|
|
|
|
|
|
|
comp.dispose();
|
2015-11-10 00:43:17 +02:00
|
|
|
});
|
2017-01-27 05:04:34 +02:00
|
|
|
|
|
|
|
QUnit.test('should use the stateful mixin', function(assert) {
|
2020-11-11 01:09:37 +02:00
|
|
|
const comp = new Component(this.player, {});
|
2017-01-27 05:04:34 +02:00
|
|
|
|
|
|
|
assert.ok(Obj.isPlain(comp.state), '`state` is a plain object');
|
|
|
|
assert.strictEqual(Object.prototype.toString.call(comp.setState), '[object Function]', '`setState` is a function');
|
|
|
|
|
|
|
|
comp.setState({foo: 'bar'});
|
|
|
|
assert.strictEqual(comp.state.foo, 'bar', 'the component passes a basic stateful test');
|
2018-01-30 20:26:21 +02:00
|
|
|
|
|
|
|
comp.dispose();
|
2017-01-27 05:04:34 +02:00
|
|
|
});
|
2019-01-08 21:14:46 +02:00
|
|
|
|
|
|
|
QUnit.test('should remove child when the child moves to the other parent', function(assert) {
|
2020-11-11 01:09:37 +02:00
|
|
|
const parentComponent1 = new Component(this.player, {});
|
|
|
|
const parentComponent2 = new Component(this.player, {});
|
|
|
|
const childComponent = new Component(this.player, {});
|
2019-01-08 21:14:46 +02:00
|
|
|
|
|
|
|
parentComponent1.addChild(childComponent);
|
|
|
|
|
|
|
|
assert.strictEqual(parentComponent1.children().length, 1, 'the children number of `parentComponent1` is 1');
|
|
|
|
assert.strictEqual(parentComponent1.children()[0], childComponent, 'the first child of `parentComponent1` is `childComponent`');
|
|
|
|
assert.strictEqual(parentComponent1.el().childNodes[0], childComponent.el(), '`parentComponent1` contains the DOM element of `childComponent`');
|
|
|
|
|
|
|
|
parentComponent2.addChild(childComponent);
|
|
|
|
|
|
|
|
assert.strictEqual(parentComponent1.children().length, 0, 'the children number of `parentComponent1` is 0');
|
|
|
|
assert.strictEqual(parentComponent1.el().childNodes.length, 0, 'the length of `childNodes` of `parentComponent1` is 0');
|
|
|
|
|
|
|
|
assert.strictEqual(parentComponent2.children().length, 1, 'the children number of `parentComponent2` is 1');
|
|
|
|
assert.strictEqual(parentComponent2.children()[0], childComponent, 'the first child of `parentComponent2` is `childComponent`');
|
|
|
|
assert.strictEqual(parentComponent2.el().childNodes.length, 1, 'the length of `childNodes` of `parentComponent2` is 1');
|
|
|
|
assert.strictEqual(parentComponent2.el().childNodes[0], childComponent.el(), '`parentComponent2` contains the DOM element of `childComponent`');
|
2019-03-18 21:49:48 +02:00
|
|
|
|
|
|
|
parentComponent1.dispose();
|
|
|
|
parentComponent2.dispose();
|
|
|
|
childComponent.dispose();
|
2019-01-08 21:14:46 +02:00
|
|
|
});
|
2020-03-30 23:39:23 +02:00
|
|
|
|
|
|
|
QUnit.test('getDescendant should work as expected', function(assert) {
|
2020-11-11 01:09:37 +02:00
|
|
|
const comp = new Component(this.player, {name: 'component'});
|
|
|
|
const descendant1 = new Component(this.player, {name: 'descendant1'});
|
|
|
|
const descendant2 = new Component(this.player, {name: 'descendant2'});
|
|
|
|
const descendant3 = new Component(this.player, {name: 'descendant3'});
|
2020-03-30 23:39:23 +02:00
|
|
|
|
|
|
|
comp.addChild(descendant1);
|
|
|
|
descendant1.addChild(descendant2);
|
|
|
|
descendant2.addChild(descendant3);
|
|
|
|
|
|
|
|
assert.equal(comp.getDescendant('descendant1', 'descendant2', 'descendant3'), descendant3, 'can pass as args');
|
|
|
|
assert.equal(comp.getDescendant(['descendant1', 'descendant2', 'descendant3']), descendant3, 'can pass as array');
|
|
|
|
assert.equal(comp.getDescendant('descendant1'), descendant1, 'can pass as single string');
|
|
|
|
assert.equal(comp.getDescendant(), comp, 'no args returns base component');
|
|
|
|
assert.notOk(comp.getDescendant('descendant5'), 'undefined descendant returned');
|
|
|
|
assert.notOk(comp.getDescendant('descendant1', 'descendant5'), 'undefined descendant returned');
|
|
|
|
assert.notOk(comp.getDescendant(['descendant1', 'descendant5']), 'undefined descendant returned');
|
|
|
|
|
|
|
|
comp.dispose();
|
|
|
|
});
|
2020-12-11 23:04:09 +02:00
|
|
|
|
|
|
|
QUnit.test('ready queue should not run after dispose', function(assert) {
|
|
|
|
let option = false;
|
|
|
|
let callback = false;
|
|
|
|
|
|
|
|
const comp = new Component(this.player, {name: 'component'}, () => {
|
|
|
|
option = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
comp.ready(() => {
|
|
|
|
callback = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
comp.dispose();
|
|
|
|
comp.triggerReady();
|
|
|
|
// TODO: improve this error. It is a variant of:
|
|
|
|
// "Cannot read property 'parentNode' of null"
|
|
|
|
//
|
|
|
|
// but on some browsers such as IE 11 and safari 9 other errors are thrown,
|
|
|
|
// I think any error at all works for our purposes here.
|
|
|
|
assert.throws(() => this.clock.tick(1), /.*/, 'throws trigger error');
|
|
|
|
|
|
|
|
assert.notOk(option, 'ready option not run');
|
|
|
|
assert.notOk(callback, 'ready callback not run');
|
|
|
|
|
|
|
|
});
|
2022-05-18 16:59:17 +02:00
|
|
|
|
|
|
|
QUnit.test('a component\'s el can be replaced on dispose', function(assert) {
|
|
|
|
const comp = this.player.addChild('Component', {}, {}, 2);
|
|
|
|
const prevIndex = Array.from(this.player.el_.childNodes).indexOf(comp.el_);
|
|
|
|
const replacementEl = document.createElement('div');
|
|
|
|
|
|
|
|
comp.dispose({restoreEl: replacementEl});
|
|
|
|
|
|
|
|
assert.strictEqual(replacementEl.parentNode, this.player.el_, 'replacement was inserted');
|
|
|
|
assert.strictEqual(Array.from(this.player.el_.childNodes).indexOf(replacementEl), prevIndex, 'replacement was inserted at same position');
|
|
|
|
|
|
|
|
});
|