|
| 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 | +} |
0 commit comments