-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #56 from nickovchinnikov/nick/gameWithRedux
Add React + Redux and useReducer
- Loading branch information
Showing
12 changed files
with
8,365 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import React from 'react'; | ||
import userEvent from '@testing-library/user-event'; | ||
import { render, screen } from '@testing-library/react'; | ||
|
||
import { GameWithRedux } from './GameWithRedux'; | ||
|
||
jest.mock('@/core/Field'); | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
describe('GameWithHooks test cases', () => { | ||
it('Render game field by default', () => { | ||
const { asFragment } = render(<GameWithRedux />); | ||
expect(asFragment()).toMatchSnapshot(); | ||
}); | ||
it('Cell click works fine', () => { | ||
const { asFragment } = render(<GameWithRedux />); | ||
userEvent.click(screen.getByTestId('0,0')); | ||
expect(asFragment()).toMatchSnapshot(); | ||
}); | ||
it('Context menu handler on a cell works fine', () => { | ||
const { asFragment } = render(<GameWithRedux />); | ||
userEvent.click(screen.getByTestId('0,0'), { button: 2 }); | ||
expect(asFragment()).toMatchSnapshot(); | ||
}); | ||
it('Reset handler works fine', () => { | ||
const { asFragment } = render(<GameWithRedux />); | ||
userEvent.click(screen.getByTestId('0,0')); | ||
expect(asFragment()).toMatchSnapshot(); | ||
userEvent.click(screen.getByRole('button')); | ||
expect(asFragment()).toMatchSnapshot(); | ||
}); | ||
it('Change level works fine', () => { | ||
const { asFragment } = render(<GameWithRedux />); | ||
userEvent.selectOptions(screen.getByRole('combobox'), 'intermediate'); | ||
expect(asFragment()).toMatchSnapshot(); | ||
}); | ||
it('Game over reset the game state', () => { | ||
const { asFragment } = render(<GameWithRedux />); | ||
userEvent.click(screen.getByTestId('0,0')); | ||
expect(asFragment()).toMatchSnapshot(); | ||
userEvent.click(screen.getByText('🙂')); | ||
expect(asFragment()).toMatchSnapshot(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import React, { FC, useReducer, useCallback } from 'react'; | ||
|
||
import { Coords } from '@/core/Field'; | ||
|
||
import { GameLevels, LevelNames } from '@/modules/GameSettings'; | ||
|
||
import { Scoreboard } from '@/components/Scoreboard'; | ||
import { Grid } from '@/components/Grid'; | ||
import { GameOver } from '@/components/Game'; | ||
|
||
import { reducer, actions, getInitialState } from './game'; | ||
|
||
export const GameWithRedux: FC = () => { | ||
const [ | ||
{ level, time, isGameOver, isWin, settings, playerField, flagCounter }, | ||
dispatch, | ||
] = useReducer(reducer, getInitialState()); | ||
|
||
const [, bombs] = settings; | ||
|
||
const onClick = useCallback( | ||
(coords: Coords) => dispatch(actions.openCell(coords)), | ||
// Stryker disable next-line ArrayDeclaration | ||
[] | ||
); | ||
|
||
const onReset = useCallback( | ||
() => dispatch(actions.reset()), | ||
// Stryker disable next-line ArrayDeclaration | ||
[] | ||
); | ||
|
||
const onContextMenu = useCallback( | ||
(coords: Coords) => dispatch(actions.setFlag(coords)), | ||
// Stryker disable next-line ArrayDeclaration | ||
[] | ||
); | ||
|
||
const onChangeLevel = useCallback( | ||
({ target: { value: level } }: React.ChangeEvent<HTMLSelectElement>) => | ||
dispatch(actions.changeLevel(level as LevelNames)), | ||
// Stryker disable next-line ArrayDeclaration | ||
[] | ||
); | ||
|
||
return ( | ||
<> | ||
<Scoreboard | ||
time={String(time)} | ||
bombs={String(bombs - flagCounter)} | ||
levels={GameLevels as unknown as string[]} | ||
defaultLevel={level} | ||
onChangeLevel={onChangeLevel} | ||
onReset={onReset} | ||
/> | ||
{isGameOver && <GameOver onClick={onReset} isWin={isWin} />} | ||
<Grid onClick={onClick} onContextMenu={onContextMenu}> | ||
{playerField} | ||
</Grid> | ||
</> | ||
); | ||
}; |
Oops, something went wrong.