diff --git a/src/components/Navbar/Dice/Dice.jsx b/src/components/Navbar/Dice/Dice.jsx index dfd1630..ff5a4d5 100644 --- a/src/components/Navbar/Dice/Dice.jsx +++ b/src/components/Navbar/Dice/Dice.jsx @@ -1,24 +1,28 @@ import React, { useContext } from 'react'; import { SocketContext } from '../../../App'; import images from '../../../constants/diceImages'; +import styles from './Dice.module.css'; -const Dice = ({ rolledNumber, nowMoving, color, movingPlayer }) => { +const Dice = ({ rolledNumber, nowMoving, playerColor, movingPlayer }) => { const socket = useContext(SocketContext); - const handleRoll = () => { + const handleClick = () => { socket.emit('game:roll'); }; + const isCurrentPlayer = movingPlayer === playerColor; + const hasRolledNumber = rolledNumber !== null; + return ( -
- {movingPlayer === color ? ( - rolledNumber ? ( - {rolledNumber} - ) : nowMoving ? ( - roll - ) : null - ) : null} +
+ {isCurrentPlayer && + (hasRolledNumber ? ( + {rolledNumber} + ) : ( + nowMoving && roll + ))}
); }; + export default Dice; diff --git a/src/components/Navbar/Dice/Dice.module.css b/src/components/Navbar/Dice/Dice.module.css new file mode 100644 index 0000000..ce20014 --- /dev/null +++ b/src/components/Navbar/Dice/Dice.module.css @@ -0,0 +1,11 @@ +.container { + margin-left: 20px; + margin-right: 20px; + width: 50px; + height: 50px; +} +.container > img { + width: 100%; + height: 100%; + cursor: pointer; +} diff --git a/src/components/Navbar/Dice/Dice.test.js b/src/components/Navbar/Dice/Dice.test.js index e69de29..5de3150 100644 --- a/src/components/Navbar/Dice/Dice.test.js +++ b/src/components/Navbar/Dice/Dice.test.js @@ -0,0 +1,74 @@ +import React from 'react'; +import { render, screen, fireEvent } from '@testing-library/react'; +import '@testing-library/jest-dom'; +import Dice from './Dice'; +import { SocketContext } from '../../../App'; + +const mockSocket = { + emit: jest.fn(), +}; + +describe('Dice component', () => { + let props; + const MOVING_PLAYER = 'blue'; + const NOT_MOVING_PLAYER = 'red'; + const THIS_PLAYER_MOVING = true; + + beforeEach(() => { + props = { + rolledNumber: null, + nowMoving: false, + playerColor: '', + movingPlayer: '', + }; + }); + + it('should render correct rolledNumber next to moving player', () => { + props.rolledNumber = 5; + props.movingPlayer = MOVING_PLAYER; + props.playerColor = MOVING_PLAYER; + render(); + expect(screen.queryByAltText(props.rolledNumber)).toBeInTheDocument(); + }); + + it('should not render rolledNumber next to not moving player', () => { + props.rolledNumber = 5; + props.movingPlayer = MOVING_PLAYER; + props.playerColor = NOT_MOVING_PLAYER; + render(); + expect(screen.queryByAltText(props.rolledNumber)).not.toBeInTheDocument(); + }); + + it('should render roll icon next to moving player', () => { + props.rolledNumber = null; + props.movingPlayer = MOVING_PLAYER; + props.playerColor = MOVING_PLAYER; + props.nowMoving = THIS_PLAYER_MOVING; + render(); + expect(screen.queryByAltText('roll')).toBeInTheDocument(); + }); + + it('should not render roll icon next to not moving player', () => { + props.rolledNumber = null; + props.movingPlayer = MOVING_PLAYER; + props.playerColor = MOVING_PLAYER; + props.nowMoving = !THIS_PLAYER_MOVING; + render(); + expect(screen.queryByAltText('roll')).not.toBeInTheDocument(); + }); + + it('should send data on click', () => { + props.rolledNumber = null; + props.movingPlayer = MOVING_PLAYER; + props.playerColor = MOVING_PLAYER; + props.nowMoving = THIS_PLAYER_MOVING; + render( + + + + ); + const dice = screen.getByAltText('roll'); + fireEvent.click(dice); + expect(mockSocket.emit).toHaveBeenCalledWith('game:roll'); + }); +}); diff --git a/src/components/Navbar/Navbar.jsx b/src/components/Navbar/Navbar.jsx index 1a84d19..608b6e0 100644 --- a/src/components/Navbar/Navbar.jsx +++ b/src/components/Navbar/Navbar.jsx @@ -2,26 +2,27 @@ import React from 'react'; import Dice from './Dice/Dice'; import NameContainer from './NameContainer/NameContainer'; import ReadyButton from './ReadyButton/ReadyButton'; -import './Navbar.css'; import { PLAYER_COLORS } from '../../constants/colors'; import { useContext } from 'react'; import { PlayerDataContext } from '../../App'; +import styles from './Navbar.module.css'; const Navbar = ({ players, started, time, isReady, rolledNumber, nowMoving, movingPlayer }) => { const context = useContext(PlayerDataContext); + const diceProps = { + rolledNumber, + nowMoving, + movingPlayer, + }; + return ( <> {players.map((player, index) => ( -
+
- - {context.color === player.color || (!started && )} + {started ? : null} + {context.color === player.color && !started ? : null}
))} diff --git a/src/components/Navbar/Navbar.css b/src/components/Navbar/Navbar.module.css similarity index 73% rename from src/components/Navbar/Navbar.css rename to src/components/Navbar/Navbar.module.css index 6dc92fd..e574f19 100644 --- a/src/components/Navbar/Navbar.css +++ b/src/components/Navbar/Navbar.module.css @@ -1,13 +1,4 @@ -.dice-container { - margin-left: 20px; - margin-right: 20px; - width: 50px; - height: 50px; -} -.roll { - cursor: pointer; -} -.player-container { +.playerContainer { display: flex; align-items: center; flex-direction: row; diff --git a/src/components/Navbar/Navbar.test.js b/src/components/Navbar/Navbar.test.js new file mode 100644 index 0000000..a9f7457 --- /dev/null +++ b/src/components/Navbar/Navbar.test.js @@ -0,0 +1,72 @@ +import React from 'react'; +import { render, screen } from '@testing-library/react'; +import '@testing-library/jest-dom'; +import Navbar from './Navbar'; +import { PlayerDataContext } from '../../App'; + +const mockPlayers = [ + { name: 'Player1', color: 'red' }, + { name: 'Player2', color: 'blue' }, +]; + +const mockPlayerData = { + color: 'red', +}; + +jest.mock('./NameContainer/NameContainer.jsx', () => () => { + return ; +}); + +jest.mock('./ReadyButton/ReadyButton.jsx', () => () => { + return ; +}); + +jest.mock('./Dice/Dice.jsx', () => () => { + return ; +}); + +const setup = props => { + props.players = mockPlayers; + return render( + + + + ); +}; + +describe('Navbar component', () => { + it('should render NameContainer for each player', () => { + setup({ + started: true, + }); + expect(screen.getAllByTestId('name-container')).toHaveLength(mockPlayers.length); + }); + + it('should render Dice when started is true', () => { + setup({ + started: true, + }); + expect(screen.getAllByTestId('dice-container')).toHaveLength(mockPlayers.length); + }); + + it('should not render ReadyButton when started is true', () => { + setup({ + started: true, + }); + expect(screen.queryByTestId('ready-button')).toBeNull(); + }); + + it('should render ReadyButton when started is false', () => { + setup({ + started: false, + }); + expect(screen.getByTestId('ready-button')).toBeInTheDocument(); + }); + + it('does not render Dice when started is false', () => { + setup({ + started: false, + }); + expect(screen.queryByTestId('dice-container')).toBeNull(); + }); +}); diff --git a/src/index.css b/src/index.css index f4893af..cae3a75 100644 --- a/src/index.css +++ b/src/index.css @@ -20,10 +20,6 @@ canvas { border-radius: 15px; border: 2px solid black; } -.dice-container > img { - width: 50px; - height: 50px; -} .navbar-container { display: flex; flex-direction: row;