@@ -107,13 +107,17 @@ describe("gameReducer", () => {
107
107
expect ( typeof stateBefore . board [ 1 ] [ 0 ] ) . toBe ( "string" ) ;
108
108
expect ( typeof stateBefore . board [ 3 ] [ 1 ] ) . toBe ( "string" ) ;
109
109
110
+ expect ( stateBefore . hasChanged ) . toBeFalsy ( ) ;
111
+
110
112
act ( ( ) => dispatch ( { type : "move_up" } ) ) ;
111
113
112
114
const [ stateAfter ] = result . current ;
113
115
expect ( typeof stateAfter . board [ 0 ] [ 0 ] ) . toBe ( "string" ) ;
114
116
expect ( typeof stateAfter . board [ 0 ] [ 1 ] ) . toBe ( "string" ) ;
115
117
expect ( isNil ( stateAfter . board [ 1 ] [ 0 ] ) ) . toBeTruthy ( ) ;
116
118
expect ( isNil ( stateAfter . board [ 3 ] [ 1 ] ) ) . toBeTruthy ( ) ;
119
+
120
+ expect ( stateAfter . hasChanged ) . toBeTruthy ( ) ;
117
121
} ) ;
118
122
119
123
it ( "should stack tiles with the same values on top of each other" , ( ) => {
@@ -176,6 +180,7 @@ describe("gameReducer", () => {
176
180
expect ( stateBefore . tiles [ stateBefore . board [ 1 ] [ 0 ] ] . value ) . toBe ( 2 ) ;
177
181
expect ( isNil ( stateBefore . board [ 2 ] [ 0 ] ) ) . toBeTruthy ( ) ;
178
182
expect ( stateBefore . tiles [ stateBefore . board [ 3 ] [ 0 ] ] . value ) . toBe ( 2 ) ;
183
+ expect ( stateBefore . hasChanged ) . toBeFalsy ( ) ;
179
184
180
185
act ( ( ) => dispatch ( { type : "move_up" } ) ) ;
181
186
@@ -184,6 +189,32 @@ describe("gameReducer", () => {
184
189
expect ( isNil ( stateAfter . board [ 1 ] [ 0 ] ) ) . toBeTruthy ( ) ;
185
190
expect ( isNil ( stateAfter . board [ 2 ] [ 0 ] ) ) . toBeTruthy ( ) ;
186
191
expect ( isNil ( stateAfter . board [ 3 ] [ 0 ] ) ) . toBeTruthy ( ) ;
192
+ expect ( stateAfter . hasChanged ) . toBeTruthy ( ) ;
193
+ } ) ;
194
+
195
+ it ( "should keep hasChanged falsy if no tile changed position or value" , ( ) => {
196
+ const tile1 : Tile = {
197
+ position : [ 0 , 0 ] ,
198
+ value : 4 ,
199
+ } ;
200
+ const tile2 : Tile = {
201
+ position : [ 0 , 1 ] ,
202
+ value : 2 ,
203
+ } ;
204
+
205
+ const { result } = renderHook ( ( ) =>
206
+ useReducer ( gameReducer , initialState ) ,
207
+ ) ;
208
+ const [ , dispatch ] = result . current ;
209
+
210
+ act ( ( ) => {
211
+ dispatch ( { type : "create_tile" , tile : tile1 } ) ;
212
+ dispatch ( { type : "create_tile" , tile : tile2 } ) ;
213
+ dispatch ( { type : "move_up" } ) ;
214
+ } ) ;
215
+
216
+ const [ stateAfter ] = result . current ;
217
+ expect ( stateAfter . hasChanged ) . toBeFalsy ( ) ;
187
218
} ) ;
188
219
} ) ;
189
220
@@ -212,13 +243,15 @@ describe("gameReducer", () => {
212
243
expect ( isNil ( stateBefore . board [ 3 ] [ 0 ] ) ) . toBeTruthy ( ) ;
213
244
expect ( typeof stateBefore . board [ 1 ] [ 0 ] ) . toBe ( "string" ) ;
214
245
expect ( typeof stateBefore . board [ 3 ] [ 1 ] ) . toBe ( "string" ) ;
246
+ expect ( stateBefore . hasChanged ) . toBeFalsy ( ) ;
215
247
216
248
act ( ( ) => dispatch ( { type : "move_down" } ) ) ;
217
249
218
250
const [ stateAfter ] = result . current ;
219
251
expect ( typeof stateAfter . board [ 3 ] [ 0 ] ) . toBe ( "string" ) ;
220
252
expect ( typeof stateAfter . board [ 3 ] [ 1 ] ) . toBe ( "string" ) ;
221
253
expect ( isNil ( stateAfter . board [ 1 ] [ 0 ] ) ) . toBeTruthy ( ) ;
254
+ expect ( stateAfter . hasChanged ) . toBeTruthy ( ) ;
222
255
} ) ;
223
256
224
257
it ( "should stack tiles with the same values on top of each other" , ( ) => {
@@ -281,6 +314,7 @@ describe("gameReducer", () => {
281
314
expect ( stateBefore . tiles [ stateBefore . board [ 1 ] [ 0 ] ] . value ) . toBe ( 2 ) ;
282
315
expect ( isNil ( stateBefore . board [ 2 ] [ 0 ] ) ) . toBeTruthy ( ) ;
283
316
expect ( stateBefore . tiles [ stateBefore . board [ 3 ] [ 0 ] ] . value ) . toBe ( 2 ) ;
317
+ expect ( stateBefore . hasChanged ) . toBeFalsy ( ) ;
284
318
285
319
act ( ( ) => dispatch ( { type : "move_down" } ) ) ;
286
320
@@ -289,6 +323,31 @@ describe("gameReducer", () => {
289
323
expect ( isNil ( stateAfter . board [ 1 ] [ 0 ] ) ) . toBeTruthy ( ) ;
290
324
expect ( isNil ( stateAfter . board [ 2 ] [ 0 ] ) ) . toBeTruthy ( ) ;
291
325
expect ( stateAfter . tiles [ stateAfter . board [ 3 ] [ 0 ] ] . value ) . toBe ( 4 ) ;
326
+ expect ( stateAfter . hasChanged ) . toBeTruthy ( ) ;
327
+ } ) ;
328
+
329
+ it ( "should keep hasChanged falsy if no tile changed position or value" , ( ) => {
330
+ const tile1 : Tile = {
331
+ position : [ 0 , 3 ] ,
332
+ value : 2 ,
333
+ } ;
334
+ const tile2 : Tile = {
335
+ position : [ 1 , 3 ] ,
336
+ value : 2 ,
337
+ } ;
338
+
339
+ const { result } = renderHook ( ( ) =>
340
+ useReducer ( gameReducer , initialState ) ,
341
+ ) ;
342
+ const [ , dispatch ] = result . current ;
343
+
344
+ act ( ( ) => {
345
+ dispatch ( { type : "create_tile" , tile : tile1 } ) ;
346
+ dispatch ( { type : "create_tile" , tile : tile2 } ) ;
347
+ dispatch ( { type : "move_down" } ) ;
348
+ } ) ;
349
+
350
+ expect ( result . current [ 0 ] . hasChanged ) . toBeFalsy ( ) ;
292
351
} ) ;
293
352
294
353
it ( "should keep the original order of tiles (regression test)" , ( ) => {
@@ -352,13 +411,15 @@ describe("gameReducer", () => {
352
411
expect ( isNil ( stateBefore . board [ 3 ] [ 0 ] ) ) . toBeTruthy ( ) ;
353
412
expect ( typeof stateBefore . board [ 1 ] [ 0 ] ) . toBe ( "string" ) ;
354
413
expect ( typeof stateBefore . board [ 3 ] [ 1 ] ) . toBe ( "string" ) ;
414
+ expect ( stateBefore . hasChanged ) . toBeFalsy ( ) ;
355
415
356
416
act ( ( ) => dispatch ( { type : "move_left" } ) ) ;
357
417
358
418
const [ stateAfter ] = result . current ;
359
419
expect ( typeof stateAfter . board [ 1 ] [ 0 ] ) . toBe ( "string" ) ;
360
420
expect ( typeof stateAfter . board [ 3 ] [ 0 ] ) . toBe ( "string" ) ;
361
421
expect ( isNil ( stateAfter . board [ 3 ] [ 1 ] ) ) . toBeTruthy ( ) ;
422
+ expect ( stateAfter . hasChanged ) . toBeTruthy ( ) ;
362
423
} ) ;
363
424
364
425
it ( "should stack tiles with the same values on top of each other" , ( ) => {
@@ -421,6 +482,7 @@ describe("gameReducer", () => {
421
482
expect ( isNil ( stateBefore . board [ 1 ] [ 1 ] ) ) . toBeTruthy ( ) ;
422
483
expect ( isNil ( stateBefore . board [ 1 ] [ 2 ] ) ) . toBeTruthy ( ) ;
423
484
expect ( stateBefore . tiles [ stateBefore . board [ 1 ] [ 3 ] ] . value ) . toBe ( 2 ) ;
485
+ expect ( stateBefore . hasChanged ) . toBeFalsy ( ) ;
424
486
425
487
act ( ( ) => dispatch ( { type : "move_left" } ) ) ;
426
488
@@ -430,6 +492,31 @@ describe("gameReducer", () => {
430
492
expect ( isNil ( stateAfter . board [ 1 ] [ 1 ] ) ) . toBeTruthy ( ) ;
431
493
expect ( isNil ( stateAfter . board [ 1 ] [ 2 ] ) ) . toBeTruthy ( ) ;
432
494
expect ( isNil ( stateAfter . board [ 1 ] [ 3 ] ) ) . toBeTruthy ( ) ;
495
+ expect ( stateAfter . hasChanged ) . toBeTruthy ( ) ;
496
+ } ) ;
497
+
498
+ it ( "should keep hasChanged falsy if no tile changed position or value" , ( ) => {
499
+ const tile1 : Tile = {
500
+ position : [ 0 , 0 ] ,
501
+ value : 2 ,
502
+ } ;
503
+ const tile2 : Tile = {
504
+ position : [ 0 , 1 ] ,
505
+ value : 2 ,
506
+ } ;
507
+
508
+ const { result } = renderHook ( ( ) =>
509
+ useReducer ( gameReducer , initialState ) ,
510
+ ) ;
511
+ const [ , dispatch ] = result . current ;
512
+
513
+ act ( ( ) => {
514
+ dispatch ( { type : "create_tile" , tile : tile1 } ) ;
515
+ dispatch ( { type : "create_tile" , tile : tile2 } ) ;
516
+ dispatch ( { type : "move_left" } ) ;
517
+ } ) ;
518
+
519
+ expect ( result . current [ 0 ] . hasChanged ) . toBeFalsy ( ) ;
433
520
} ) ;
434
521
} ) ;
435
522
@@ -459,6 +546,7 @@ describe("gameReducer", () => {
459
546
expect ( isNil ( stateBefore . board [ 3 ] [ 3 ] ) ) . toBeTruthy ( ) ;
460
547
expect ( typeof stateBefore . board [ 1 ] [ 0 ] ) . toBe ( "string" ) ;
461
548
expect ( typeof stateBefore . board [ 3 ] [ 1 ] ) . toBe ( "string" ) ;
549
+ expect ( stateBefore . hasChanged ) . toBeFalsy ( ) ;
462
550
463
551
act ( ( ) => dispatch ( { type : "move_right" } ) ) ;
464
552
@@ -467,6 +555,7 @@ describe("gameReducer", () => {
467
555
expect ( typeof stateAfter . board [ 3 ] [ 3 ] ) . toBe ( "string" ) ;
468
556
expect ( isNil ( stateAfter . board [ 1 ] [ 0 ] ) ) . toBeTruthy ( ) ;
469
557
expect ( isNil ( stateAfter . board [ 3 ] [ 1 ] ) ) . toBeTruthy ( ) ;
558
+ expect ( stateAfter . hasChanged ) . toBeTruthy ( ) ;
470
559
} ) ;
471
560
472
561
it ( "should stack tiles with the same values on top of each other" , ( ) => {
@@ -529,6 +618,7 @@ describe("gameReducer", () => {
529
618
expect ( isNil ( stateBefore . board [ 1 ] [ 1 ] ) ) . toBeTruthy ( ) ;
530
619
expect ( isNil ( stateBefore . board [ 1 ] [ 2 ] ) ) . toBeTruthy ( ) ;
531
620
expect ( stateBefore . tiles [ stateBefore . board [ 1 ] [ 3 ] ] . value ) . toBe ( 2 ) ;
621
+ expect ( stateBefore . hasChanged ) . toBeFalsy ( ) ;
532
622
533
623
act ( ( ) => dispatch ( { type : "move_right" } ) ) ;
534
624
@@ -537,6 +627,31 @@ describe("gameReducer", () => {
537
627
expect ( isNil ( stateAfter . board [ 1 ] [ 1 ] ) ) . toBeTruthy ( ) ;
538
628
expect ( isNil ( stateAfter . board [ 1 ] [ 2 ] ) ) . toBeTruthy ( ) ;
539
629
expect ( stateAfter . tiles [ stateAfter . board [ 1 ] [ 3 ] ] . value ) . toBe ( 4 ) ;
630
+ expect ( stateAfter . hasChanged ) . toBeTruthy ( ) ;
631
+ } ) ;
632
+
633
+ it ( "should keep hasChanged falsy if no tile changed position or value" , ( ) => {
634
+ const tile1 : Tile = {
635
+ position : [ 3 , 0 ] ,
636
+ value : 2 ,
637
+ } ;
638
+ const tile2 : Tile = {
639
+ position : [ 3 , 1 ] ,
640
+ value : 2 ,
641
+ } ;
642
+
643
+ const { result } = renderHook ( ( ) =>
644
+ useReducer ( gameReducer , initialState ) ,
645
+ ) ;
646
+ const [ , dispatch ] = result . current ;
647
+
648
+ act ( ( ) => {
649
+ dispatch ( { type : "create_tile" , tile : tile1 } ) ;
650
+ dispatch ( { type : "create_tile" , tile : tile2 } ) ;
651
+ dispatch ( { type : "move_right" } ) ;
652
+ } ) ;
653
+
654
+ expect ( result . current [ 0 ] . hasChanged ) . toBeFalsy ( ) ;
540
655
} ) ;
541
656
542
657
it ( "keep the original order of tiles (regression test)" , ( ) => {
0 commit comments