1
0
mirror of https://github.com/videojs/video.js.git synced 2025-01-10 23:30:03 +02:00
video.js/test/unit/resize-manager.test.js
Gary Katsevman 9ceb4e4fe0
feat: playerresize event in all cases (#4864)
Use ResizeObserver when available for better and more performant resizing information, otherwise, fall back to a throttled resize event on an iframe that's the size of the player.
Allows a video.js user to disable this by setting resizeManager: false as an option since the component will not be initialized.

Add a debounce util.

This reverts #4800 (e0ed0b5) because we end up getting two playerresize events with the dimension methods now.
2018-01-30 13:26:21 -05:00

103 lines
2.4 KiB
JavaScript

/* eslint-env qunit */
import ResizeManager from '../../src/js/resize-manager.js';
import TestHelpers from './test-helpers.js';
import * as browser from '../../src/js/utils/browser.js';
import sinon from 'sinon';
if (!browser.IS_IE8) {
QUnit.module('ResizeManager', {
beforeEach() {
this.clock = sinon.useFakeTimers();
this.player = TestHelpers.makePlayer();
},
afterEach() {
this.player.dispose();
this.clock.restore();
}
});
QUnit.test('ResizeManager creates an iframe if ResizeObserver is not available', function(assert) {
const rm = new ResizeManager(this.player, {ResizeObserver: null});
assert.equal(rm.el().tagName.toLowerCase(), 'iframe', 'we got an iframe');
rm.dispose();
});
QUnit.test('ResizeManager uses the ResizeObserver, if given', function(assert) {
let roCreated = false;
let observeCalled = false;
let unobserveCalled = false;
let disconnectCalled = false;
let sameEl = false;
class MyResizeObserver {
constructor(fn) {
roCreated = true;
}
observe(el) {
observeCalled = true;
this.el = el;
}
unobserve(el) {
unobserveCalled = true;
sameEl = this.el === el;
}
disconnect() {
disconnectCalled = true;
}
}
const rm = new ResizeManager(this.player, {ResizeObserver: MyResizeObserver});
assert.ok(roCreated, 'we intantiated the RO that was passed');
assert.ok(observeCalled, 'we observed the RO');
assert.equal(rm.resizeObserver_.el, this.player.el(), 'we observed the player el');
rm.dispose();
assert.ok(unobserveCalled, 'we unobserve when disposed');
assert.ok(sameEl, 'we unobserve the same el as we observed');
assert.ok(disconnectCalled, 'we disconnected when disposed');
});
QUnit.test('ResizeManager triggers `playerresize` when the observer method is called', function(assert) {
let observer;
class MyResizeObserver {
constructor(fn) {
observer = fn;
}
observe(el) {
this.el = el;
}
unobserve(el) {
}
disconnect() {
}
}
let playerresizeCalled = 0;
const rm = new ResizeManager(this.player, {ResizeObserver: MyResizeObserver});
this.player.on('playerresize', function() {
playerresizeCalled++;
});
observer();
this.clock.tick(100);
assert.equal(playerresizeCalled, 1, 'playerresize was triggered');
rm.dispose();
});
}