Skip to content

Commit

Permalink
Fix: Add UT for Fullscreen.js fullscreenchange
Browse files Browse the repository at this point in the history
Slight refactor of Fullscreen.js to have separate methods for binding and unbinding events for testing purposes as spies werent working. Also added destroy function as events werent being unbound.
  • Loading branch information
DanDeMicco committed Jan 16, 2018
1 parent 871a6b4 commit 28a0474
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
33 changes: 33 additions & 0 deletions src/lib/Fullscreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,44 @@ class Fullscreen extends EventEmitter {
constructor() {
super();

this.bindDOMListeners();
}

/**
* Binds DOM listeners for Fullscreen.
*
* @protected
* @return {void}
*/
bindDOMListeners() {
// as of now (1/12/18) fullscreenchange is not universally adopted, fscreen will
// detect and add the appropriate vendor prefixed event
fscreen.addEventListener('fullscreenchange', this.fullscreenchangeHandler);
}

/**
* Binds DOM listeners for Fullscreen.
*
* @protected
* @return {void}
*/
unbindDOMListeners() {
// as of now (1/12/18) fullscreenchange is not universally adopted, fscreen will
// detect and add the appropriate vendor prefixed event
fscreen.removeEventListener('fullscreenchange', this.fullscreenchangeHandler);
}

/**
* [destructor]
*
* @protected
* @return {void}
*/
destroy() {
this.unbindDOMListeners();
this.removeAllListeners();
}

/**
* Returns true if the browser supports fullscreen natively
*
Expand Down
11 changes: 11 additions & 0 deletions src/lib/__tests__/Fullscreen-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,17 @@ describe('lib/Fullscreen', () => {

expect(fullscreen.emit).to.have.been.calledWith('exit');
});

it('should be called only once when the fullscreenchange event is emitted', () => {
const spy = sandbox.spy(fullscreen, 'fullscreenchangeHandler');
//rebind the dom listeners to use the spy
fullscreen.bindDOMListeners();

const event = new Event('webkitfullscreenchange');

window.document.dispatchEvent(event);
expect(spy).to.be.called.once;
});
});

describe('toggle()', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/viewers/BaseViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ class BaseViewer extends EventEmitter {
});
}

fullscreen.removeAllListeners();
fullscreen.destroy();
document.defaultView.removeEventListener('resize', this.debouncedResizeHandler);
this.removeAllListeners();

Expand Down

0 comments on commit 28a0474

Please sign in to comment.