| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647 |
- /**
- * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
- */
- /* globals document, window, Event, MouseEvent */
- import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
- import DomEmitterMixin from '../../src/dom/emittermixin';
- import EmitterMixin from '../../src/emittermixin';
- describe( 'DomEmitterMixin', () => {
- let emitter, domEmitter, node;
- testUtils.createSinonSandbox();
- beforeEach( () => {
- emitter = Object.create( EmitterMixin );
- domEmitter = Object.create( DomEmitterMixin );
- node = document.createElement( 'div' );
- } );
- afterEach( () => {
- domEmitter.stopListening();
- } );
- describe( 'listenTo', () => {
- it( 'should listen to EmitterMixin events', () => {
- const spy = testUtils.sinon.spy();
- domEmitter.listenTo( emitter, 'test', spy );
- emitter.fire( 'test' );
- sinon.assert.calledOnce( spy );
- } );
- it( 'should listen to native DOM events', () => {
- const spy = testUtils.sinon.spy();
- domEmitter.listenTo( node, 'test', spy );
- node.dispatchEvent( new Event( 'test' ) );
- sinon.assert.calledOnce( spy );
- } );
- it( 'should listen to native DOM events - window as source', () => {
- const spy = testUtils.sinon.spy();
- domEmitter.listenTo( window, 'test', spy );
- window.dispatchEvent( new Event( 'test' ) );
- sinon.assert.calledOnce( spy );
- } );
- it( 'should listen to native DOM events - document as source', () => {
- const spy = testUtils.sinon.spy();
- domEmitter.listenTo( document, 'test', spy );
- document.dispatchEvent( new Event( 'test' ) );
- sinon.assert.calledOnce( spy );
- } );
- // #187
- it( 'should work for DOM Nodes belonging to another window', done => {
- const spy = testUtils.sinon.spy();
- const iframe = document.createElement( 'iframe' );
- iframe.addEventListener( 'load', () => {
- const iframeNode = iframe.contentWindow.document.createElement( 'div' );
- domEmitter.listenTo( iframeNode, 'test', spy );
- iframeNode.dispatchEvent( new Event( 'test' ) );
- sinon.assert.calledOnce( spy );
- iframe.remove();
- done();
- } );
- document.body.appendChild( iframe );
- } );
- describe( 'event capturing', () => {
- beforeEach( () => {
- document.body.appendChild( node );
- } );
- afterEach( () => {
- document.body.removeChild( node );
- } );
- it( 'should not use capturing at default', () => {
- const spy = testUtils.sinon.spy();
- domEmitter.listenTo( document, 'test', spy );
- node.dispatchEvent( new Event( 'test', { bubbles: false } ) );
- sinon.assert.notCalled( spy );
- } );
- it( 'should optionally use capturing', () => {
- const spy = testUtils.sinon.spy();
- domEmitter.listenTo( document, 'test', spy, { useCapture: true } );
- node.dispatchEvent( new Event( 'test', { bubbles: false } ) );
- sinon.assert.calledOnce( spy );
- } );
- } );
- describe( 'event passive handling', () => {
- it( 'should not use passive mode by default', () => {
- const spy = sinon.spy( node, 'addEventListener' );
- domEmitter.listenTo( node, 'test', () => {} );
- expect( spy.calledWith( 'test', sinon.match.func, sinon.match( { capture: false, passive: false } ) ) ).to.be.true;
- } );
- it( 'should optionally use passive mode', () => {
- const spy = sinon.spy( node, 'addEventListener' );
- domEmitter.listenTo( node, 'test', () => {}, { usePassive: true } );
- expect( spy.calledWith( 'test', sinon.match.func, sinon.match( { capture: false, passive: true } ) ) ).to.be.true;
- } );
- it( 'should not get activated for event capturing (if not desired)', () => {
- const spy = sinon.spy( node, 'addEventListener' );
- domEmitter.listenTo( node, 'test', () => {}, { useCapture: true } );
- expect( spy.calledWith( 'test', sinon.match.func, sinon.match( { capture: true, passive: false } ) ) ).to.be.true;
- } );
- } );
- } );
- describe( 'stopListening', () => {
- it( 'should stop listening to EmitterMixin events, specific event', () => {
- const spy1 = testUtils.sinon.spy();
- const spy2 = testUtils.sinon.spy();
- domEmitter.listenTo( emitter, 'foo', spy1 );
- domEmitter.listenTo( emitter, 'foo:bar', spy2 );
- emitter.fire( 'foo:bar' );
- sinon.assert.calledOnce( spy1 );
- sinon.assert.calledOnce( spy2 );
- domEmitter.stopListening( emitter, 'foo' );
- emitter.fire( 'foo:bar' );
- sinon.assert.calledOnce( spy1 );
- sinon.assert.calledTwice( spy2 );
- } );
- it( 'should stop listening to EmitterMixin events, specific emitter', () => {
- const spy1 = testUtils.sinon.spy();
- const spy2 = testUtils.sinon.spy();
- domEmitter.listenTo( emitter, 'foo', spy1 );
- domEmitter.listenTo( emitter, 'foo:bar', spy2 );
- emitter.fire( 'foo:bar' );
- sinon.assert.calledOnce( spy1 );
- sinon.assert.calledOnce( spy2 );
- domEmitter.stopListening( emitter );
- emitter.fire( 'foo:bar' );
- sinon.assert.calledOnce( spy1 );
- sinon.assert.calledOnce( spy2 );
- } );
- it( 'should stop listening to a specific event callback', () => {
- const spy1 = testUtils.sinon.spy();
- const spy2 = testUtils.sinon.spy();
- domEmitter.listenTo( node, 'event1', spy1 );
- domEmitter.listenTo( node, 'event2', spy2 );
- node.dispatchEvent( new Event( 'event1' ) );
- node.dispatchEvent( new Event( 'event2' ) );
- domEmitter.stopListening( node, 'event1', spy1 );
- node.dispatchEvent( new Event( 'event1' ) );
- node.dispatchEvent( new Event( 'event2' ) );
- sinon.assert.calledOnce( spy1 );
- sinon.assert.calledTwice( spy2 );
- } );
- it( 'should stop listening to a specific event callback (only from the given node)', () => {
- const spy1 = testUtils.sinon.spy();
- const spy2 = testUtils.sinon.spy();
- const node1 = document.createElement( 'div' );
- const node2 = document.createElement( 'div' );
- domEmitter.listenTo( node1, 'event1', spy1 );
- domEmitter.listenTo( node1, 'event2', spy2 );
- domEmitter.listenTo( node2, 'event1', spy1 );
- node1.dispatchEvent( new Event( 'event1' ) );
- node1.dispatchEvent( new Event( 'event2' ) );
- node2.dispatchEvent( new Event( 'event1' ) );
- sinon.assert.calledTwice( spy1 );
- sinon.assert.calledOnce( spy2 );
- domEmitter.stopListening( node1, 'event1', spy1 );
- node1.dispatchEvent( new Event( 'event1' ) );
- node1.dispatchEvent( new Event( 'event2' ) );
- node2.dispatchEvent( new Event( 'event1' ) );
- sinon.assert.calledThrice( spy1 );
- sinon.assert.calledTwice( spy2 );
- } );
- it( 'should stop listening to a specific event callback (re–listen)', () => {
- const spy = testUtils.sinon.spy();
- domEmitter.listenTo( node, 'event', spy );
- node.dispatchEvent( new Event( 'event' ) );
- domEmitter.stopListening( node, 'event', spy );
- domEmitter.listenTo( node, 'event', spy );
- node.dispatchEvent( new Event( 'event' ) );
- sinon.assert.calledTwice( spy );
- } );
- it( 'should stop listening to an specific event', () => {
- const spy1a = testUtils.sinon.spy();
- const spy1b = testUtils.sinon.spy();
- const spy2 = testUtils.sinon.spy();
- domEmitter.listenTo( node, 'event1', spy1a );
- domEmitter.listenTo( node, 'event1', spy1b );
- domEmitter.listenTo( node, 'event2', spy2 );
- node.dispatchEvent( new Event( 'event1' ) );
- node.dispatchEvent( new Event( 'event2' ) );
- sinon.assert.calledOnce( spy1a );
- sinon.assert.calledOnce( spy1b );
- sinon.assert.calledOnce( spy2 );
- domEmitter.stopListening( node, 'event1' );
- node.dispatchEvent( new Event( 'event1' ) );
- node.dispatchEvent( new Event( 'event2' ) );
- sinon.assert.calledOnce( spy1a );
- sinon.assert.calledOnce( spy1b );
- sinon.assert.calledTwice( spy2 );
- } );
- it( 'should stop listening to all events from a specific node', () => {
- const spy1 = testUtils.sinon.spy();
- const spy2 = testUtils.sinon.spy();
- domEmitter.listenTo( node, 'event1', spy1 );
- domEmitter.listenTo( node, 'event2', spy2 );
- node.dispatchEvent( new Event( 'event1' ) );
- node.dispatchEvent( new Event( 'event2' ) );
- domEmitter.stopListening( node );
- node.dispatchEvent( new Event( 'event1' ) );
- node.dispatchEvent( new Event( 'event2' ) );
- sinon.assert.calledOnce( spy1 );
- sinon.assert.calledOnce( spy2 );
- } );
- it( 'should stop listening to all events from a specific node (only that node)', () => {
- const spy1 = testUtils.sinon.spy();
- const spy2 = testUtils.sinon.spy();
- const node1 = document.createElement( 'div' );
- const node2 = document.createElement( 'div' );
- domEmitter.listenTo( node1, 'event', spy1 );
- domEmitter.listenTo( node2, 'event', spy2 );
- node1.dispatchEvent( new Event( 'event' ) );
- node2.dispatchEvent( new Event( 'event' ) );
- domEmitter.stopListening( node1 );
- node1.dispatchEvent( new Event( 'event' ) );
- node2.dispatchEvent( new Event( 'event' ) );
- sinon.assert.calledOnce( spy1 );
- sinon.assert.calledTwice( spy2 );
- } );
- it( 'should stop listening to everything', () => {
- const spy1 = testUtils.sinon.spy();
- const spy2 = testUtils.sinon.spy();
- const spy3 = testUtils.sinon.spy();
- const node1 = document.createElement( 'div' );
- const node2 = document.createElement( 'div' );
- domEmitter.listenTo( node1, 'event1', spy1 );
- domEmitter.listenTo( node2, 'event2', spy2 );
- domEmitter.listenTo( emitter, 'event3', spy3 );
- node1.dispatchEvent( new Event( 'event1' ) );
- node2.dispatchEvent( new Event( 'event2' ) );
- emitter.fire( 'event3' );
- domEmitter.stopListening();
- node1.dispatchEvent( new Event( 'event1' ) );
- node2.dispatchEvent( new Event( 'event2' ) );
- emitter.fire( 'event3' );
- sinon.assert.calledOnce( spy1 );
- sinon.assert.calledOnce( spy2 );
- sinon.assert.calledOnce( spy3 );
- } );
- it( 'should stop listening to everything what left', () => {
- const spy1 = testUtils.sinon.spy();
- const spy2 = testUtils.sinon.spy();
- const spy3 = testUtils.sinon.spy();
- const spy4 = testUtils.sinon.spy();
- const node1 = document.createElement( 'div' );
- const node2 = document.createElement( 'div' );
- domEmitter.listenTo( node1, 'event1', spy1 );
- domEmitter.listenTo( node1, 'event2', spy2 );
- domEmitter.listenTo( node2, 'event1', spy3 );
- domEmitter.listenTo( node2, 'event2', spy4 );
- domEmitter.stopListening( node1, 'event1', spy1 );
- domEmitter.stopListening( node2, 'event1' );
- node1.dispatchEvent( new Event( 'event1' ) );
- node1.dispatchEvent( new Event( 'event2' ) );
- node2.dispatchEvent( new Event( 'event1' ) );
- node2.dispatchEvent( new Event( 'event2' ) );
- sinon.assert.notCalled( spy1 );
- sinon.assert.calledOnce( spy2 );
- sinon.assert.notCalled( spy3 );
- sinon.assert.calledOnce( spy4 );
- domEmitter.stopListening();
- node1.dispatchEvent( new Event( 'event1' ) );
- node1.dispatchEvent( new Event( 'event2' ) );
- node2.dispatchEvent( new Event( 'event1' ) );
- node2.dispatchEvent( new Event( 'event2' ) );
- sinon.assert.notCalled( spy1 );
- sinon.assert.calledOnce( spy2 );
- sinon.assert.notCalled( spy3 );
- sinon.assert.calledOnce( spy4 );
- } );
- it( 'should not stop other nodes when a non-listened node is provided', () => {
- const spy = testUtils.sinon.spy();
- const node1 = document.createElement( 'div' );
- const node2 = document.createElement( 'div' );
- domEmitter.listenTo( node1, 'test', spy );
- domEmitter.stopListening( node2 );
- node1.dispatchEvent( new Event( 'test' ) );
- sinon.assert.called( spy );
- } );
- it( 'should pass DOM Event data to the listener', () => {
- const spy = testUtils.sinon.spy();
- domEmitter.listenTo( node, 'click', spy );
- const mouseEvent = new MouseEvent( 'click', {
- screenX: 10,
- screenY: 20
- } );
- node.dispatchEvent( mouseEvent );
- sinon.assert.calledOnce( spy );
- expect( spy.args[ 0 ][ 1 ] ).to.equal( mouseEvent );
- } );
- it( 'should detach native DOM event listener proxy, specific event', () => {
- const spy1a = testUtils.sinon.spy();
- const spy1b = testUtils.sinon.spy();
- domEmitter.listenTo( node, 'test', spy1a );
- const proxyEmitter = domEmitter._getProxyEmitter( node );
- const spy2 = testUtils.sinon.spy( proxyEmitter, 'fire' );
- node.dispatchEvent( new Event( 'test' ) );
- sinon.assert.calledOnce( spy1a );
- sinon.assert.calledOnce( spy2 );
- domEmitter.stopListening( node, 'test' );
- node.dispatchEvent( new Event( 'test' ) );
- sinon.assert.calledOnce( spy1a );
- sinon.assert.calledOnce( spy2 );
- // Attach same event again.
- domEmitter.listenTo( node, 'test', spy1b );
- node.dispatchEvent( new Event( 'test' ) );
- expect( proxyEmitter ).to.equal( domEmitter._getProxyEmitter( node ) );
- sinon.assert.calledOnce( spy1a );
- sinon.assert.calledOnce( spy1b );
- sinon.assert.calledTwice( spy2 );
- } );
- it( 'should detach native DOM event listener proxy, specific callback', () => {
- const spy1a = testUtils.sinon.spy();
- const spy1b = testUtils.sinon.spy();
- const spy1c = testUtils.sinon.spy();
- domEmitter.listenTo( node, 'test', spy1a );
- domEmitter.listenTo( node, 'test', spy1b );
- const proxyEmitter = domEmitter._getProxyEmitter( node );
- const spy2 = testUtils.sinon.spy( proxyEmitter, 'fire' );
- node.dispatchEvent( new Event( 'test' ) );
- sinon.assert.calledOnce( spy1a );
- sinon.assert.calledOnce( spy1b );
- sinon.assert.calledOnce( spy2 );
- domEmitter.stopListening( node, 'test', spy1a );
- node.dispatchEvent( new Event( 'test' ) );
- sinon.assert.calledOnce( spy1a );
- sinon.assert.calledTwice( spy1b );
- sinon.assert.calledTwice( spy2 );
- domEmitter.stopListening( node, 'test', spy1b );
- node.dispatchEvent( new Event( 'test' ) );
- sinon.assert.calledOnce( spy1a );
- sinon.assert.calledTwice( spy1b );
- sinon.assert.calledTwice( spy2 );
- // Attach same event again.
- domEmitter.listenTo( node, 'test', spy1c );
- node.dispatchEvent( new Event( 'test' ) );
- expect( proxyEmitter ).to.equal( domEmitter._getProxyEmitter( node ) );
- sinon.assert.calledOnce( spy1a );
- sinon.assert.calledTwice( spy1b );
- sinon.assert.calledOnce( spy1c );
- sinon.assert.calledThrice( spy2 );
- } );
- it( 'should detach native DOM event listener proxy, specific emitter', () => {
- const spy1a = testUtils.sinon.spy();
- const spy1b = testUtils.sinon.spy();
- const spy1c = testUtils.sinon.spy();
- const spy1d = testUtils.sinon.spy();
- domEmitter.listenTo( node, 'test1', spy1a );
- domEmitter.listenTo( node, 'test2', spy1b );
- const proxyEmitter = domEmitter._getProxyEmitter( node );
- const spy2 = testUtils.sinon.spy( proxyEmitter, 'fire' );
- node.dispatchEvent( new Event( 'test1' ) );
- node.dispatchEvent( new Event( 'test2' ) );
- sinon.assert.calledOnce( spy1a );
- sinon.assert.calledOnce( spy1b );
- sinon.assert.calledTwice( spy2 );
- domEmitter.stopListening( node );
- node.dispatchEvent( new Event( 'test1' ) );
- node.dispatchEvent( new Event( 'test2' ) );
- sinon.assert.calledOnce( spy1a );
- sinon.assert.calledOnce( spy1b );
- sinon.assert.calledTwice( spy2 );
- // Attach same event again.
- domEmitter.listenTo( node, 'test1', spy1c );
- domEmitter.listenTo( node, 'test2', spy1d );
- // Old proxy emitter died when stopped listening to the node.
- const proxyEmitter2 = domEmitter._getProxyEmitter( node );
- const spy3 = testUtils.sinon.spy( proxyEmitter2, 'fire' );
- node.dispatchEvent( new Event( 'test1' ) );
- node.dispatchEvent( new Event( 'test2' ) );
- expect( proxyEmitter ).to.not.be.equal( proxyEmitter2 );
- sinon.assert.calledOnce( spy1a );
- sinon.assert.calledOnce( spy1b );
- sinon.assert.calledOnce( spy1c );
- sinon.assert.calledOnce( spy1d );
- sinon.assert.calledTwice( spy2 );
- sinon.assert.calledTwice( spy3 );
- } );
- it( 'should detach native DOM event listener proxy, everything', () => {
- const spy1a = testUtils.sinon.spy();
- const spy1b = testUtils.sinon.spy();
- const spy1c = testUtils.sinon.spy();
- const spy1d = testUtils.sinon.spy();
- const spyEl2 = testUtils.sinon.spy();
- const node2 = document.createElement( 'div' );
- domEmitter.listenTo( node, 'test1', spy1a );
- domEmitter.listenTo( node, 'test2', spy1b );
- domEmitter.listenTo( node2, 'test1', spyEl2 );
- const proxyEmitter = domEmitter._getProxyEmitter( node );
- const spy2 = testUtils.sinon.spy( proxyEmitter, 'fire' );
- node.dispatchEvent( new Event( 'test1' ) );
- node.dispatchEvent( new Event( 'test2' ) );
- node2.dispatchEvent( new Event( 'test1' ) );
- sinon.assert.calledOnce( spy1a );
- sinon.assert.calledOnce( spy1b );
- sinon.assert.calledTwice( spy2 );
- sinon.assert.calledOnce( spyEl2 );
- domEmitter.stopListening();
- node.dispatchEvent( new Event( 'test1' ) );
- node.dispatchEvent( new Event( 'test2' ) );
- node2.dispatchEvent( new Event( 'test1' ) );
- sinon.assert.calledOnce( spy1a );
- sinon.assert.calledOnce( spy1b );
- sinon.assert.calledTwice( spy2 );
- sinon.assert.calledOnce( spyEl2 );
- // Attach same event again.
- domEmitter.listenTo( node, 'test1', spy1c );
- domEmitter.listenTo( node, 'test2', spy1d );
- // Old proxy emitter died when stopped listening to the node.
- const proxyEmitter2 = domEmitter._getProxyEmitter( node );
- const spy3 = testUtils.sinon.spy( proxyEmitter2, 'fire' );
- node.dispatchEvent( new Event( 'test1' ) );
- node.dispatchEvent( new Event( 'test2' ) );
- expect( proxyEmitter ).to.not.be.equal( proxyEmitter2 );
- sinon.assert.calledOnce( spy1a );
- sinon.assert.calledOnce( spy1b );
- sinon.assert.calledOnce( spy1c );
- sinon.assert.calledOnce( spy1d );
- sinon.assert.calledTwice( spy2 );
- sinon.assert.calledTwice( spy3 );
- sinon.assert.calledOnce( spyEl2 );
- } );
- // #187
- it( 'should work for DOM Nodes belonging to another window', done => {
- const spy = testUtils.sinon.spy();
- const iframe = document.createElement( 'iframe' );
- iframe.addEventListener( 'load', () => {
- const iframeNode = iframe.contentWindow.document.createElement( 'div' );
- domEmitter.listenTo( iframeNode, 'test', spy );
- iframeNode.dispatchEvent( new Event( 'test' ) );
- domEmitter.stopListening( iframeNode );
- iframeNode.dispatchEvent( new Event( 'test' ) );
- sinon.assert.calledOnce( spy );
- iframe.remove();
- done();
- } );
- document.body.appendChild( iframe );
- } );
- describe( 'event capturing', () => {
- beforeEach( () => {
- document.body.appendChild( node );
- } );
- afterEach( () => {
- document.body.removeChild( node );
- } );
- it( 'should remove listeners when re–listen', () => {
- const spy = testUtils.sinon.spy();
- domEmitter.listenTo( document, 'test', spy, { useCapture: true } );
- node.dispatchEvent( new Event( 'test' ) );
- sinon.assert.calledOnce( spy );
- domEmitter.stopListening( document, 'test' );
- node.dispatchEvent( new Event( 'test' ) );
- sinon.assert.calledOnce( spy );
- // Listen again.
- domEmitter.listenTo( document, 'test', spy, { useCapture: true } );
- node.dispatchEvent( new Event( 'test', { bubbles: false } ) );
- sinon.assert.calledTwice( spy );
- domEmitter.stopListening( document, 'test' );
- node.dispatchEvent( new Event( 'test', { bubbles: false } ) );
- sinon.assert.calledTwice( spy );
- } );
- } );
- } );
- } );
|