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';
|
2017-01-19 23:01:56 +02:00
|
|
|
import * as 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 {}
|
|
|
|
TestComponent1.prototype.options_ = {
|
|
|
|
children: [
|
|
|
|
'testComponent2',
|
|
|
|
'testComponent3'
|
|
|
|
]
|
|
|
|
};
|
|
|
|
Component.registerComponent('TestComponent1', TestComponent1);
|
|
|
|
Component.registerComponent('TestComponent2', TestComponent2);
|
|
|
|
Component.registerComponent('TestComponent3', TestComponent3);
|
|
|
|
Component.registerComponent('TestComponent4', TestComponent4);
|
|
|
|
|
2016-08-04 17:49:32 +02:00
|
|
|
QUnit.module('Component', {
|
2016-08-12 19:51:31 +02:00
|
|
|
beforeEach() {
|
2014-12-03 21:31:39 +02:00
|
|
|
this.clock = sinon.useFakeTimers();
|
|
|
|
},
|
2016-08-12 19:51:31 +02:00
|
|
|
afterEach() {
|
2014-12-03 21:31:39 +02:00
|
|
|
this.clock.restore();
|
|
|
|
}
|
|
|
|
});
|
2012-12-11 03:40:12 +03:00
|
|
|
|
2016-08-04 17:49:32 +02:00
|
|
|
const getFakePlayer = function() {
|
2013-01-18 04:33:53 +03:00
|
|
|
return {
|
|
|
|
// Fake player requries an ID
|
2016-08-04 17:49:32 +02:00
|
|
|
id() {
|
|
|
|
return 'player_1';
|
|
|
|
},
|
|
|
|
reportUserActivity() {}
|
2013-02-09 01:14:36 +03:00
|
|
|
};
|
2013-01-18 04:33:53 +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) {
|
2016-08-04 17:49:32 +02:00
|
|
|
const comp = new Component(getFakePlayer(), {});
|
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) {
|
2016-08-04 17:49:32 +02:00
|
|
|
const comp = new Component(getFakePlayer());
|
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) {
|
2016-08-04 17:49:32 +02:00
|
|
|
const comp = new Component(getFakePlayer());
|
|
|
|
|
|
|
|
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
|
|
|
});
|
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
QUnit.test('addChild should throw if the child does not exist', function(assert) {
|
2016-08-04 17:49:32 +02:00
|
|
|
const comp = new Component(getFakePlayer());
|
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) {
|
|
|
|
const comp = new Component(getFakePlayer());
|
|
|
|
const comp2 = new Component(getFakePlayer());
|
|
|
|
|
|
|
|
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) {
|
|
|
|
const comp = new Component(getFakePlayer());
|
|
|
|
|
|
|
|
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) {
|
2016-08-04 17:49:32 +02:00
|
|
|
const comp = new Component(getFakePlayer(), {
|
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) {
|
2016-08-04 17:49:32 +02:00
|
|
|
const comp = new Component(getFakePlayer(), {
|
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) {
|
2016-08-04 17:49:32 +02:00
|
|
|
const comp = new Component(getFakePlayer(), {
|
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
|
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
|
|
|
|
2016-08-04 17:49:32 +02:00
|
|
|
const comp = new Component(getFakePlayer(), {
|
|
|
|
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
|
|
|
|
|
|
|
// Reset default component options to none
|
2015-03-11 03:01:11 +02:00
|
|
|
Component.prototype.options_ = null;
|
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) {
|
2016-08-04 17:49:32 +02:00
|
|
|
const testComp = new TestComponent1(TestHelpers.makePlayer(), {
|
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
|
|
|
|
|
|
|
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 {
|
2015-03-11 03:01:11 +02:00
|
|
|
parent = new Component(getFakePlayer(), 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 {
|
2015-03-11 03:01:11 +02:00
|
|
|
parent = new Component(getFakePlayer(), 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) {
|
2016-08-04 17:49:32 +02:00
|
|
|
const comp = new Component(getFakePlayer());
|
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);
|
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();
|
2017-01-19 23:01:56 +02:00
|
|
|
const data = DomData.getData(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();
|
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');
|
2017-01-19 23:01:56 +02:00
|
|
|
assert.ok(!DomData.hasData(el), 'listener data nulled');
|
2018-09-28 20:58:15 +02:00
|
|
|
assert.ok(
|
|
|
|
!Object.getOwnPropertyNames(data).length,
|
|
|
|
'original listener data object was emptied'
|
|
|
|
);
|
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) {
|
2016-08-04 17:49:32 +02:00
|
|
|
const comp = new Component(getFakePlayer(), {});
|
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) {
|
2016-08-04 17:49:32 +02:00
|
|
|
const comp = new Component(getFakePlayer(), {});
|
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) {
|
2016-08-04 17:49:32 +02:00
|
|
|
const comp = new Component(getFakePlayer(), {});
|
|
|
|
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) {
|
2016-08-04 17:49:32 +02:00
|
|
|
const player = getFakePlayer();
|
|
|
|
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) {
|
2016-08-04 17:49:32 +02:00
|
|
|
const player = getFakePlayer();
|
|
|
|
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) {
|
2016-08-04 17:49:32 +02:00
|
|
|
const player = getFakePlayer();
|
|
|
|
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) {
|
2016-08-04 17:49:32 +02:00
|
|
|
const player = getFakePlayer();
|
|
|
|
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) {
|
2016-08-04 17:49:32 +02:00
|
|
|
const player = getFakePlayer();
|
|
|
|
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
|
|
|
|
2016-08-04 17:49:32 +02:00
|
|
|
const comp = new Component(getFakePlayer(), {}, 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;
|
|
|
|
const comp = new Component(getFakePlayer(), {});
|
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) {
|
2016-08-04 17:49:32 +02:00
|
|
|
const comp = new Component(getFakePlayer(), {});
|
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
|
|
|
});
|
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
QUnit.test('should show and hide an element', function(assert) {
|
2016-08-04 17:49:32 +02:00
|
|
|
const comp = new Component(getFakePlayer(), {});
|
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;
|
|
|
|
|
|
|
|
const comp = new Component(getFakePlayer(), {});
|
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');
|
|
|
|
const comp = new Component(getFakePlayer(), {});
|
|
|
|
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');
|
|
|
|
const comp = new Component(getFakePlayer(), {});
|
|
|
|
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) {
|
2015-04-14 22:08:32 +02:00
|
|
|
class CompWithContent extends Component {}
|
|
|
|
|
2016-08-04 17:49:32 +02:00
|
|
|
CompWithContent.prototype.createEl = function() {
|
2017-01-18 07:46:43 +02:00
|
|
|
// Create the main component element
|
2016-08-04 17:49:32 +02:00
|
|
|
const el = Dom.createEl('div');
|
|
|
|
|
2013-05-01 03:27:36 +03:00
|
|
|
// Create the element where children will be appended
|
2016-08-04 17:49:32 +02:00
|
|
|
this.contentEl_ = Dom.createEl('div', { id: 'contentEl' });
|
2013-05-01 03:27:36 +03:00
|
|
|
el.appendChild(this.contentEl_);
|
|
|
|
return el;
|
|
|
|
};
|
|
|
|
|
2016-08-04 17:49:32 +02:00
|
|
|
const comp = new CompWithContent(getFakePlayer());
|
|
|
|
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
|
|
|
|
|
|
|
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) {
|
2016-08-04 17:49:32 +02:00
|
|
|
const comp = new Component(getFakePlayer());
|
|
|
|
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.
|
2015-05-04 01:12:38 +02:00
|
|
|
browser.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
|
2015-05-04 01:12:38 +02:00
|
|
|
browser.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) {
|
2016-08-04 17:49:32 +02:00
|
|
|
const comp = new Component(getFakePlayer());
|
|
|
|
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) {
|
2016-08-04 17:49:32 +02:00
|
|
|
const comp = new Component(getFakePlayer());
|
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
|
|
|
|
2017-01-19 22:30:47 +02:00
|
|
|
QUnit.test('should provide *AnimationFrame methods that automatically get cleared on component disposal', function(assert) {
|
|
|
|
const comp = new Component(getFakePlayer());
|
|
|
|
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);
|
|
|
|
|
|
|
|
// Make sure the component thinks it supports rAF.
|
|
|
|
comp.supportsRaf_ = true;
|
|
|
|
|
|
|
|
const spyRAF = sinon.spy();
|
|
|
|
|
|
|
|
comp.requestAnimationFrame(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.cancelAnimationFrame(comp.requestAnimationFrame(spyRAF));
|
|
|
|
this.clock.tick(1);
|
|
|
|
assert.strictEqual(spyRAF.callCount, 1, 'second rAF callback was not called because it was cancelled');
|
|
|
|
|
|
|
|
comp.requestAnimationFrame(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('*AnimationFrame methods fall back to timers if rAF not supported', function(assert) {
|
|
|
|
const comp = new Component(getFakePlayer());
|
|
|
|
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.
|
|
|
|
const rAF = window.requestAnimationFrame = sinon.spy();
|
|
|
|
const cAF = window.cancelAnimationFrame = sinon.spy();
|
|
|
|
|
|
|
|
// Make sure the component thinks it does not support rAF.
|
|
|
|
comp.supportsRaf_ = false;
|
|
|
|
|
|
|
|
sinon.spy(comp, 'setTimeout');
|
|
|
|
sinon.spy(comp, 'clearTimeout');
|
|
|
|
|
|
|
|
comp.cancelAnimationFrame(comp.requestAnimationFrame(() => {}));
|
|
|
|
|
|
|
|
assert.strictEqual(rAF.callCount, 0, 'window.requestAnimationFrame was not called');
|
|
|
|
assert.strictEqual(cAF.callCount, 0, 'window.cancelAnimationFrame was not called');
|
|
|
|
assert.strictEqual(comp.setTimeout.callCount, 1, 'Component#setTimeout was called');
|
|
|
|
assert.strictEqual(comp.clearTimeout.callCount, 1, 'Component#clearTimeout was called');
|
|
|
|
|
|
|
|
comp.dispose();
|
|
|
|
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) {
|
|
|
|
const comp = new Component(getFakePlayer());
|
|
|
|
const el = comp.el();
|
|
|
|
const data = DomData.getData(el);
|
|
|
|
|
|
|
|
comp.setTimeout(() => {}, 1);
|
|
|
|
|
|
|
|
assert.equal(data.handlers.dispose.length, 2, 'we got a new dispose handler');
|
|
|
|
assert.ok(/vjs-timeout-\d/.test(data.handlers.dispose[1].guid), 'we got a new dispose handler');
|
|
|
|
|
|
|
|
this.clock.tick(1);
|
|
|
|
|
|
|
|
assert.equal(data.handlers.dispose.length, 1, 'we removed our dispose handle');
|
|
|
|
|
|
|
|
comp.dispose();
|
|
|
|
});
|
|
|
|
|
|
|
|
QUnit.test('requestAnimationFrame should remove dispose handler on trigger', function(assert) {
|
|
|
|
const comp = new Component(getFakePlayer());
|
|
|
|
const el = comp.el();
|
|
|
|
const data = DomData.getData(el);
|
|
|
|
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);
|
|
|
|
|
|
|
|
// Make sure the component thinks it supports rAF.
|
|
|
|
comp.supportsRaf_ = true;
|
|
|
|
|
|
|
|
const spyRAF = sinon.spy();
|
|
|
|
|
|
|
|
comp.requestAnimationFrame(spyRAF);
|
|
|
|
|
|
|
|
assert.equal(data.handlers.dispose.length, 2, 'we got a new dispose handler');
|
|
|
|
assert.ok(/vjs-raf-\d/.test(data.handlers.dispose[1].guid), 'we got a new dispose handler');
|
|
|
|
|
|
|
|
this.clock.tick(1);
|
|
|
|
|
|
|
|
assert.equal(data.handlers.dispose.length, 1, 'we removed our dispose handle');
|
|
|
|
|
|
|
|
comp.dispose();
|
|
|
|
|
|
|
|
window.requestAnimationFrame = oldRAF;
|
|
|
|
window.cancelAnimationFrame = oldCAF;
|
2017-01-19 22:30:47 +02:00
|
|
|
});
|
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
QUnit.test('$ and $$ functions', function(assert) {
|
2016-08-04 17:49:32 +02:00
|
|
|
const comp = new Component(getFakePlayer());
|
|
|
|
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) {
|
|
|
|
const comp = new Component(getFakePlayer(), {});
|
|
|
|
|
|
|
|
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
|
|
|
});
|