Przeglądaj źródła

Tests: Refactoring in EmitterMixin#delegate|stopDelegating tests to be more like James Bond.

Aleksander Nowodzinski 9 lat temu
rodzic
commit
a577f9b893
1 zmienionych plików z 62 dodań i 48 usunięć
  1. 62 48
      packages/ckeditor5-utils/tests/emittermixin.js

+ 62 - 48
packages/ckeditor5-utils/tests/emittermixin.js

@@ -565,25 +565,25 @@ describe( 'delegate', () => {
 		it( 'forwards multiple events to another emitter', () => {
 			const emitterA = getEmitterInstance();
 			const emitterB = getEmitterInstance();
+			const spyFoo = sinon.spy();
+			const spyBar = sinon.spy();
+			const spyBaz = sinon.spy();
 			const dataA = {};
 			const dataB = {};
 
 			emitterB.delegate( 'foo', 'bar', 'baz' ).to( emitterA );
 
-			const evts = [];
-
-			function recordEvent( ...args ) {
-				evts.push( args );
-			}
-
-			emitterA.on( 'foo', recordEvent );
-			emitterA.on( 'bar', recordEvent );
-			emitterA.on( 'baz', recordEvent );
+			emitterA.on( 'foo', spyFoo );
+			emitterA.on( 'bar', spyBar );
+			emitterA.on( 'baz', spyBaz );
 
 			emitterB.fire( 'foo', dataA, dataB );
 
-			expect( evts ).to.have.length( 1 );
-			assertDelegated( evts[ 0 ], {
+			sinon.assert.calledOnce( spyFoo );
+			sinon.assert.notCalled( spyBar );
+			sinon.assert.notCalled( spyBaz );
+
+			assertDelegated( spyFoo.args[ 0 ], {
 				expectedSource: emitterB,
 				expectedName: 'foo',
 				expectedPath: [ emitterB, emitterA ],
@@ -592,8 +592,11 @@ describe( 'delegate', () => {
 
 			emitterB.fire( 'bar' );
 
-			expect( evts ).to.have.length( 2 );
-			assertDelegated( evts[ 1 ], {
+			sinon.assert.calledOnce( spyFoo );
+			sinon.assert.calledOnce( spyBar );
+			sinon.assert.notCalled( spyBaz );
+
+			assertDelegated( spyBar.args[ 0 ], {
 				expectedSource: emitterB,
 				expectedName: 'bar',
 				expectedPath: [ emitterB, emitterA ],
@@ -602,8 +605,11 @@ describe( 'delegate', () => {
 
 			emitterB.fire( 'baz' );
 
-			expect( evts ).to.have.length( 3 );
-			assertDelegated( evts[ 2 ], {
+			sinon.assert.calledOnce( spyFoo );
+			sinon.assert.calledOnce( spyBar );
+			sinon.assert.calledOnce( spyBaz );
+
+			assertDelegated( spyBaz.args[ 0 ], {
 				expectedSource: emitterB,
 				expectedName: 'baz',
 				expectedPath: [ emitterB, emitterA ],
@@ -611,26 +617,34 @@ describe( 'delegate', () => {
 			} );
 
 			emitterB.fire( 'not-delegated' );
-			expect( evts ).to.have.length( 3 );
+
+			sinon.assert.calledOnce( spyFoo );
+			sinon.assert.calledOnce( spyBar );
+			sinon.assert.calledOnce( spyBaz );
 		} );
 
 		it( 'does not forward events which are not supposed to be delegated', () => {
 			const emitterA = getEmitterInstance();
 			const emitterB = getEmitterInstance();
+			const spyFoo = sinon.spy();
+			const spyBar = sinon.spy();
+			const spyBaz = sinon.spy();
 
 			emitterB.delegate( 'foo', 'bar', 'baz' ).to( emitterA );
 
-			let fireLog = [];
-			emitterA.on( 'foo', () => fireLog.push( 'A#foo' ) );
-			emitterA.on( 'bar', () => fireLog.push( 'A#bar' ) );
-			emitterA.on( 'baz', () => fireLog.push( 'A#baz' ) );
+			emitterA.on( 'foo', spyFoo );
+			emitterA.on( 'bar', spyBar );
+			emitterA.on( 'baz', spyBaz );
 
 			emitterB.fire( 'foo' );
 			emitterB.fire( 'bar' );
 			emitterB.fire( 'baz' );
 			emitterB.fire( 'not-delegated' );
 
-			expect( fireLog ).to.deep.equal( [ 'A#foo', 'A#bar', 'A#baz' ] );
+			sinon.assert.callOrder( spyFoo, spyBar, spyBaz );
+			sinon.assert.callCount( spyFoo, 1 );
+			sinon.assert.callCount( spyBar, 1 );
+			sinon.assert.callCount( spyBaz, 1 );
 		} );
 
 		it( 'supports deep chain event delegation', ( done ) => {
@@ -656,24 +670,20 @@ describe( 'delegate', () => {
 			emitterC.fire( 'foo', data );
 		} );
 
-		it( 'executes callbacks first, then delegates further', ( done ) => {
+		it( 'executes callbacks first, then delegates further', () => {
 			const emitterA = getEmitterInstance();
 			const emitterB = getEmitterInstance();
-			const callOrder = [];
+			const spyA = sinon.spy();
+			const spyB = sinon.spy();
 
 			emitterB.delegate( 'foo' ).to( emitterA );
 
-			emitterA.on( 'foo', () => {
-				callOrder.push( 'emitterA' );
-				expect( callOrder ).to.deep.equal( [ 'emitterB', 'emitterA' ] );
-				done();
-			} );
-
-			emitterB.on( 'foo', () => {
-				callOrder.push( 'emitterB' );
-			} );
+			emitterA.on( 'foo', spyA );
+			emitterB.on( 'foo', spyB );
 
 			emitterB.fire( 'foo' );
+
+			sinon.assert.callOrder( spyB, spyA );
 		} );
 	} );
 } );
@@ -689,74 +699,78 @@ describe( 'stopDelegating', () => {
 		const emitterA = getEmitterInstance();
 		const emitterB = getEmitterInstance();
 		const emitterC = getEmitterInstance();
+		const spyFoo = sinon.spy();
+		const spyBar = sinon.spy();
 
 		emitterA.delegate( 'foo' ).to( emitterB );
 		emitterA.delegate( 'bar' ).to( emitterC );
 
-		let fireLog = [];
-		emitterB.on( 'foo', () => fireLog.push( 'B#foo' ) );
-		emitterC.on( 'bar', () => fireLog.push( 'C#bar' ) );
+		emitterB.on( 'foo', spyFoo );
+		emitterC.on( 'bar', spyBar );
 
 		emitterA.fire( 'foo' );
 		emitterA.fire( 'bar' );
 
-		expect( fireLog ).to.deep.equal( [ 'B#foo', 'C#bar' ] );
+		sinon.assert.callOrder( spyFoo, spyBar );
 
 		emitterA.stopDelegating();
 
 		emitterA.fire( 'foo' );
 		emitterA.fire( 'bar' );
 
-		expect( fireLog ).to.deep.equal( [ 'B#foo', 'C#bar' ] );
+		sinon.assert.callOrder( spyFoo, spyBar );
 	} );
 
 	it( 'stops delegating a specific event to all emitters', () => {
 		const emitterA = getEmitterInstance();
 		const emitterB = getEmitterInstance();
 		const emitterC = getEmitterInstance();
+		const spyFooB = sinon.spy();
+		const spyFooC = sinon.spy();
+		const spyBarC = sinon.spy();
 
 		emitterA.delegate( 'foo' ).to( emitterB );
 		emitterA.delegate( 'foo' ).to( emitterC );
 		emitterA.delegate( 'bar' ).to( emitterC );
 
-		let fireLog = [];
-		emitterB.on( 'foo', () => fireLog.push( 'B#foo' ) );
-		emitterC.on( 'foo', () => fireLog.push( 'C#foo' ) );
-		emitterC.on( 'bar', () => fireLog.push( 'C#bar' ) );
+		emitterB.on( 'foo', spyFooB );
+		emitterC.on( 'foo', spyFooC );
+		emitterC.on( 'bar', spyBarC );
 
 		emitterA.fire( 'foo' );
 		emitterA.fire( 'bar' );
 
-		expect( fireLog ).to.deep.equal( [ 'B#foo', 'C#foo', 'C#bar' ] );
+		sinon.assert.callOrder( spyFooB, spyFooC, spyBarC );
 
 		emitterA.stopDelegating( 'foo' );
 
 		emitterA.fire( 'foo' );
 		emitterA.fire( 'bar' );
 
-		expect( fireLog ).to.deep.equal( [ 'B#foo', 'C#foo', 'C#bar', 'C#bar' ] );
+		sinon.assert.callOrder( spyFooB, spyFooC, spyBarC, spyBarC );
 	} );
 
 	it( 'stops delegating a specific event to a specific emitter', () => {
 		const emitterA = getEmitterInstance();
 		const emitterB = getEmitterInstance();
 		const emitterC = getEmitterInstance();
+		const spyFooB = sinon.spy();
+		const spyFooC = sinon.spy();
 
 		emitterA.delegate( 'foo' ).to( emitterB );
 		emitterA.delegate( 'foo' ).to( emitterC );
 
-		let fireLog = [];
-		emitterB.on( 'foo', () => fireLog.push( 'B#foo' ) );
-		emitterC.on( 'foo', () => fireLog.push( 'C#foo' ) );
+		emitterB.on( 'foo', spyFooB );
+		emitterC.on( 'foo', spyFooC );
 
 		emitterA.fire( 'foo' );
 
-		expect( fireLog ).to.deep.equal( [ 'B#foo', 'C#foo' ] );
+		sinon.assert.callOrder( spyFooB, spyFooC );
 
 		emitterA.stopDelegating( 'foo', emitterC );
 		emitterA.fire( 'foo' );
 
-		expect( fireLog ).to.deep.equal( [ 'B#foo', 'C#foo', 'B#foo' ] );
+		sinon.assert.callOrder( spyFooB, spyFooC, spyFooB );
 	} );
 
 	it( 'passes when stopping delegation of a specific event to an emitter which wasn\'t a destination', () => {