Skip to content

Commit 3cdfbb1

Browse files
committed
chore: add test for hasChanged in game-reducer
1 parent 39c739d commit 3cdfbb1

File tree

1 file changed

+115
-0
lines changed

1 file changed

+115
-0
lines changed

__tests__/reducers/game-reducer.test.ts

+115
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,17 @@ describe("gameReducer", () => {
107107
expect(typeof stateBefore.board[1][0]).toBe("string");
108108
expect(typeof stateBefore.board[3][1]).toBe("string");
109109

110+
expect(stateBefore.hasChanged).toBeFalsy();
111+
110112
act(() => dispatch({ type: "move_up" }));
111113

112114
const [stateAfter] = result.current;
113115
expect(typeof stateAfter.board[0][0]).toBe("string");
114116
expect(typeof stateAfter.board[0][1]).toBe("string");
115117
expect(isNil(stateAfter.board[1][0])).toBeTruthy();
116118
expect(isNil(stateAfter.board[3][1])).toBeTruthy();
119+
120+
expect(stateAfter.hasChanged).toBeTruthy();
117121
});
118122

119123
it("should stack tiles with the same values on top of each other", () => {
@@ -176,6 +180,7 @@ describe("gameReducer", () => {
176180
expect(stateBefore.tiles[stateBefore.board[1][0]].value).toBe(2);
177181
expect(isNil(stateBefore.board[2][0])).toBeTruthy();
178182
expect(stateBefore.tiles[stateBefore.board[3][0]].value).toBe(2);
183+
expect(stateBefore.hasChanged).toBeFalsy();
179184

180185
act(() => dispatch({ type: "move_up" }));
181186

@@ -184,6 +189,32 @@ describe("gameReducer", () => {
184189
expect(isNil(stateAfter.board[1][0])).toBeTruthy();
185190
expect(isNil(stateAfter.board[2][0])).toBeTruthy();
186191
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();
187218
});
188219
});
189220

@@ -212,13 +243,15 @@ describe("gameReducer", () => {
212243
expect(isNil(stateBefore.board[3][0])).toBeTruthy();
213244
expect(typeof stateBefore.board[1][0]).toBe("string");
214245
expect(typeof stateBefore.board[3][1]).toBe("string");
246+
expect(stateBefore.hasChanged).toBeFalsy();
215247

216248
act(() => dispatch({ type: "move_down" }));
217249

218250
const [stateAfter] = result.current;
219251
expect(typeof stateAfter.board[3][0]).toBe("string");
220252
expect(typeof stateAfter.board[3][1]).toBe("string");
221253
expect(isNil(stateAfter.board[1][0])).toBeTruthy();
254+
expect(stateAfter.hasChanged).toBeTruthy();
222255
});
223256

224257
it("should stack tiles with the same values on top of each other", () => {
@@ -281,6 +314,7 @@ describe("gameReducer", () => {
281314
expect(stateBefore.tiles[stateBefore.board[1][0]].value).toBe(2);
282315
expect(isNil(stateBefore.board[2][0])).toBeTruthy();
283316
expect(stateBefore.tiles[stateBefore.board[3][0]].value).toBe(2);
317+
expect(stateBefore.hasChanged).toBeFalsy();
284318

285319
act(() => dispatch({ type: "move_down" }));
286320

@@ -289,6 +323,31 @@ describe("gameReducer", () => {
289323
expect(isNil(stateAfter.board[1][0])).toBeTruthy();
290324
expect(isNil(stateAfter.board[2][0])).toBeTruthy();
291325
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();
292351
});
293352

294353
it("should keep the original order of tiles (regression test)", () => {
@@ -352,13 +411,15 @@ describe("gameReducer", () => {
352411
expect(isNil(stateBefore.board[3][0])).toBeTruthy();
353412
expect(typeof stateBefore.board[1][0]).toBe("string");
354413
expect(typeof stateBefore.board[3][1]).toBe("string");
414+
expect(stateBefore.hasChanged).toBeFalsy();
355415

356416
act(() => dispatch({ type: "move_left" }));
357417

358418
const [stateAfter] = result.current;
359419
expect(typeof stateAfter.board[1][0]).toBe("string");
360420
expect(typeof stateAfter.board[3][0]).toBe("string");
361421
expect(isNil(stateAfter.board[3][1])).toBeTruthy();
422+
expect(stateAfter.hasChanged).toBeTruthy();
362423
});
363424

364425
it("should stack tiles with the same values on top of each other", () => {
@@ -421,6 +482,7 @@ describe("gameReducer", () => {
421482
expect(isNil(stateBefore.board[1][1])).toBeTruthy();
422483
expect(isNil(stateBefore.board[1][2])).toBeTruthy();
423484
expect(stateBefore.tiles[stateBefore.board[1][3]].value).toBe(2);
485+
expect(stateBefore.hasChanged).toBeFalsy();
424486

425487
act(() => dispatch({ type: "move_left" }));
426488

@@ -430,6 +492,31 @@ describe("gameReducer", () => {
430492
expect(isNil(stateAfter.board[1][1])).toBeTruthy();
431493
expect(isNil(stateAfter.board[1][2])).toBeTruthy();
432494
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();
433520
});
434521
});
435522

@@ -459,6 +546,7 @@ describe("gameReducer", () => {
459546
expect(isNil(stateBefore.board[3][3])).toBeTruthy();
460547
expect(typeof stateBefore.board[1][0]).toBe("string");
461548
expect(typeof stateBefore.board[3][1]).toBe("string");
549+
expect(stateBefore.hasChanged).toBeFalsy();
462550

463551
act(() => dispatch({ type: "move_right" }));
464552

@@ -467,6 +555,7 @@ describe("gameReducer", () => {
467555
expect(typeof stateAfter.board[3][3]).toBe("string");
468556
expect(isNil(stateAfter.board[1][0])).toBeTruthy();
469557
expect(isNil(stateAfter.board[3][1])).toBeTruthy();
558+
expect(stateAfter.hasChanged).toBeTruthy();
470559
});
471560

472561
it("should stack tiles with the same values on top of each other", () => {
@@ -529,6 +618,7 @@ describe("gameReducer", () => {
529618
expect(isNil(stateBefore.board[1][1])).toBeTruthy();
530619
expect(isNil(stateBefore.board[1][2])).toBeTruthy();
531620
expect(stateBefore.tiles[stateBefore.board[1][3]].value).toBe(2);
621+
expect(stateBefore.hasChanged).toBeFalsy();
532622

533623
act(() => dispatch({ type: "move_right" }));
534624

@@ -537,6 +627,31 @@ describe("gameReducer", () => {
537627
expect(isNil(stateAfter.board[1][1])).toBeTruthy();
538628
expect(isNil(stateAfter.board[1][2])).toBeTruthy();
539629
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();
540655
});
541656

542657
it("keep the original order of tiles (regression test)", () => {

0 commit comments

Comments
 (0)