Skip to content
Snippets Groups Projects
Commit 313a4851 authored by HOLZINGER Ulysse's avatar HOLZINGER Ulysse
Browse files

Test unitaires pour App, Board et Cell

parent 16702930
Branches
No related tags found
No related merge requests found
import React, { useState } from 'react'; import React, { useEffect, useState } from 'react';
import Board from './components/Board'; import Board from './components/Board';
import './styles.scss'; import './styles.scss';
import confettis from './assets/confetti-4.gif' import confettis from './assets/confetti-4.gif'
...@@ -13,6 +13,7 @@ const App = () => { ...@@ -13,6 +13,7 @@ const App = () => {
const [winner, setWinner] = useState(0) const [winner, setWinner] = useState(0)
const [gameStatus,setGameStatus] = useState(null) const [gameStatus,setGameStatus] = useState(null)
const [reversed, setReversed]=useState(null) const [reversed, setReversed]=useState(null)
const [percentage, setPercentage] = useState(20)
function ResetGame(){ function ResetGame(){
setBoard(() => ( setBoard(() => (
...@@ -78,22 +79,20 @@ const App = () => { ...@@ -78,22 +79,20 @@ const App = () => {
if (isBoardFull(newBoard)) if (isBoardFull(newBoard))
setGameStatus('draw') setGameStatus('draw')
} }
setBoard(newBoard) setBoard(newBoard)
if (Math.random() < 0.2) { if (Math.random() < percentage/100) {
const randomIndex = Math.floor(Math.random() * 7) const randomIndex = Math.floor(Math.random() * 7)
reverseColumn(randomIndex) reverseColumn(randomIndex)
setReversed(randomIndex) setReversed(randomIndex)
} }
setCurrentPlayer(currentPlayer === 1 ? -1 : 1) setCurrentPlayer(currentPlayer === 1 ? -1 : 1)
}; }
function reverseColumn(colIndex) { function reverseColumn(colIndex) {
const newBoard = [...board] const newBoard = [...board]
let column = newBoard.map(row => row[colIndex]).reverse().filter(element=>element !== 0); let column = newBoard.map(row => row[colIndex]).reverse().filter(element=>element !== 0);
while(column.length < 6) while(column.length < 6)
column.push(0) column.push(0)
console.log(column)
for (let i = 0; i < 6; i++) for (let i = 0; i < 6; i++)
newBoard[i][colIndex] = column[i] newBoard[i][colIndex] = column[i]
setBoard(newBoard) setBoard(newBoard)
...@@ -102,6 +101,8 @@ const App = () => { ...@@ -102,6 +101,8 @@ const App = () => {
return ( return (
<div className="app"> <div className="app">
<h1>Puissance 4</h1> <h1>Puissance 4</h1>
<div>Pourcentage de chance que le reverse s'applique : {percentage}%</div>
<input type="range" min="0" max="100" value={percentage} onChange={(e)=>{setPercentage(e.target.value)}}/>
{ {
gameStatus === 'win' || gameStatus === 'draw' ? ( gameStatus === 'win' || gameStatus === 'draw' ? (
<> <>
...@@ -110,7 +111,7 @@ const App = () => { ...@@ -110,7 +111,7 @@ const App = () => {
gameStatus === 'win' ? (<h1>Gagnant : joueur {winner===1? '1':'2'}</h1>) : (<h1>Egalité</h1>) gameStatus === 'win' ? (<h1>Gagnant : joueur {winner===1? '1':'2'}</h1>) : (<h1>Egalité</h1>)
} }
<button onClick={()=>{ResetGame()}}>Recommencer</button> <button data-testid={'restart'} onClick={()=>{ResetGame()}}>Recommencer</button>
</>) : ( </>) : (
<> <>
<div>Au tour du joueur {currentPlayer===1? '1':'2'}</div> <div>Au tour du joueur {currentPlayer===1? '1':'2'}</div>
......
import { render, screen } from '@testing-library/react'; import React from 'react';
import { render, fireEvent, screen, queryByText, getByTestId } from '@testing-library/react';
import App from './App'; import App from './App';
test('renders learn react link', () => { const { getAllByTestId, getByText } = screen;
test('handles player click and resets the game', () => {
const { getAllByTestId } = screen;
const { getByText, getByTestId } = render(<App />);
expect(getByText('Puissance 4')).toBeInTheDocument();
fireEvent.click(getAllByTestId('cell')[0]);
const recommencerButton = queryByText(document.body, 'Recommencer');
if (recommencerButton) {
fireEvent.click(recommencerButton);
}
});
test('handles winning scenario', () => {
render(<App />);
fireEvent.click(getAllByTestId('cell')[0]);
fireEvent.click(getAllByTestId('cell')[1]);
fireEvent.click(getAllByTestId('cell')[0]);
fireEvent.click(getAllByTestId('cell')[1]);
fireEvent.click(getAllByTestId('cell')[0]);
fireEvent.click(getAllByTestId('cell')[1]);
fireEvent.click(getAllByTestId('cell')[0]);
expect(getByText('Gagnant : joueur 1')).toBeInTheDocument();
});
test('handles draw scenario', () => {
render(<App />);
fireEvent.change(screen.getByRole('slider'), { target: { value: 0 } });
for(let i = 0; i < 3; i++) {
fireEvent.click(getAllByTestId('cell')[0]);
fireEvent.click(getAllByTestId('cell')[1]);
fireEvent.click(getAllByTestId('cell')[2]);
fireEvent.click(getAllByTestId('cell')[3]);
fireEvent.click(getAllByTestId('cell')[4]);
fireEvent.click(getAllByTestId('cell')[5]);
fireEvent.click(getAllByTestId('cell')[6]);
}
for(let i = 0; i < 2; i++) {
fireEvent.click(getAllByTestId('cell')[1]);
fireEvent.click(getAllByTestId('cell')[2]);
fireEvent.click(getAllByTestId('cell')[3]);
fireEvent.click(getAllByTestId('cell')[4]);
fireEvent.click(getAllByTestId('cell')[5]);
fireEvent.click(getAllByTestId('cell')[6]);
}
fireEvent.click(getAllByTestId('cell')[6]);
fireEvent.click(getAllByTestId('cell')[0]);
fireEvent.click(getAllByTestId('cell')[0]);
fireEvent.click(getAllByTestId('cell')[5]);
fireEvent.click(getAllByTestId('cell')[4]);
fireEvent.click(getAllByTestId('cell')[3]);
fireEvent.click(getAllByTestId('cell')[2]);
fireEvent.click(getAllByTestId('cell')[1]);
fireEvent.click(getAllByTestId('cell')[0]);
expect(getByText('Egalité')).toBeInTheDocument();
});
test('handles reverse column scenario', () => {
render(<App />);
fireEvent.change(screen.getByRole('slider'), { target: { value: 100 } });
fireEvent.click(getAllByTestId('cell')[0]);
expect(getByText(/Reverse sur la colonne/)).toBeInTheDocument();
});
test('changes reversibility percentage', () => {
const { getByRole, getByText } = screen;
render(<App />);
fireEvent.change(getByRole('slider'), { target: { value: 50 } });
expect(getByText('Pourcentage de chance que le reverse s\'applique : 50%')).toBeInTheDocument();
});
test('alternates player turns', () => {
render(<App />);
fireEvent.click(getAllByTestId('cell')[0]);
fireEvent.click(getAllByTestId('cell')[1]);
fireEvent.click(getAllByTestId('cell')[2]);
expect(getByText('Au tour du joueur 2')).toBeInTheDocument();
});
test('handles click on a full column', () => {
render(<App />); render(<App />);
const linkElement = screen.getByText(/learn react/i);
expect(linkElement).toBeInTheDocument(); for (let i = 0; i < 6; i++) {
fireEvent.click(getAllByTestId('cell')[0]);
}
fireEvent.click(getAllByTestId('cell')[0]);
expect(getByText('Au tour du joueur 1')).toBeInTheDocument();
});
test('does not display messages during the game', () => {
render(<App />);
expect(queryByText(document.body,'Gagnant : joueur 1')).toBeNull();
expect(queryByText(document.body,'Egalité')).toBeNull();
});
test('handles extreme reversibility percentages', () => {
const { getByRole, getByText } = screen;
render(<App />);
fireEvent.change(getByRole('slider'), { target: { value: 0 } });
fireEvent.click(getAllByTestId('cell')[0]);
expect(queryByText(document.body,/Reverse sur la colonne/)).toBeNull();
fireEvent.change(getByRole('slider'), { target: { value: 100 } });
fireEvent.click(getAllByTestId('cell')[0]);
expect(getByText(/Reverse sur la colonne/)).toBeInTheDocument();
}); });
connect4/src/assets/gettyimages-165727231-612x612.jpg

38.3 KiB

connect4/src/assets/jetonchad.png

51.8 KiB

connect4/src/assets/punaise-lit-monaco.jpeg

99.5 KiB

connect4/src/assets/sticker-poker-jeton-jaune.jpg

79.1 KiB

...@@ -21,4 +21,3 @@ const Board = ({ board, handleClick }) => { ...@@ -21,4 +21,3 @@ const Board = ({ board, handleClick }) => {
}; };
export default Board; export default Board;
...@@ -7,4 +7,14 @@ ...@@ -7,4 +7,14 @@
flex-direction: row; flex-direction: row;
} }
&.updated{
.row{
.cell{
.jeton{
transform: translateY(-100px);
}
}
}
}
} }
\ No newline at end of file
import React from 'react'; import React from 'react';
import './Cell.scss' import './Cell.scss'
import player1 from '../assets/jetonchad.png' import player1 from '../assets/gettyimages-165727231-612x612.jpg'
import player2 from '../assets/punaise-lit-monaco.jpeg' import player2 from '../assets/sticker-poker-jeton-jaune.jpg'
const Cell = ({ value, onClick }) => { const Cell = ({ value, onClick }) => {
return ( return (
<div className="cell" onClick={onClick}> <div className="cell" onClick={onClick} data-testid="cell">
{ {
value !==0 && <img className='jeton' src={value===1 ? player1 : player2} /> value !==0 && <img className='jeton' src={value===1 ? player1 : player2} />
} }
......
...@@ -6,10 +6,12 @@ ...@@ -6,10 +6,12 @@
display: flex; display: flex;
justify-content: center; justify-content: center;
align-items: center; align-items: center;
transition: all 0.1s; transition: transform 0.3s ease;
.jeton{ .jeton{
width: 100%; width: 100%;
border-radius: 50%; border-radius: 50%;
height: 100%; height: 100%;
object-fit: cover;
pointer-events: none;
} }
} }
\ No newline at end of file
.App { .app {
text-align: center; text-align: center;
}
.App-logo {
height: 40vmin;
pointer-events: none;
}
@media (prefers-reduced-motion: no-preference) {
.App-logo {
animation: App-logo-spin infinite 20s linear;
}
}
.App-header {
background-color: #282c34;
min-height: 100vh;
display: flex; display: flex;
flex-direction: column; flex-direction: column;
align-items: center; align-items: center;
justify-content: center; height: 100vh;
font-size: calc(10px + 2vmin);
color: white;
}
.App-link {
color: #61dafb;
}
@keyframes App-logo-spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
} }
// Board.test.js
import React from 'react';
import { render, fireEvent } from '@testing-library/react';
import Board from '../components/Board';
// Exemple de test simple
test('renders Board component', () => {
const board = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]; // Exemple de tableau
render(<Board board={board} handleClick={() => {}} />);
});
// Exemple de test avec interaction utilisateur
test('calls handleClick prop when a cell is clicked', () => {
const handleClickMock = jest.fn();
const board = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]; // Exemple de tableau
const { container } = render(<Board board={board} handleClick={handleClickMock} />);
// Simule un clic sur une cellule
fireEvent.click(container.querySelector('.cell'));
// Vérifie que la fonction handleClick a été appelée
expect(handleClickMock).toHaveBeenCalledTimes(1);
});
// Ajoutez d'autres tests en fonction de la complexité de votre composant
import React from 'react';
import { render, fireEvent } from '@testing-library/react';
import Cell from '../components/Cell';
test('renders Cell component', () => {
render(<Cell value={0} onClick={() => {}} />);
});
test('calls onClick prop when clicked', () => {
const onClickMock = jest.fn();
const { container } = render(<Cell value={0} onClick={onClickMock} />);
fireEvent.click(container.firstChild);
expect(onClickMock).toHaveBeenCalledTimes(1);
});
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment