@@ -5,6 +5,7 @@ class Events {
55 constructor ( ) {
66 this . commandHandlers = { } ;
77 this . handlers = { } ;
8+ this . emissions = { } ;
89
910 this . assert = new EventsAssert ( this ) ;
1011 }
@@ -18,14 +19,13 @@ class Events {
1819 this . handlers [ ev ] = [ ] ;
1920 }
2021
21- this . handlers [ ev ] . push ( cb ) ;
22+ this . handlers [ ev ] . push ( sinon . spy ( cb ) ) ;
2223 }
2324
24- emit ( ) {
25+ emit ( ev , ... args ) {
2526
26- }
27+ this . emissions [ ev ] = args ;
2728
28- trigger ( ev , ...args ) {
2929 if ( ! this . handlers [ ev ] ) {
3030 return ;
3131 }
@@ -34,12 +34,12 @@ class Events {
3434 }
3535
3636 request ( cmd , ...args ) {
37- assert ( this . commandHandlers [ cmd ] , `command handler for ${ cmd } not registered` ) ;
37+ assert ( this . commandHandlers [ cmd ] , `command handler for ' ${ cmd } ' not registered` ) ;
3838 Promise . resolve ( this . commandHandlers [ cmd ] ( ...args ) ) ;
3939 }
4040
4141 request2 ( cmd , ...args ) {
42- assert ( this . commandHandlers [ cmd ] , `command handler for ${ cmd } not registered` ) ;
42+ assert ( this . commandHandlers [ cmd ] , `command handler for ' ${ cmd } ' not registered` ) ;
4343 return new Promise ( ( resolve , reject ) => {
4444 args . push ( ( err , ...res ) => {
4545 if ( err ) {
@@ -53,6 +53,12 @@ class Events {
5353 this . commandHandlers [ cmd ] ( ...args ) ;
5454 } ) ;
5555 }
56+
57+ teardown ( ) {
58+ this . commandHandlers = { } ;
59+ this . handlers = { } ;
60+ this . emissions = { } ;
61+ }
5662}
5763
5864class EventsAssert {
@@ -61,19 +67,40 @@ class EventsAssert {
6167 }
6268
6369 commandHandlerRegistered ( cmd ) {
64- assert ( this . events . commandHandlers [ cmd ] , `command handler for ${ cmd } wanted, but not registered` ) ;
70+ assert ( this . events . commandHandlers [ cmd ] , `command handler for ' ${ cmd } ' wanted, but not registered` ) ;
6571 }
6672
6773 commandHandlerCalled ( cmd ) {
6874 this . commandHandlerRegistered ( cmd ) ;
6975 sinon . assert . called ( this . events . commandHandlers [ cmd ] ) ;
7076 }
77+
78+ commandHandlerNotCalled ( cmd ) {
79+ this . commandHandlerRegistered ( cmd ) ;
80+ assert ( ! this . events . commandHandlers [ cmd ] . called ) ;
81+ }
7182
7283 commandHandlerCalledWith ( cmd , ...args ) {
7384 this . commandHandlerRegistered ( cmd ) ;
7485 sinon . assert . calledWith ( this . events . commandHandlers [ cmd ] , ...args ) ;
7586 }
7687
88+ listenerRegistered ( name ) {
89+ assert ( this . events . handlers [ name ] , `event listener for '${ name } ' wanted, but not registered` ) ;
90+ }
91+
92+ emitted ( name ) {
93+ assert ( this . events . emissions [ name ] ) ;
94+ }
95+
96+ notEmitted ( name ) {
97+ assert ( ! Object . keys ( this . events . emissions ) . includes ( name ) ) ;
98+ }
99+
100+ emittedWith ( name , ...args ) {
101+ assert . equal ( this . events . emissions [ name ] , ...args ) ;
102+ }
103+
77104}
78105
79106module . exports = Events ;
0 commit comments