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

Test refactoring #31

Merged
merged 2 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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,3 +187,7 @@
### Game custom hook

[Pull request](https://github.com/nickovchinnikov/minesweeper/pull/30)

### Test refactoring

[Pull request](https://github.com/nickovchinnikov/minesweeper/pull/31/files)
144 changes: 44 additions & 100 deletions src/modules/GameWithHooks/GameWithHooks.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,112 +2,56 @@ import React from 'react';
import userEvent from '@testing-library/user-event';
import { render, screen } from '@testing-library/react';

import { CellState } from '@/helpers/Field';

import { GameWithHooks } from './GameWithHooks';

const { empty: e, hidden: h, bomb: b, flag: f } = CellState;

jest.mock('@/helpers/Field');
const mockOnClick = jest.fn();
const mockOnChangeLevel = jest.fn();
const mockOnReset = jest.fn();

jest.mock('./useGame', () => ({
__esModule: true,
useGame: () => ({
level: 'beginner',
isGameOver: true,
isWin: false,
settings: [9, 10],
playerField: [
[10, 10],
[10, 10],
],
onClick: mockOnClick,
onChangeLevel: mockOnChangeLevel,
onReset: mockOnReset,
}),
}));

beforeEach(() => {
jest.clearAllMocks();
});

describe('GameWithHooks test cases', () => {
describe('Render behaviour', () => {
it('Render game field by default', () => {
const { asFragment } = render(<GameWithHooks />);
expect(screen.getAllByRole('cell')).toHaveLength(81);
expect(asFragment()).toMatchSnapshot();
});
it('onChange game level handler', () => {
render(<GameWithHooks />);
expect(screen.getAllByRole('cell')).toHaveLength(81);
userEvent.selectOptions(screen.getByRole('combobox'), 'intermediate');
expect(screen.getAllByRole('cell')).toHaveLength(256);
userEvent.selectOptions(screen.getByRole('combobox'), 'expert');
expect(screen.getAllByRole('cell')).toHaveLength(484);
});
it('Render game field by default', () => {
const { asFragment } = render(<GameWithHooks />);
expect(asFragment()).toMatchSnapshot();
});
describe('Open cell test cases', () => {
it('Open empty cell on the beginner level', () => {
render(<GameWithHooks />);

expect(screen.queryAllByRole('cell', { name: String(e) })).toHaveLength(
0
);

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

expect(screen.getAllByRole('cell', { name: String(e) })).toHaveLength(18);
});
it('Click to the non-empty cells area', () => {
render(<GameWithHooks />);

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

expect(screen.getAllByRole('cell', { name: String(1) })).toHaveLength(1);
});
it('Cell click works fine', () => {
render(<GameWithHooks />);
userEvent.click(screen.getByTestId('0,0'));
expect(mockOnClick).toHaveBeenCalled();
});
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);

userEvent.selectOptions(screen.getByRole('combobox'), 'intermediate');
expect(screen.getAllByRole('cell')).toHaveLength(256);

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

userEvent.selectOptions(screen.getByRole('combobox'), 'expert');
expect(screen.getAllByRole('cell')).toHaveLength(484);

userEvent.click(screen.getByTestId('21,21'));
expect(screen.getAllByRole('cell', { name: String(e) })).toHaveLength(1);
expect(screen.getAllByRole('cell', { name: String(1) })).toHaveLength(2);
expect(screen.getAllByRole('cell', { name: String(2) })).toHaveLength(1);
});
it('onReset game handler', () => {
render(<GameWithHooks />);
expect(screen.getAllByRole('cell', { name: String(h) })).toHaveLength(81);

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.getByRole('button'));
expect(screen.getAllByRole('cell', { name: String(h) })).toHaveLength(81);
});
it('Reset handler works fine', () => {
render(<GameWithHooks />);
userEvent.click(screen.getByRole('button'));
expect(mockOnReset).toHaveBeenCalled();
});
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();
});
it('Change level works fine', () => {
render(<GameWithHooks />);
userEvent.selectOptions(screen.getByRole('combobox'), 'intermediate');
expect(mockOnChangeLevel).toHaveBeenCalled();
});
it('Game over reset the game state', () => {
render(<GameWithHooks />);
userEvent.click(screen.getByText('🙁'));
expect(mockOnReset).toHaveBeenCalled();
});
});
Loading