Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nick/on click on reset #29

Merged
merged 3 commits into from
Aug 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/modules/GameWithHooks/GameWithHooks.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ describe('GameWithHooks test cases', () => {

expect(screen.getAllByRole('cell', { name: String(1) })).toHaveLength(1);
});
});
describe('OnClick with OnChangeGameLevel and OnReset', () => {
it('Check click to the cell when the level is changed', () => {
render(<GameWithHooks />);
expect(screen.getAllByRole('cell')).toHaveLength(81);
Expand Down Expand Up @@ -77,4 +79,35 @@ describe('GameWithHooks test cases', () => {
expect(screen.getAllByRole('cell', { name: String(h) })).toHaveLength(81);
});
});
describe('Game over behavior', () => {
it('Player loose the game', () => {
render(<GameWithHooks />);

userEvent.click(screen.getByTestId('0,8'));
expect(screen.getAllByRole('cell', { name: String(1) })).toHaveLength(1);

userEvent.click(screen.getByTestId('0,0'));
expect(screen.getAllByRole('cell', { name: String(e) })).toHaveLength(18);

userEvent.click(screen.getByTestId('0,7'));

const gameLoosePopup = screen.getByText('🙁');

expect(gameLoosePopup).toBeInTheDocument();

expect(screen.queryAllByRole('cell', { name: String(h) })).toHaveLength(
0
);
expect(screen.getAllByRole('cell', { name: String(e) })).toHaveLength(27);
expect(screen.getAllByRole('cell', { name: String(1) })).toHaveLength(30);
expect(screen.getAllByRole('cell', { name: String(2) })).toHaveLength(12);
expect(screen.getAllByRole('cell', { name: String(3) })).toHaveLength(2);

userEvent.click(gameLoosePopup);

expect(screen.getAllByRole('cell', { name: String(h) })).toHaveLength(81);

expect(screen.queryByText('🙁')).not.toBeInTheDocument();
});
});
});
18 changes: 14 additions & 4 deletions src/modules/GameWithHooks/GameWithHooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ import { GameArea, Wrapper, GameOver } from '@/components/Game';
export const GameWithHooks: FC = () => {
const [level, setLevel] = useState<LevelNames>('beginner');

const [isGameOver, setIsGameOver] = useState(false);
const [isWin, setIsWin] = useState(false);

const [size, bombs] = GameSettings[level];

const [playerField, setPlayerField] = useState<Field>(
Expand All @@ -30,8 +33,13 @@ export const GameWithHooks: FC = () => {
);

const onClick = (coords: Coords) => {
const newPlayerField = openCell(coords, playerField, gameField);
setPlayerField([...newPlayerField]);
try {
const newPlayerField = openCell(coords, playerField, gameField);
setPlayerField([...newPlayerField]);
} catch (e) {
setPlayerField([...gameField]);
setIsGameOver(true);
}
};

const resetHandler = ([size, bombs]: [number, number]) => {
Expand All @@ -43,6 +51,8 @@ export const GameWithHooks: FC = () => {

setGameField([...newGameField]);
setPlayerField([...newPlayerField]);
setIsGameOver(false);
setIsWin(false);
};

const onChangeLevel = ({
Expand All @@ -63,13 +73,13 @@ export const GameWithHooks: FC = () => {
<GameArea>
<Scoreboard
time="0"
bombs="10"
bombs={String(bombs)}
levels={GameLevels as unknown as string[]}
defaultLevel={level}
onChangeLevel={onChangeLevel}
onReset={onReset}
/>
<GameOver onClick={() => null} isWin={true} />
{isGameOver && <GameOver onClick={onReset} isWin={isWin} />}
<Grid onClick={onClick} onContextMenu={() => null}>
{playerField}
</Grid>
Expand Down
Loading