2013-02-09 01:14:36 +03:00
|
|
|
module('Component');
|
2012-12-11 03:40:12 +03:00
|
|
|
|
2013-01-18 04:33:53 +03:00
|
|
|
var getFakePlayer = function(){
|
|
|
|
return {
|
|
|
|
// Fake player requries an ID
|
2014-02-07 03:54:35 +03:00
|
|
|
id: function(){ return 'player_1'; },
|
|
|
|
reportUserActivity: function(){}
|
2013-02-09 01:14:36 +03:00
|
|
|
};
|
2013-01-18 04:33:53 +03:00
|
|
|
};
|
|
|
|
|
2012-12-11 03:40:12 +03:00
|
|
|
test('should create an element', function(){
|
2013-01-18 04:33:53 +03:00
|
|
|
var comp = new vjs.Component(getFakePlayer(), {});
|
2012-12-11 03:40:12 +03:00
|
|
|
|
2013-01-11 00:06:12 +03:00
|
|
|
ok(comp.el().nodeName);
|
2012-12-11 03:40:12 +03:00
|
|
|
});
|
|
|
|
|
2012-12-31 08:45:50 +03:00
|
|
|
test('should add a child component', function(){
|
2013-01-18 04:33:53 +03:00
|
|
|
var comp = new vjs.Component(getFakePlayer());
|
2012-12-31 08:45:50 +03:00
|
|
|
|
2013-02-09 01:14:36 +03:00
|
|
|
var child = comp.addChild('component');
|
2012-12-31 08:45:50 +03:00
|
|
|
|
2013-01-11 00:06:12 +03:00
|
|
|
ok(comp.children().length === 1);
|
|
|
|
ok(comp.children()[0] === child);
|
|
|
|
ok(comp.el().childNodes[0] === child.el());
|
|
|
|
ok(comp.getChild('component') === child);
|
|
|
|
ok(comp.getChildById(child.id()) === child);
|
2012-12-31 08:45:50 +03:00
|
|
|
});
|
|
|
|
|
2014-03-18 22:49:59 +03:00
|
|
|
test('should init child components from options', function(){
|
2013-01-18 04:33:53 +03:00
|
|
|
var comp = new vjs.Component(getFakePlayer(), {
|
2013-01-11 00:06:12 +03:00
|
|
|
children: {
|
|
|
|
'component': true
|
|
|
|
}
|
|
|
|
});
|
2012-12-31 08:45:50 +03:00
|
|
|
|
2013-01-11 00:06:12 +03:00
|
|
|
ok(comp.children().length === 1);
|
|
|
|
ok(comp.el().childNodes.length === 1);
|
|
|
|
});
|
2012-12-31 08:45:50 +03:00
|
|
|
|
2014-03-18 22:49:59 +03:00
|
|
|
test('should init child components from simple children array', function(){
|
|
|
|
var comp = new vjs.Component(getFakePlayer(), {
|
|
|
|
children: [
|
|
|
|
'component',
|
|
|
|
'component',
|
|
|
|
'component'
|
|
|
|
]
|
|
|
|
});
|
|
|
|
|
|
|
|
ok(comp.children().length === 3);
|
|
|
|
ok(comp.el().childNodes.length === 3);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should init child components from children array of objects', function(){
|
|
|
|
var comp = new vjs.Component(getFakePlayer(), {
|
|
|
|
children: [
|
2014-05-06 03:11:14 +03:00
|
|
|
{ 'name': 'component' },
|
|
|
|
{ 'name': 'component' },
|
|
|
|
{ 'name': 'component' }
|
2014-03-18 22:49:59 +03:00
|
|
|
]
|
|
|
|
});
|
|
|
|
|
|
|
|
ok(comp.children().length === 3);
|
|
|
|
ok(comp.el().childNodes.length === 3);
|
|
|
|
});
|
|
|
|
|
2013-01-16 19:09:56 +03:00
|
|
|
test('should do a deep merge of child options', function(){
|
2013-01-26 04:36:40 +03:00
|
|
|
// Create a default option for component
|
|
|
|
vjs.Component.prototype.options_ = {
|
|
|
|
'example': {
|
2013-01-16 19:09:56 +03:00
|
|
|
'childOne': { 'foo': 'bar', 'asdf': 'fdsa' },
|
|
|
|
'childTwo': {},
|
|
|
|
'childThree': {}
|
|
|
|
}
|
2013-02-09 01:14:36 +03:00
|
|
|
};
|
2013-01-16 19:09:56 +03:00
|
|
|
|
2013-01-26 04:36:40 +03:00
|
|
|
var comp = new vjs.Component(getFakePlayer(), {
|
|
|
|
'example': {
|
2013-01-16 19:09:56 +03:00
|
|
|
'childOne': { 'foo': 'baz', 'abc': '123' },
|
|
|
|
'childThree': null,
|
|
|
|
'childFour': {}
|
|
|
|
}
|
2013-01-26 04:36:40 +03:00
|
|
|
});
|
2013-01-16 19:09:56 +03:00
|
|
|
|
2013-01-26 04:36:40 +03:00
|
|
|
var mergedOptions = comp.options();
|
|
|
|
var children = mergedOptions['example'];
|
2013-01-16 19:09:56 +03:00
|
|
|
|
|
|
|
ok(children['childOne']['foo'] === 'baz', 'value three levels deep overridden');
|
|
|
|
ok(children['childOne']['asdf'] === 'fdsa', 'value three levels deep maintained');
|
|
|
|
ok(children['childOne']['abc'] === '123', 'value three levels deep added');
|
|
|
|
ok(children['childTwo'], 'object two levels deep maintained');
|
|
|
|
ok(children['childThree'] === null, 'object two levels deep removed');
|
|
|
|
ok(children['childFour'], 'object two levels deep added');
|
2013-01-26 04:36:40 +03:00
|
|
|
|
|
|
|
ok(vjs.Component.prototype.options_['example']['childOne']['foo'] === 'bar', 'prototype options were not overridden');
|
|
|
|
|
|
|
|
// Reset default component options to none
|
|
|
|
vjs.Component.prototype.options_ = null;
|
2013-01-16 19:09:56 +03:00
|
|
|
});
|
|
|
|
|
2013-01-11 00:06:12 +03:00
|
|
|
test('should dispose of component and children', function(){
|
2013-01-18 04:33:53 +03:00
|
|
|
var comp = new vjs.Component(getFakePlayer());
|
2012-12-31 08:45:50 +03:00
|
|
|
|
2013-01-11 00:06:12 +03:00
|
|
|
// Add a child
|
2013-02-09 01:14:36 +03:00
|
|
|
var child = comp.addChild('Component');
|
2013-01-11 00:06:12 +03:00
|
|
|
ok(comp.children().length === 1);
|
2012-12-31 08:45:50 +03:00
|
|
|
|
2013-01-11 00:06:12 +03:00
|
|
|
// Add a listener
|
|
|
|
comp.on('click', function(){ return true; });
|
|
|
|
var data = vjs.getData(comp.el());
|
|
|
|
var id = comp.el()[vjs.expando];
|
2012-12-11 03:40:12 +03:00
|
|
|
|
2013-07-19 00:39:14 +03:00
|
|
|
var hasDisposed = false;
|
2014-02-07 04:11:33 +03:00
|
|
|
var bubbles = null;
|
|
|
|
comp.on('dispose', function(event){
|
|
|
|
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
|
|
|
|
2013-07-19 00:39:14 +03:00
|
|
|
ok(hasDisposed, 'component fired dispose event');
|
2014-02-07 04:11:33 +03:00
|
|
|
ok(bubbles === false, 'dispose event does not bubble');
|
2013-01-11 00:06:12 +03:00
|
|
|
ok(!comp.children(), 'component children were deleted');
|
|
|
|
ok(!comp.el(), 'component element was deleted');
|
|
|
|
ok(!child.children(), 'child children were deleted');
|
|
|
|
ok(!child.el(), 'child element was deleted');
|
2013-02-09 01:14:36 +03:00
|
|
|
ok(!vjs.cache[id], 'listener cache nulled');
|
|
|
|
ok(vjs.isEmpty(data), 'original listener cache object was emptied');
|
2012-12-11 03:40:12 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
test('should add and remove event listeners to element', function(){
|
2013-01-18 04:33:53 +03:00
|
|
|
var comp = new vjs.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.
|
2013-01-11 00:06:12 +03:00
|
|
|
expect(2);
|
2012-12-11 03:40:12 +03:00
|
|
|
|
|
|
|
var testListener = function(){
|
|
|
|
ok(true, 'fired event once');
|
2013-01-11 00:06:12 +03:00
|
|
|
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');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should trigger a listener once using one()', function(){
|
2013-01-18 04:33:53 +03:00
|
|
|
var comp = new vjs.Component(getFakePlayer(), {});
|
2012-12-11 03:40:12 +03:00
|
|
|
|
|
|
|
expect(1);
|
|
|
|
|
|
|
|
var testListener = function(){
|
|
|
|
ok(true, 'fired event once');
|
|
|
|
};
|
|
|
|
|
|
|
|
comp.one('test-event', testListener);
|
|
|
|
comp.trigger('test-event');
|
|
|
|
comp.trigger('test-event');
|
2012-12-31 08:45:50 +03:00
|
|
|
});
|
2013-01-11 00:06:12 +03:00
|
|
|
|
|
|
|
test('should trigger a listener when ready', function(){
|
|
|
|
expect(2);
|
|
|
|
|
|
|
|
var optionsReadyListener = function(){
|
2013-02-09 01:14:36 +03:00
|
|
|
ok(true, 'options listener fired');
|
2013-01-11 00:06:12 +03:00
|
|
|
};
|
|
|
|
var methodReadyListener = function(){
|
2013-02-09 01:14:36 +03:00
|
|
|
ok(true, 'ready method listener fired');
|
2013-01-11 00:06:12 +03:00
|
|
|
};
|
|
|
|
|
2013-01-18 04:33:53 +03:00
|
|
|
var comp = new vjs.Component(getFakePlayer(), {}, optionsReadyListener);
|
2013-01-11 00:06:12 +03:00
|
|
|
|
|
|
|
comp.triggerReady();
|
|
|
|
|
|
|
|
comp.ready(methodReadyListener);
|
|
|
|
|
|
|
|
// First two listeners should only be fired once and then removed
|
|
|
|
comp.triggerReady();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should add and remove a CSS class', function(){
|
2013-01-18 04:33:53 +03:00
|
|
|
var comp = new vjs.Component(getFakePlayer(), {});
|
2013-01-11 00:06:12 +03:00
|
|
|
|
|
|
|
comp.addClass('test-class');
|
|
|
|
ok(comp.el().className.indexOf('test-class') !== -1);
|
|
|
|
comp.removeClass('test-class');
|
|
|
|
ok(comp.el().className.indexOf('test-class') === -1);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should show and hide an element', function(){
|
2013-01-18 04:33:53 +03:00
|
|
|
var comp = new vjs.Component(getFakePlayer(), {});
|
2013-01-11 00:06:12 +03:00
|
|
|
|
|
|
|
comp.hide();
|
|
|
|
ok(comp.el().style.display === 'none');
|
|
|
|
comp.show();
|
|
|
|
ok(comp.el().style.display === 'block');
|
|
|
|
});
|
|
|
|
|
2014-08-26 01:41:33 +03:00
|
|
|
test('dimension() should ignore undefined, null, and NaN values', function() {
|
|
|
|
var comp, width, height, newWidth, newHeight;
|
|
|
|
width = 300;
|
|
|
|
height = 150;
|
|
|
|
|
|
|
|
comp = new vjs.Component(getFakePlayer(), {}),
|
|
|
|
// set component dimension
|
|
|
|
|
|
|
|
comp.dimensions(width, height);
|
|
|
|
|
|
|
|
newWidth = comp.dimension('width', null);
|
|
|
|
|
|
|
|
equal(newWidth, width, 'we did not set the width with null');
|
|
|
|
|
|
|
|
newHeight = comp.dimension('height', NaN);
|
|
|
|
|
|
|
|
equal(newHeight, height, 'we did not set the height with NaN');
|
|
|
|
|
|
|
|
newWidth = comp.dimension('width', undefined);
|
|
|
|
|
|
|
|
equal(newWidth, width, 'we did not set the width with undefined');
|
|
|
|
});
|
|
|
|
|
2013-01-11 00:06:12 +03:00
|
|
|
test('should change the width and height of a component', function(){
|
|
|
|
var container = document.createElement('div');
|
2013-01-18 04:33:53 +03:00
|
|
|
var comp = new vjs.Component(getFakePlayer(), {});
|
2013-01-11 00:06:12 +03:00
|
|
|
var el = comp.el();
|
|
|
|
var fixture = document.getElementById('qunit-fixture');
|
|
|
|
|
|
|
|
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');
|
|
|
|
|
|
|
|
ok(comp.width() === 500, 'percent values working');
|
2013-04-13 02:51:04 +03:00
|
|
|
var compStyle = vjs.getComputedDimension(el, 'width');
|
|
|
|
ok(compStyle === comp.width() + 'px', 'matches computed style');
|
2013-01-11 00:06:12 +03:00
|
|
|
ok(comp.height() === 123, 'px values working');
|
|
|
|
|
|
|
|
comp.width(321);
|
|
|
|
ok(comp.width() === 321, 'integer values working');
|
2013-03-09 11:39:28 +03:00
|
|
|
|
|
|
|
comp.width('auto');
|
|
|
|
comp.height('auto');
|
|
|
|
ok(comp.width() === 1000, 'forced width was removed');
|
|
|
|
ok(comp.height() === 0, 'forced height was removed');
|
2013-01-11 00:06:12 +03:00
|
|
|
});
|
2013-05-01 03:27:36 +03:00
|
|
|
|
|
|
|
|
|
|
|
test('should use a defined content el for appending children', function(){
|
|
|
|
var CompWithContent = vjs.Component.extend();
|
|
|
|
CompWithContent.prototype.createEl = function(){
|
|
|
|
// Create the main componenent element
|
|
|
|
var el = vjs.createEl('div');
|
|
|
|
// Create the element where children will be appended
|
|
|
|
this.contentEl_ = vjs.createEl('div', { 'id': 'contentEl' });
|
|
|
|
el.appendChild(this.contentEl_);
|
|
|
|
return el;
|
|
|
|
};
|
|
|
|
|
|
|
|
var comp = new CompWithContent(getFakePlayer());
|
|
|
|
var child = comp.addChild('component');
|
|
|
|
|
|
|
|
ok(comp.children().length === 1);
|
|
|
|
ok(comp.el().childNodes[0]['id'] === 'contentEl');
|
|
|
|
ok(comp.el().childNodes[0].childNodes[0] === child.el());
|
|
|
|
|
|
|
|
comp.removeChild(child);
|
|
|
|
|
|
|
|
ok(comp.children().length === 0, 'Length should now be zero');
|
|
|
|
ok(comp.el().childNodes[0]['id'] === 'contentEl', 'Content El should still exist');
|
|
|
|
ok(comp.el().childNodes[0].childNodes[0] !== child.el(), 'Child el should be removed.');
|
|
|
|
});
|
2013-08-10 00:29:22 +03:00
|
|
|
|
|
|
|
test('should emit a tap event', function(){
|
2014-05-07 03:22:29 +03:00
|
|
|
expect(2);
|
2013-08-10 00:29:22 +03:00
|
|
|
|
|
|
|
// Fake touch support. Real touch support isn't needed for this test.
|
|
|
|
var origTouch = vjs.TOUCH_ENABLED;
|
|
|
|
vjs.TOUCH_ENABLED = true;
|
|
|
|
|
2014-02-07 03:58:25 +03:00
|
|
|
var comp = new vjs.Component(getFakePlayer());
|
2014-02-06 04:58:30 +03:00
|
|
|
|
2013-08-10 00:29:22 +03:00
|
|
|
comp.emitTapEvents();
|
|
|
|
comp.on('tap', function(){
|
|
|
|
ok(true, 'Tap event emitted');
|
|
|
|
});
|
2014-05-07 03:22:29 +03:00
|
|
|
|
|
|
|
// A touchstart followed by touchend should trigger a tap
|
|
|
|
vjs.trigger(comp.el(), {type: 'touchstart', touches: [{}]});
|
|
|
|
comp.trigger('touchend');
|
|
|
|
|
|
|
|
// A touchmove with a lot of movement should not trigger a tap
|
|
|
|
vjs.trigger(comp.el(), {type: 'touchstart', touches: [
|
|
|
|
{ pageX: 0, pageY: 0 }
|
|
|
|
]});
|
|
|
|
vjs.trigger(comp.el(), {type: 'touchmove', touches: [
|
|
|
|
{ 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
|
|
|
|
vjs.trigger(comp.el(), {type: 'touchstart', touches: [
|
|
|
|
{ pageX: 0, pageY: 0 }
|
|
|
|
]});
|
|
|
|
vjs.trigger(comp.el(), {type: 'touchmove', touches: [
|
|
|
|
{ pageX: 10, pageY: 10 }
|
|
|
|
]});
|
2013-08-10 00:29:22 +03:00
|
|
|
comp.trigger('touchend');
|
|
|
|
|
|
|
|
// Reset to orignial value
|
|
|
|
vjs.TOUCH_ENABLED = origTouch;
|
|
|
|
});
|