Skip to content

Commit

Permalink
Refactor test for use-game hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
zourdyzou committed Aug 9, 2022
1 parent 423ebc4 commit cd4def8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/modules/GameWithHooks/hooks/use-game/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ interface ReturnType {
level: LevelNames;
isGameOver: boolean;
isWin: boolean;
gameField: Field;
settings: [number, number];
playerField: Field;
onClickHandler: (coords: Coordinates) => void;
Expand Down Expand Up @@ -57,6 +58,7 @@ export const useGame = (): ReturnType => {
level,
isGameOver,
isWin,
gameField,
settings: [size, bombs],
playerField,
onClickHandler,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { CellState, Field } from "@helpers/field";
import { useGame } from "@modules/GameWithHooks/hooks/use-game/index";
import { act, renderHook } from "@testing-library/react";

const { bomb: b } = CellState;

const flatWithFilter = (field: Field, cond: number) => field.flat().filter((v) => v === cond);

describe("useGame test cases", () => {
it("Render hook by default", () => {
const { result } = renderHook(useGame);
const { gameField } = result.current;

expect(flatWithFilter(gameField, b)).toHaveLength(10);
});
it("onReset game handler", () => {
const { result } = renderHook(useGame);
const { playerField, onClickHandler, onResetHandler } = result.current;

expect(playerField).toHaveLength(9);

act(() => onClickHandler([5, 5]));
act(onResetHandler);

const { gameField } = result.current;

expect(flatWithFilter(gameField, b)).toHaveLength(10);
});
});

0 comments on commit cd4def8

Please sign in to comment.