Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: harmonizes playing card typings #126

Closed
wants to merge 1 commit into from
Closed
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { cleanup, render } from '@testing-library/react';
import React from 'react';
import { PlayingCard, PlayingCardSuit, PlayingCardName } from '@models/PlayingCard';
import { PlayingCard } from '@models/PlayingCard';
import { PlayingCardFrontFace } from './PlayingCardFrontFace';

describe('PlayingCardFrontFace', () => {
Expand All @@ -12,8 +12,8 @@ describe('PlayingCardFrontFace', () => {
const card: PlayingCard = {
id: '1',
type: 'PlayingCard',
suit: PlayingCardSuit.Hearts,
name: PlayingCardName.Five,
suit: 'hearts',
name: '5',
};

const { getByTestId } = render(<PlayingCardFrontFace card={card} />);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export interface PlayingCardFrontFaceProps {
}

export const PlayingCardFrontFace: FC<PlayingCardFrontFaceProps> = ({ card }) => {
const cardString = `${card?.suit?.toLowerCase()}_${card?.name?.toLowerCase()}`;
const cardString = `${card.suit.toLowerCase()}_${card.name.toLowerCase()}`;

return <SvgPlayingCard card={cardString} data-testid="PlayingCardFrontFace" />;
};
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import { render } from '@testing-library/react';
import { UnoCard } from '@models/UnoCard';
import { PlayingCard, PlayingCardName, PlayingCardSuit } from '@models/PlayingCard';
import { PlayingCard } from '@models/PlayingCard';
import { CardRenderer } from '@models/CardRenderer';
import { CardFaceRenderer } from './CardFaceRenderer';

Expand All @@ -15,8 +15,8 @@ describe('CardFaceRenderer', () => {
const playingCard: PlayingCard = {
id: '2',
type: 'PlayingCard',
name: PlayingCardName.Ace,
suit: PlayingCardSuit.Clubs,
name: '1',
suit: 'clubs',
};
it('should display an UnoCardFace', () => {
const { getByTestId } = render(<CardFaceRenderer card={unoCard} />);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable max-lines */
import { PlayingCard, PlayingCardSuit, PlayingCardName } from '@models/PlayingCard';
import { PlayingCard } from '@models/PlayingCard';
import { cleanup, fireEvent, render, screen } from '@testing-library/react';
import React from 'react';
import { CardPile } from './CardPile';
Expand Down Expand Up @@ -45,8 +45,8 @@ describe('CardPile', () => {
const card: PlayingCard = {
id: '1',
type: 'PlayingCard',
suit: PlayingCardSuit.Spades,
name: PlayingCardName.Ace,
suit: 'spades',
name: '1',
};
const { getByTestId } = render(<CardPile cards={[card]} position={{ x: 0, y: 0 }} width={40} height={60} />);
cardPile = getByTestId('CardPile');
Expand Down Expand Up @@ -79,14 +79,14 @@ describe('CardPile', () => {
const card: PlayingCard = {
id: '1',
type: 'PlayingCard',
suit: PlayingCardSuit.Spades,
name: PlayingCardName.Ace,
suit: 'spades',
name: '1',
};
const otherCard: PlayingCard = {
id: '1',
type: 'PlayingCard',
suit: PlayingCardSuit.Hearts,
name: PlayingCardName.Three,
suit: 'hearts',
name: '3',
};
const { getAllByTestId, getByTestId } = render(
<CardPile cards={[card, otherCard]} position={{ x: 0, y: 0 }} width={40} height={60} />
Expand Down Expand Up @@ -119,10 +119,10 @@ describe('CardPile', () => {
describe('when rendering more than two cards', () => {
it('should render the custom cards', () => {
const cards: PlayingCard[] = [
{ id: '2', type: 'PlayingCard', suit: PlayingCardSuit.Spades, name: PlayingCardName.Ace },
{ id: '4', type: 'PlayingCard', suit: PlayingCardSuit.Hearts, name: PlayingCardName.Ace },
{ id: '6', type: 'PlayingCard', suit: PlayingCardSuit.Diamonds, name: PlayingCardName.Ace },
{ id: '8', type: 'PlayingCard', suit: PlayingCardSuit.Clubs, name: PlayingCardName.Ace },
{ id: '2', type: 'PlayingCard', suit: 'spades', name: '1' },
{ id: '4', type: 'PlayingCard', suit: 'hearts', name: '1' },
{ id: '6', type: 'PlayingCard', suit: 'diamonds', name: '1' },
{ id: '8', type: 'PlayingCard', suit: 'clubs', name: '1' },
];

const { getAllByTestId } = render(<CardPile cards={cards} position={{ x: 0, y: 0 }} width={40} height={60} />);
Expand Down Expand Up @@ -165,8 +165,8 @@ describe('CardPile', () => {
const card: PlayingCard = {
id: '1',
type: 'PlayingCard',
suit: PlayingCardSuit.Spades,
name: PlayingCardName.Ace,
suit: 'spades',
name: '1',
};

render(
Expand Down Expand Up @@ -205,8 +205,8 @@ describe('CardPile', () => {
const card: PlayingCard = {
id: '1',
type: 'PlayingCard',
suit: PlayingCardSuit.Spades,
name: PlayingCardName.Ace,
suit: 'spades',
name: '1',
};

render(
Expand Down
32 changes: 14 additions & 18 deletions packages/components/src/components/organisms/Table/Table.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import React from 'react';
import * as cardStyleUtils from '@utils/card-style';
import { fireEvent, screen } from '@testing-library/react';
import * as reactRedux from 'react-redux';
import { PlayingCardSuit, PlayingCardName, PlayingCard } from '@models/PlayingCard';
import { PlayingCard } from '@models/PlayingCard';
import { UnoCard } from '@models/UnoCard';
import { Card } from '@models/Card';
import { CardTypeStyle } from '@utils/card-style';
Expand Down Expand Up @@ -60,8 +60,8 @@ describe('Table', () => {
const playingCard: PlayingCard = {
id: '2',
type: 'PlayingCard',
suit: PlayingCardSuit.Spades,
name: PlayingCardName.Two,
suit: 'spades',
name: '2',
};
const renderRoot = render(<Table height={400} width={600} />, {
initialState: {
Expand Down Expand Up @@ -122,7 +122,7 @@ describe('Table', () => {
table: {
cards: {
2: {
cards: { id: '2', type: 'PlayingCard', suit: PlayingCardSuit.Spades, name: PlayingCardName.Two },
cards: { id: '2', type: 'PlayingCard', suit: 'spades', name: '2' },
isFaceUp: false,
position: { x: 0, y: 0 },
},
Expand Down Expand Up @@ -152,7 +152,7 @@ describe('Table', () => {
table: {
cards: {
2: {
cards: { id: '2', type: 'PlayingCard', suit: PlayingCardSuit.Spades, name: PlayingCardName.Two },
cards: { id: '2', type: 'PlayingCard', suit: 'spades', name: '2' },
isFaceUp: false,
position: { x: 0, y: 0 },
},
Expand All @@ -176,9 +176,7 @@ describe('Table', () => {
describe('on rendering PlayingCards', () => {
let getByTestId;
let getAllByTestId;
const cards: PlayingCard[] = [
{ id: '1', type: 'PlayingCard', suit: PlayingCardSuit.Spades, name: PlayingCardName.Two },
];
const cards: PlayingCard[] = [{ id: '1', type: 'PlayingCard', suit: 'spades', name: '2' }];
beforeEach(() => {
customCardStyleFn.mockReset();
customCardStyleFn.mockReturnValue({
Expand Down Expand Up @@ -246,9 +244,9 @@ describe('Table', () => {
cards: {
2: {
cards: [
{ id: '5', type: 'PlayingCard', suit: PlayingCardSuit.Spades, name: PlayingCardName.Two },
{ id: '7', type: 'PlayingCard', suit: PlayingCardSuit.Hearts, name: PlayingCardName.Three },
{ id: '14', type: 'PlayingCard', suit: PlayingCardSuit.Diamonds, name: PlayingCardName.Four },
{ id: '5', type: 'PlayingCard', suit: 'spades', name: '2' },
{ id: '7', type: 'PlayingCard', suit: 'hearts', name: '3' },
{ id: '14', type: 'PlayingCard', suit: 'diamonds', name: '4' },
],
isFaceUp: false,
position: { x: 0, y: 0 },
Expand Down Expand Up @@ -279,9 +277,9 @@ describe('Table', () => {
cards: {
2: {
cards: [
{ id: '31', type: 'PlayingCard', suit: PlayingCardSuit.Spades, name: PlayingCardName.Two },
{ id: '41', type: 'PlayingCard', suit: PlayingCardSuit.Hearts, name: PlayingCardName.Three },
{ id: '61', type: 'PlayingCard', suit: PlayingCardSuit.Diamonds, name: PlayingCardName.Four },
{ id: '31', type: 'PlayingCard', suit: 'spades', name: '2' },
{ id: '41', type: 'PlayingCard', suit: 'hearts', name: '3' },
{ id: '61', type: 'PlayingCard', suit: 'diamonds', name: '4' },
],
isFaceUp: false,
position: { x: 0, y: 0 },
Expand Down Expand Up @@ -311,7 +309,7 @@ describe('Table', () => {
table: {
cards: {
5: {
cards: [{ id: '1', type: 'PlayingCard', suit: PlayingCardSuit.Spades, name: PlayingCardName.Two }],
cards: [{ id: '1', type: 'PlayingCard', suit: 'spades', name: '2' }],
isFaceUp: false,
position: { x: 0, y: 0 },
},
Expand All @@ -337,9 +335,7 @@ describe('Table', () => {
const customRenderFnSpy = jest.fn((_a: { card: Card; isBack?: boolean; currentElement?: JSX.Element }) => (
<div data-testid="SPY-FACE" />
));
const cards: PlayingCard[] = [
{ id: '1', type: 'PlayingCard', suit: PlayingCardSuit.Spades, name: PlayingCardName.Two },
];
const cards: PlayingCard[] = [{ id: '1', type: 'PlayingCard', suit: 'spades', name: '2' }];
let getAllByTestId;

beforeEach(() => {
Expand Down
39 changes: 18 additions & 21 deletions packages/components/src/models/PlayingCard.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,24 @@
import type { Card } from '@models/Card';

export enum PlayingCardSuit {
Spades = 'Spades',
Hearts = 'Hearts',
Diamonds = 'Diamonds',
Clubs = 'Clubs',
}
export const allPlayingCardSuits = ['spades', 'hearts', 'diamonds', 'clubs'] as const;
export type PlayingCardSuit = typeof allPlayingCardSuits[number];

export enum PlayingCardName {
Ace = '1',
Two = '2',
Three = '3',
Four = '4',
Five = '5',
Six = '6',
Seven = '7',
Eight = '8',
Nine = '9',
Ten = '10',
Jack = 'Jack',
Queen = 'Queen',
King = 'King',
}
export const allPlayingCardNames = [
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'10',
'jack',
'queen',
'king',
] as const;
export type PlayingCardName = typeof allPlayingCardNames[number];

const PLAYING_CARD_TYPE = 'PlayingCard' as const;
export interface PlayingCard extends Card {
Expand Down
26 changes: 13 additions & 13 deletions packages/components/src/store/slices/table/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable max-lines */
import { Position } from '@models/Position';
import { PlayingCard, PlayingCardName, PlayingCardSuit } from '@models/PlayingCard';
import { allPlayingCardNames, allPlayingCardSuits, PlayingCard } from '@models/PlayingCard';
import * as arrayUtils from '@utils/array-utils';
import tableReducer, {
addRandomPlayingCard,
Expand Down Expand Up @@ -48,8 +48,8 @@ describe('Cards reducer', () => {
expect(cardState.position.y).toBeLessThanOrEqual(140);

expect(cardId).toMatch(uuidRegExp);
expect(Object.values(PlayingCardSuit)).toContain(card.suit);
expect(Object.values(PlayingCardName)).toContain(card.name);
expect(allPlayingCardSuits).toContain(card.suit);
expect(allPlayingCardNames).toContain(card.name);
});

it('should generate a random card with default position', () => {
Expand All @@ -66,8 +66,8 @@ describe('Cards reducer', () => {
expect(cardState.position.y).toEqual(0);

expect(cardId).toMatch(uuidRegExp);
expect(Object.values(PlayingCardSuit)).toContain(card.suit);
expect(Object.values(PlayingCardName)).toContain(card.name);
expect(allPlayingCardSuits).toContain(card.suit);
expect(allPlayingCardNames).toContain(card.name);
});
});

Expand Down Expand Up @@ -112,7 +112,7 @@ describe('Cards reducer', () => {
const currentState = {
cards: {
1: {
cards: { id: '1', type: 'PlayingCard', name: PlayingCardName.Two, suit: PlayingCardSuit.Spades },
cards: { id: '1', type: 'PlayingCard', name: '2', suit: 'spades' },
isFaceUp: false,
position: { x: 0, y: 0 },
},
Expand All @@ -121,7 +121,7 @@ describe('Cards reducer', () => {
const expectedState = {
cards: {
1: {
cards: { id: '1', type: 'PlayingCard', name: PlayingCardName.Two, suit: PlayingCardSuit.Spades },
cards: { id: '1', type: 'PlayingCard', name: '2', suit: 'spades' },
isFaceUp: false,
position,
},
Expand All @@ -140,7 +140,7 @@ describe('Cards reducer', () => {
const initialState = {
cards: {
1: {
cards: { id: '1', type: 'PlayingCard', name: PlayingCardName.Two, suit: PlayingCardSuit.Spades },
cards: { id: '1', type: 'PlayingCard', name: '2', suit: 'spades' },
position: { x: 0, y: 0 },
isFaceUp: false,
},
Expand All @@ -149,7 +149,7 @@ describe('Cards reducer', () => {
const expectedState = {
cards: {
1: {
cards: { id: '1', type: 'PlayingCard', name: PlayingCardName.Two, suit: PlayingCardSuit.Spades },
cards: { id: '1', type: 'PlayingCard', name: '2', suit: 'spades' },
position: { x: 0, y: 0 },
isFaceUp: true,
},
Expand All @@ -165,8 +165,8 @@ describe('Cards reducer', () => {
describe('shuffleCardDeck', () => {
it('should shuffle cards', () => {
const shuffledCards = [
{ id: '3', type: 'PlayingCard', name: PlayingCardName.Three, suit: PlayingCardSuit.Spades },
{ id: '1', type: 'PlayingCard', name: PlayingCardName.Two, suit: PlayingCardSuit.Spades },
{ id: '3', type: 'PlayingCard', name: '3', suit: 'spades' },
{ id: '1', type: 'PlayingCard', name: '2', suit: 'spades' },
];
jest.spyOn(arrayUtils, 'shuffleArray').mockReturnValue(shuffledCards);

Expand All @@ -176,8 +176,8 @@ describe('Cards reducer', () => {
cards: {
1: {
cards: [
{ id: '1', type: 'PlayingCard', name: PlayingCardName.Two, suit: PlayingCardSuit.Spades },
{ id: '3', type: 'PlayingCard', name: PlayingCardName.Three, suit: PlayingCardSuit.Spades },
{ id: '1', type: 'PlayingCard', name: '2', suit: 'spades' },
{ id: '3', type: 'PlayingCard', name: '3', suit: 'spades' },
],
position: { x: 0, y: 0 },
isFaceUp: false,
Expand Down
6 changes: 3 additions & 3 deletions packages/components/src/utils/card-style.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as cardDimensionsUtils from '@utils/card-dimensions';
import { UnoCard } from '@models/UnoCard';
import { Dimensions } from '@models/Dimensions';
import { UnoCardDimensions } from '@atoms/UnoCardFace';
import { PlayingCard, PlayingCardName, PlayingCardSuit } from '@models/PlayingCard';
import { PlayingCard } from '@models/PlayingCard';
import { PlayingCardDimensions } from '@atoms/SvgPlayingCard';
import { defaultCardStyleFactory, CustomCardStyleFactory } from './card-style';

Expand All @@ -16,8 +16,8 @@ describe('defaultCardStyleFactory', () => {
const playingCard: PlayingCard = {
type: 'PlayingCard',
id: '1',
name: PlayingCardName.Ace,
suit: PlayingCardSuit.Clubs,
name: '1',
suit: 'clubs',
};
const tableDimensions: Dimensions = { width: 1, height: 2 };
const mockDimensions: Dimensions = { width: 100, height: 200 };
Expand Down
9 changes: 3 additions & 6 deletions packages/components/src/utils/generate-card-deck.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import { PlayingCard, PlayingCardName, PlayingCardSuit } from '@models/PlayingCard';
import { allPlayingCardNames, allPlayingCardSuits, PlayingCard } from '@models/PlayingCard';
import { v4 as uuid } from 'uuid';
import { Deck } from '@models/Deck';
import { AllUnoColors, AllUnoValues, UnoCard, UnoCardColor } from '@models/UnoCard';
import { shuffleArray } from './array-utils';

export const generateRandomPlayingCardDeck = (): Deck<PlayingCard> => {
const names = Object.values(PlayingCardName);
const suits = Object.values(PlayingCardSuit);

const cardDeck: PlayingCard[] = suits.flatMap((suit) => {
return names.map((name) => {
const cardDeck: PlayingCard[] = allPlayingCardSuits.flatMap((suit) => {
return allPlayingCardNames.map((name) => {
return { type: 'PlayingCard', name, suit, id: uuid() };
});
});
Expand Down
Loading