Skip to content
Open
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
6 changes: 5 additions & 1 deletion src/components/Players/CardPicker/CardConfigs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,11 @@ export const getRandomEmoji = () => {

export const getCustomCards = (values: string[]) => {
const customCards: CardConfig[] = customCardsTemplate;
values.forEach((value, index) => (customCards[index].displayValue = value));
const isEveryCardValueNumber = values.every((value) => !isNaN(Number(value)));
values.forEach((value, index) => {
customCards[index].displayValue = value;
if (isEveryCardValueNumber) customCards[index].value = Number(value);
});

return customCards.filter(
(card) => card.displayValue !== undefined && card.displayValue.trim() !== '',
Expand Down
2 changes: 1 addition & 1 deletion src/components/Players/CardPicker/CardPicker.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import * as playersService from '../../../service/players';
import { Game, GameType } from '../../../types/game';
import { Player } from '../../../types/player';
import { Status } from '../../../types/status';
import { getCards, getCustomCards } from './CardConfigs';
import { getCards } from './CardConfigs';
import { CardPicker } from './CardPicker';
import * as cardConfigs from './CardConfigs';

Expand Down
25 changes: 21 additions & 4 deletions src/components/Poker/GameController/GameController.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
id: 'xyz',
name: 'testGame',
cards: [
{ value: 1, displayValue: '1', color: 'red' },
{ value: 2, displayValue: '2', color: 'blue' },
{ value: 3, displayValue: '3', color: 'green' },
{ value: 1, displayValue: 'A', color: 'red' },
{ value: 2, displayValue: 'B', color: 'blue' },
{ value: 3, displayValue: 'C', color: 'green' },
],
createdBy: 'someone',
createdAt: new Date(),
Expand Down Expand Up @@ -84,9 +84,26 @@
currentPlayerId={mockCurrentPlayerId}
/>,
);

expect(screen.queryByText('Average:')).not.toBeInTheDocument();
});
it('should display game average for Custom GameType', () => {
const mockGameWithNumericDisplayValues = { ...mockGame };
mockGameWithNumericDisplayValues.cards = [
{ value: 1, displayValue: '1', color: 'red' },
{ value: 2, displayValue: '2', color: 'blue' },
{ value: 3, displayValue: '3', color: 'green' },
]

render(
<GameController
game={{ ...mockGameWithNumericDisplayValues, gameType: GameType.Custom }}
currentPlayerId={mockCurrentPlayerId}
/>,
);

expect(screen.queryByText('Average:')).toBeInTheDocument();

Check failure on line 105 in src/components/Poker/GameController/GameController.test.tsx

View workflow job for this annotation

GitHub Actions / build (18.x)

Use `getBy*` queries rather than `queryBy*` for checking element is present
});
it('should display exit option', () => {
render(<GameController game={mockGame} currentPlayerId={mockCurrentPlayerId} />);

Expand Down
5 changes: 4 additions & 1 deletion src/components/Poker/GameController/GameController.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { AlertDialog } from '../../../components/AlertDialog/AlertDialog';
import { finishGame, removeGame, resetGame, updateStoryName } from '../../../service/games';
import { Game, GameType } from '../../../types/game';
import { isModerator } from '../../../utils/isModerator';
import { isNumeric } from '../../../utils/isStringNumeric';
import './GameController.css';

interface GameControllerProps {
Expand All @@ -43,6 +44,8 @@ export const GameController: React.FC<GameControllerProps> = ({ game, currentPla
document.body.removeChild(dummy);
setShowCopiedMessage(true);
};
const canShowAverageForCustomGameType =
game.gameType === GameType.Custom && game.cards.every((card) => isNumeric(card.displayValue));

const leaveGame = () => {
history.push(`/`);
Expand All @@ -67,7 +70,7 @@ export const GameController: React.FC<GameControllerProps> = ({ game, currentPla
</Typography>
{game.gameType !== GameType.TShirt &&
game.gameType !== GameType.TShirtAndNumber &&
game.gameType !== GameType.Custom && (
(game.gameType !== GameType.Custom || canShowAverageForCustomGameType) && (
<>
<Divider className='GameControllerDivider' orientation='vertical' flexItem />
<Typography variant='subtitle1'>Average:</Typography>
Expand Down
53 changes: 53 additions & 0 deletions src/utils/isStringNumeric.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { isNumeric } from './isStringNumeric';

describe('isModerator', () => {
it('"1" must be numeric', () => {
const result = isNumeric('1');

expect(result).toBe(true);
});

it('"2" must be numeric', () => {
const result = isNumeric('2');

expect(result).toBe(true);
});

it('"0" must be numeric', () => {
const result = isNumeric('0');

expect(result).toBe(true);
});


it('"-1" must be numeric', () => {
const result = isNumeric('-1');

expect(result).toBe(true);
});

it('"1.0" must be numeric', () => {
const result = isNumeric('1.0');

expect(result).toBe(true);
});

it('"1 1" must not be numeric', () => {
const result = isNumeric('1 1');

expect(result).toBe(false);
});


it('"z" must not be numeric', () => {
const result = isNumeric('z');

expect(result).toBe(false);
});

it('"1b" must not be numeric', () => {
const result = isNumeric('1b');

expect(result).toBe(false);
});
});
6 changes: 6 additions & 0 deletions src/utils/isStringNumeric.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const isNumeric = (incomingNumber: string | number) => {
if (typeof incomingNumber !== 'string' && typeof incomingNumber !== 'number') return false;
if (typeof incomingNumber === 'string' && incomingNumber.trim() === '') return false;
const number = Number(incomingNumber);
return Number.isFinite(number);
};
Loading