@@ -20,8 +20,9 @@ jest.mock('koa-socket', () => {
2020 this . broadcast = { emit : jest . fn ( ) } ;
2121 }
2222
23- receive ( type , ...args ) {
24- this . callbacks [ type ] ( args [ 0 ] , args [ 1 ] , args [ 2 ] , args [ 3 ] , args [ 4 ] ) ;
23+ async receive ( type , ...args ) {
24+ await this . callbacks [ type ] ( args [ 0 ] , args [ 1 ] , args [ 2 ] , args [ 3 ] , args [ 4 ] ) ;
25+ return ;
2526 }
2627
2728 on ( type , callback ) {
@@ -65,7 +66,7 @@ test('basic', () => {
6566 io . socket . receive ( 'disconnect' ) ;
6667} ) ;
6768
68- test ( 'sync' , ( ) => {
69+ test ( 'sync' , async ( ) => {
6970 const server = Server ( { games : [ game ] } ) ;
7071 const io = server . context . io ;
7172 expect ( server ) . not . toBe ( undefined ) ;
@@ -74,40 +75,42 @@ test('sync', () => {
7475
7576 // Sync causes the server to respond.
7677 expect ( io . socket . emit ) . toHaveBeenCalledTimes ( 0 ) ;
77- io . socket . receive ( 'sync' , 'gameID' ) ;
78+ await io . socket . receive ( 'sync' , 'gameID' ) ;
79+
7880 expect ( io . socket . emit ) . toHaveBeenCalledTimes ( 1 ) ;
7981 expect ( spy ) . toHaveBeenCalled ( ) ;
8082
8183 // Sync a second time does not create a game.
8284 spy . mockReset ( ) ;
83- io . socket . receive ( 'sync' , 'gameID' ) ;
85+ await io . socket . receive ( 'sync' , 'gameID' ) ;
86+
8487 expect ( io . socket . emit ) . toHaveBeenCalledTimes ( 2 ) ;
8588 expect ( spy ) . not . toHaveBeenCalled ( ) ;
8689
8790 spy . mockRestore ( ) ;
8891} ) ;
8992
90- test ( 'action' , ( ) => {
93+ test ( 'action' , async ( ) => {
9194 const server = Server ( { games : [ game ] } ) ;
9295 const io = server . context . io ;
9396 const action = ActionCreators . gameEvent ( 'endTurn' ) ;
9497
95- io . socket . receive ( 'action' , action ) ;
98+ await io . socket . receive ( 'action' , action ) ;
9699 expect ( io . socket . emit ) . toHaveBeenCalledTimes ( 0 ) ;
97100 io . socket . emit . mockReset ( ) ;
98101
99- io . socket . receive ( 'sync' , 'gameID' ) ;
102+ await io . socket . receive ( 'sync' , 'gameID' ) ;
100103 io . socket . id = 'second' ;
101- io . socket . receive ( 'sync' , 'gameID' ) ;
104+ await io . socket . receive ( 'sync' , 'gameID' ) ;
102105 io . socket . emit . mockReset ( ) ;
103106
104107 // View-only players cannot send actions.
105- io . socket . receive ( 'action' , action , 0 , 'gameID' , null ) ;
108+ await io . socket . receive ( 'action' , action , 0 , 'gameID' , null ) ;
106109 expect ( io . socket . emit ) . not . toHaveBeenCalled ( ) ;
107110
108111 // Actions are broadcasted as state updates.
109112 // The playerID parameter is necessary to account for view-only players.
110- io . socket . receive ( 'action' , action , 0 , 'gameID' , '0' ) ;
113+ await io . socket . receive ( 'action' , action , 0 , 'gameID' , '0' ) ;
111114 expect ( io . socket . emit ) . lastCalledWith ( 'sync' , 'gameID' , {
112115 G : { } ,
113116 _id : 1 ,
@@ -141,55 +144,55 @@ test('action', () => {
141144 io . socket . emit . mockReset ( ) ;
142145
143146 // ... but not if the gameID is not known.
144- io . socket . receive ( 'action' , action , 1 , 'unknown' , '1' ) ;
147+ await io . socket . receive ( 'action' , action , 1 , 'unknown' , '1' ) ;
145148 expect ( io . socket . emit ) . toHaveBeenCalledTimes ( 0 ) ;
146149
147150 // ... and not if the _id doesn't match the internal state.
148- io . socket . receive ( 'action' , action , 100 , 'gameID' , '1' ) ;
151+ await io . socket . receive ( 'action' , action , 100 , 'gameID' , '1' ) ;
149152 expect ( io . socket . emit ) . toHaveBeenCalledTimes ( 0 ) ;
150153
151154 // ... and not if player != currentPlayer
152- io . socket . receive ( 'action' , action , 1 , 'gameID' , '100' ) ;
155+ await io . socket . receive ( 'action' , action , 1 , 'gameID' , '100' ) ;
153156 expect ( io . socket . emit ) . toHaveBeenCalledTimes ( 0 ) ;
154157
155158 // Another broadcasted action.
156- io . socket . receive ( 'action' , action , 1 , 'gameID' , '1' ) ;
159+ await io . socket . receive ( 'action' , action , 1 , 'gameID' , '1' ) ;
157160 expect ( io . socket . emit ) . toHaveBeenCalledTimes ( 2 ) ;
158161} ) ;
159162
160- test ( 'playerView (sync)' , ( ) => {
163+ test ( 'playerView (sync)' , async ( ) => {
161164 // Write the player into G.
162165 const game = Game ( {
163166 playerView : ( G , ctx , player ) => {
164- return { ... G , player } ;
167+ return Object . assign ( { } , G , { player } ) ;
165168 } ,
166169 } ) ;
167170
168171 const server = Server ( { games : [ game ] } ) ;
169172 const io = server . context . io ;
170173
171- io . socket . receive ( 'sync' , 'gameID' , 0 ) ;
174+ await io . socket . receive ( 'sync' , 'gameID' , 0 ) ;
172175 expect ( io . socket . emit ) . toHaveBeenCalledTimes ( 1 ) ;
173176 expect ( io . socket . emit . mock . calls [ 0 ] [ 2 ] . G ) . toEqual ( { player : 0 } ) ;
174177} ) ;
175178
176- test ( 'playerView (action)' , ( ) => {
179+ test ( 'playerView (action)' , async ( ) => {
177180 const game = Game ( {
178181 playerView : ( G , ctx , player ) => {
179- return { ... G , player } ;
182+ return Object . assign ( { } , G , { player } ) ;
180183 } ,
181184 } ) ;
182185 const server = Server ( { games : [ game ] } ) ;
183186 const io = server . context . io ;
184187 const action = ActionCreators . gameEvent ( 'endTurn' ) ;
185188
186189 io . socket . id = 'first' ;
187- io . socket . receive ( 'sync' , 'gameID' , '0' , 2 ) ;
190+ await io . socket . receive ( 'sync' , 'gameID' , '0' , 2 ) ;
188191 io . socket . id = 'second' ;
189- io . socket . receive ( 'sync' , 'gameID' , '1' , 2 ) ;
192+ await io . socket . receive ( 'sync' , 'gameID' , '1' , 2 ) ;
190193 io . socket . emit . mockReset ( ) ;
191194
192- io . socket . receive ( 'action' , action , 0 , 'gameID' , '0' ) ;
195+ await io . socket . receive ( 'action' , action , 0 , 'gameID' , '0' ) ;
193196 expect ( io . socket . emit ) . toHaveBeenCalledTimes ( 2 ) ;
194197
195198 const G_player0 = io . socket . emit . mock . calls [ 0 ] [ 2 ] . G ;
@@ -199,19 +202,26 @@ test('playerView (action)', () => {
199202 expect ( G_player1 . player ) . toBe ( '1' ) ;
200203} ) ;
201204
202- test ( 'custom db implementation' , ( ) => {
205+ test ( 'custom db implementation' , async ( ) => {
203206 let getId = null ;
207+
204208 class Custom {
205- get ( id ) {
209+ constructor ( ) {
210+ this . games = new Map ( ) ;
211+ }
212+ async get ( id ) {
206213 getId = id ;
214+ return await this . games . get ( id ) ;
215+ }
216+ async set ( id , state ) {
217+ return await this . games . set ( id , state ) ;
207218 }
208- set ( ) { }
209219 }
210220
211221 const game = Game ( { } ) ;
212222 const server = Server ( { games : [ game ] , db : new Custom ( ) } ) ;
213223 const io = server . context . io ;
214224
215- io . socket . receive ( 'sync' , 'gameID' ) ;
225+ await io . socket . receive ( 'sync' , 'gameID' ) ;
216226 expect ( getId ) . toBe ( 'gameID' ) ;
217227} ) ;
0 commit comments