2015-10-28 19:28:15 +02:00
|
|
|
import * as Dom from '../../src/js/utils/dom';
|
2015-03-26 06:43:41 +02:00
|
|
|
import Player from '../../src/js/player.js';
|
|
|
|
import document from 'global/document';
|
|
|
|
|
2016-08-03 21:27:03 +02:00
|
|
|
const TestHelpers = {
|
|
|
|
makeTag() {
|
|
|
|
const videoTag = document.createElement('video');
|
|
|
|
|
2013-06-24 22:47:47 +03:00
|
|
|
videoTag.id = 'example_1';
|
2018-10-10 21:30:20 +02:00
|
|
|
videoTag.className = 'video-js';
|
2013-06-24 22:47:47 +03:00
|
|
|
return videoTag;
|
|
|
|
},
|
2015-03-26 06:43:41 +02:00
|
|
|
|
2019-04-23 19:42:52 +02:00
|
|
|
makePlayer(playerOptions, videoTag) {
|
2015-03-26 06:43:41 +02:00
|
|
|
videoTag = videoTag || TestHelpers.makeTag();
|
2013-06-24 22:47:47 +03:00
|
|
|
|
2016-08-03 21:27:03 +02:00
|
|
|
const fixture = document.getElementById('qunit-fixture');
|
|
|
|
|
2013-06-24 22:47:47 +03:00
|
|
|
fixture.appendChild(videoTag);
|
|
|
|
|
|
|
|
playerOptions = playerOptions || {};
|
2016-08-03 21:27:03 +02:00
|
|
|
playerOptions.techOrder = playerOptions.techOrder || ['techFaker'];
|
2013-06-24 22:47:47 +03:00
|
|
|
|
2019-04-23 19:42:52 +02:00
|
|
|
return new Player(videoTag, playerOptions);
|
2015-03-26 06:43:41 +02:00
|
|
|
},
|
2014-10-16 22:59:41 +03:00
|
|
|
|
2016-08-03 21:27:03 +02:00
|
|
|
getComputedStyle(el, rule) {
|
2015-05-17 00:59:46 +02:00
|
|
|
if (document.defaultView && document.defaultView.getComputedStyle) {
|
|
|
|
return document.defaultView.getComputedStyle(el, null).getPropertyValue(rule);
|
|
|
|
}
|
2014-10-16 22:59:41 +03:00
|
|
|
|
2018-03-23 19:25:12 +02:00
|
|
|
return '';
|
2015-10-28 19:28:15 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Runs a range of assertions on a DOM element.
|
|
|
|
*
|
|
|
|
* @param {QUnit.Assert} assert
|
|
|
|
* @param {Element} el
|
|
|
|
* @param {Object} spec
|
|
|
|
* An object from which assertions are generated.
|
|
|
|
*
|
|
|
|
* @param {Object} [spec.attrs]
|
|
|
|
* An object mapping attribute names (keys) to strict values.
|
|
|
|
*
|
|
|
|
* @param {Array} [spec.classes]
|
|
|
|
* An array of classes that are expected on the element.
|
|
|
|
*
|
2018-09-28 20:58:15 +02:00
|
|
|
* @param {string} [spec.innerHTML]
|
2015-10-28 19:28:15 +02:00
|
|
|
* A string of text/html that is expected as the content of element.
|
|
|
|
* Both values will be trimmed, but remains case-sensitive.
|
|
|
|
*
|
|
|
|
* @param {Object} [spec.props]
|
|
|
|
* An object mapping property names (keys) to strict values.
|
|
|
|
*
|
2018-09-28 20:58:15 +02:00
|
|
|
* @param {string} [spec.tagName]
|
2015-10-28 19:28:15 +02:00
|
|
|
* A string (case-insensitive) representing that element's tagName.
|
|
|
|
*
|
|
|
|
* @return {Function}
|
|
|
|
* Invoke the returned function to run the assertions. This
|
|
|
|
* function has a `count` property which can be used to
|
|
|
|
* reference how many assertions will be run (e.g. for use
|
|
|
|
* with `assert.expect()`).
|
|
|
|
*/
|
2016-08-03 21:27:03 +02:00
|
|
|
assertEl(assert, el, spec) {
|
|
|
|
const attrs = spec.attrs ? Object.keys(spec.attrs) : [];
|
|
|
|
const classes = spec.classes || [];
|
|
|
|
const innerHTML = spec.innerHTML ? spec.innerHTML.trim() : '';
|
|
|
|
const props = spec.props ? Object.keys(spec.props) : [];
|
|
|
|
const tagName = spec.tagName ? spec.tagName.toLowerCase() : '';
|
2015-10-28 19:28:15 +02:00
|
|
|
|
|
|
|
// Return value is a function, which runs through all the combined
|
|
|
|
// assertions. This is done so that the count can be attached dynamically
|
|
|
|
// and run whenever desired.
|
2016-08-03 21:27:03 +02:00
|
|
|
const run = () => {
|
2015-10-28 19:28:15 +02:00
|
|
|
if (tagName) {
|
2016-08-03 21:27:03 +02:00
|
|
|
const elTagName = el.tagName.toLowerCase();
|
|
|
|
const msg = `el should have been a <${tagName}> and was a <${elTagName}>`;
|
|
|
|
|
2015-10-28 19:28:15 +02:00
|
|
|
assert.strictEqual(elTagName, tagName, msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (innerHTML) {
|
2016-08-03 21:27:03 +02:00
|
|
|
const elInnerHTML = el.innerHTML.trim();
|
|
|
|
const msg = 'el should have expected HTML content';
|
|
|
|
|
2015-10-28 19:28:15 +02:00
|
|
|
assert.strictEqual(elInnerHTML, innerHTML, msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
attrs.forEach(a => {
|
2016-08-03 21:27:03 +02:00
|
|
|
const actual = el.getAttribute(a);
|
|
|
|
const expected = spec.attrs[a];
|
|
|
|
const msg = `el should have the "${a}" attribute with ` +
|
|
|
|
`the value "${expected}" and it was "${actual}"`;
|
|
|
|
|
2015-10-28 19:28:15 +02:00
|
|
|
assert.strictEqual(actual, expected, msg);
|
|
|
|
});
|
|
|
|
|
|
|
|
classes.forEach(c => {
|
2016-08-03 21:27:03 +02:00
|
|
|
const msg = `el should have the "${c}" class in its ` +
|
|
|
|
`className, which is "${el.className}"`;
|
|
|
|
|
2017-01-19 23:01:56 +02:00
|
|
|
assert.ok(Dom.hasClass(el, c), msg);
|
2015-10-28 19:28:15 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
props.forEach(p => {
|
2016-08-03 21:27:03 +02:00
|
|
|
const actual = el[p];
|
|
|
|
const expected = spec.props[p];
|
|
|
|
const msg = `el should have the "${p}" property with the ` +
|
|
|
|
`value "${expected}" and it was "${actual}"`;
|
|
|
|
|
2015-10-28 19:28:15 +02:00
|
|
|
assert.strictEqual(actual, expected, msg);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
// Include the number of assertions to run, so it can be used to set
|
|
|
|
// expectations (via `assert.expect()`).
|
2016-08-03 21:27:03 +02:00
|
|
|
run.count = Number(!!tagName) +
|
2015-10-28 19:28:15 +02:00
|
|
|
Number(!!innerHTML) +
|
|
|
|
classes.length +
|
|
|
|
attrs.length +
|
|
|
|
props.length;
|
|
|
|
|
|
|
|
return run;
|
2016-11-23 20:54:48 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Triggers an event on a DOM node natively.
|
|
|
|
*
|
|
|
|
* @param {Element} element
|
|
|
|
* @param {string} eventType
|
|
|
|
*/
|
|
|
|
triggerDomEvent(element, eventType) {
|
2016-12-02 21:17:36 +02:00
|
|
|
let event;
|
|
|
|
|
|
|
|
if (document.createEvent) {
|
|
|
|
event = document.createEvent('HTMLEvents');
|
|
|
|
event.initEvent(eventType, true, true);
|
|
|
|
} else {
|
|
|
|
event = document.createEventObject();
|
|
|
|
event.eventType = eventType;
|
|
|
|
}
|
|
|
|
|
|
|
|
event.eventName = eventType;
|
|
|
|
|
|
|
|
if (document.createEvent) {
|
|
|
|
element.dispatchEvent(event);
|
|
|
|
} else {
|
|
|
|
element.fireEvent('on' + event.eventType, event);
|
2016-11-23 20:54:48 +02:00
|
|
|
}
|
2014-10-16 22:59:41 +03:00
|
|
|
}
|
|
|
|
};
|
2015-03-26 06:43:41 +02:00
|
|
|
|
2015-05-04 01:12:38 +02:00
|
|
|
export default TestHelpers;
|