Skip to content

Commit 57ecd1c

Browse files
committed
Add a behavior test on game happy path
1 parent 92893c2 commit 57ecd1c

File tree

6 files changed

+119
-10
lines changed

6 files changed

+119
-10
lines changed

package-lock.json

+39-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "crokinolesque",
3-
"version": "0.1.0",
3+
"version": "1.0.0",
44
"private": true,
55
"dependencies": {
66
"@emotion/react": "^11.11.4",
@@ -12,8 +12,10 @@
1212
"react-scripts": "5.0.1"
1313
},
1414
"devDependencies": {
15+
"@babel/plugin-proposal-private-property-in-object": "^7.21.11",
1516
"@testing-library/jest-dom": "^6.4.6",
1617
"@testing-library/react": "^16.0.0",
18+
"@testing-library/user-event": "^14.5.2",
1719
"@types/jest": "^29.5.12",
1820
"@types/node": "^16.18.101",
1921
"@types/react": "^18.3.3",

src/App.test.tsx

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import {render, screen} from "@testing-library/react";
2+
import userEvent from '@testing-library/user-event';
3+
import App from "./App";
4+
5+
test('the app is rendered', () => {
6+
render(<App />);
7+
expect(screen.getByText('Crokinolesque')).toBeInTheDocument();
8+
});
9+
10+
test('happy path', async () => {
11+
render(<App />);
12+
13+
await player1WillBeCalled('Player 1');
14+
await player2WillBeCalled('Player 2');
15+
16+
await userEvent.click(screen.getByRole('button', {name: 'Commencer'}));
17+
18+
expect(screen.getByText(/Le premier joueur sera Player [1,2]/i)).toBeInTheDocument();
19+
20+
await userScoresPoints('Player 1', 20);
21+
gameScoreShouldNowBe(20, 0);
22+
23+
await userScoresPoints('Player 2', 40);
24+
gameScoreShouldNowBe(20, 40);
25+
26+
await userEvent.click(screen.getByRole('button', {name: 'Egalité'}));
27+
gameScoreShouldNowBe(20, 40);
28+
29+
await userScoresPoints('Player 1', 80);
30+
31+
expect(screen.getByText(/Player 1 a gagné 100 - 40/i)).toBeInTheDocument();
32+
expect(screen.getByRole('button', {name: 'Rejouer'})).toBeInTheDocument();
33+
expect(screen.getAllByTestId('roundHistoryEntry').length).toBe(4);
34+
35+
displayedRoundHistoryShouldBe([
36+
'1 20 0',
37+
'2 20 40',
38+
'3 20 40',
39+
'4 100 40'
40+
]);
41+
});
42+
43+
async function player1WillBeCalled(playerName: string) {
44+
const player1Input = screen.getByLabelText('Joueur 1');
45+
await userEvent.click(player1Input);
46+
await userEvent.type(player1Input, playerName);
47+
}
48+
49+
async function player2WillBeCalled(playerName: string) {
50+
const player2Input = screen.getByLabelText('Joueur 2');
51+
await userEvent.click(player2Input);
52+
await userEvent.type(player2Input, playerName);
53+
}
54+
55+
async function userScoresPoints(playerName: string, points: number) {
56+
const playerButton = screen.getByRole('button', {name: playerName});
57+
await userEvent.click(playerButton);
58+
const scoreInput = screen.getByLabelText('Points');
59+
await userEvent.click(scoreInput);
60+
await userEvent.type(scoreInput, points.toString());
61+
await userEvent.click(screen.getByRole('button', {name: 'Valider'}));
62+
}
63+
64+
function gameScoreShouldNowBe(player1Score: number, player2Score: number) {
65+
expect(screen.getByText(`Player 1 : ${player1Score}`)).toBeInTheDocument();
66+
expect(screen.getByText(`Player 2 : ${player2Score}`)).toBeInTheDocument();
67+
}
68+
69+
function displayedRoundHistoryShouldBe(expected: string[]) {
70+
const roundHistoryFromDOM = screen.getAllByTestId('roundHistoryEntry').map(entry => entry.textContent as string);
71+
const expectedFormatted = expected.map(entry => entry.replace(/\s/g, ''));
72+
expect(roundHistoryFromDOM).toMatchObject(expectedFormatted);
73+
}

src/components/App.tsx src/App.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import CssBaseline from '@mui/material/CssBaseline';
55
import IconButton from '@mui/material/IconButton';
66
import LightModeIcon from '@mui/icons-material/LightMode';
77
import DarkModeIcon from '@mui/icons-material/DarkMode';
8-
import Game from './Game';
9-
import {GameProvider} from "../contexts/gameContext";
8+
import Game from './components/Game';
9+
import {GameProvider} from "./contexts/gameContext";
1010

1111
type PaletteMode = 'light' | 'dark';
1212

src/components/RoundsHistory.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export default function RoundsHistory({ rounds, player1Name, player2Name }: Prop
2424
</TableHead>
2525
<TableBody>
2626
{rounds.map((round, index) => (
27-
<TableRow key={index} sx={index % 2 !== 0 ? { backgroundColor: 'action.hover' } : {}}>
27+
<TableRow key={index} sx={index % 2 !== 0 ? { backgroundColor: 'action.hover' } : {}} data-testid="roundHistoryEntry">
2828
<TableCell align="center">{index + 1}</TableCell>
2929
<TableCell align="center">{round.player1Score}</TableCell>
3030
<TableCell align="center">{round.player2Score}</TableCell>

src/index.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from 'react';
22
import ReactDOM from 'react-dom/client';
3-
import App from './components/App';
3+
import App from './App';
44

55
const root = ReactDOM.createRoot(
66
document.getElementById('root') as HTMLElement

0 commit comments

Comments
 (0)