-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new Cards view, with listing of all cards (#562)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
- Loading branch information
1 parent
4a73b99
commit a1d03ae
Showing
6 changed files
with
127 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { render, screen } from '@testing-library/react'; | ||
import CardsView, { CARDS_VIEW_QUERY } from './CardsView'; | ||
import { MockedProvider } from '@apollo/react-testing'; | ||
import {BrowserRouter} from 'react-router-dom' | ||
import React from 'react'; | ||
|
||
it('should render the name of the view', async () => { | ||
const testFetchStatsMock = { | ||
request: { | ||
query: CARDS_VIEW_QUERY | ||
}, | ||
result: { | ||
data: { | ||
cards: null, | ||
} | ||
} | ||
} | ||
|
||
render( | ||
<MockedProvider mocks={[testFetchStatsMock]}> | ||
<CardsView /> | ||
</MockedProvider>, | ||
{wrapper: BrowserRouter}, | ||
); | ||
expect(await screen.findByText("Cards")).toBeInTheDocument(); | ||
}); | ||
|
||
|
||
it('should render cards when provided', async () => { | ||
const testFetchStatsMock = { | ||
request: { | ||
query: CARDS_VIEW_QUERY | ||
}, | ||
result: { | ||
data: { | ||
cards: [ | ||
{ bgaId: "testCard", name: "test card", mostPlayedBy: {user: {name: "test user"}, count: 100}} | ||
], | ||
} | ||
} | ||
} | ||
|
||
render( | ||
<MockedProvider mocks={[testFetchStatsMock]}> | ||
<CardsView /> | ||
</MockedProvider>, | ||
{wrapper: BrowserRouter}, | ||
); | ||
expect(await screen.findByText("test card")).toBeInTheDocument(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import React from 'react'; | ||
import { gql } from '@apollo/client/core'; | ||
import { useQuery } from '@apollo/client/react/hooks'; | ||
import { Link } from 'react-router-dom'; | ||
|
||
import PageContainer from '../components/PageContainer'; | ||
import PageTitle from "../components/PageTitle"; | ||
import Card from '../types/Card'; | ||
import User from '../types/User'; | ||
import Table from '../components/Table'; | ||
|
||
export const CARDS_VIEW_QUERY = gql` | ||
query FetchCards() { | ||
cards { | ||
bgaId | ||
name | ||
mostPlayedBy { | ||
user { | ||
name | ||
} | ||
count | ||
} | ||
} | ||
} | ||
`; | ||
|
||
|
||
|
||
const CardsView = () => { | ||
const { loading, error, data } = useQuery( | ||
CARDS_VIEW_QUERY, | ||
); | ||
|
||
|
||
let innerContent = <p></p>; | ||
if (loading) innerContent = <p>Loading...</p>; | ||
else if (error) innerContent = <p>Error: {error.message}</p>; | ||
else { | ||
const cards: Card[] = data.cards; | ||
const tableColumns = ["Name", "Most played by"]; | ||
const cardRows = cards.map((card: Card) => { | ||
const mostPlayedUser: User = data.cards.find((c: any) => {return c.bgaId === card.bgaId}).mostPlayedBy[0]; | ||
return { | ||
"Name": <Link to={`/card/${card.bgaId}`}>{card.name}</Link>, | ||
"Most played by": <Link to={`user/${mostPlayedUser.name}`}>{mostPlayedUser.name}</Link> | ||
} | ||
}); | ||
innerContent = ( | ||
<div> | ||
<PageTitle>Cards</PageTitle> | ||
<div className={"py-2"}> | ||
<Table | ||
cols={tableColumns} | ||
rows={cardRows} | ||
key="cards" | ||
showFilters={false} | ||
/> | ||
</div> | ||
</div> | ||
); | ||
} | ||
|
||
return ( | ||
<PageContainer> | ||
{innerContent} | ||
</PageContainer> | ||
) | ||
} | ||
|
||
export default CardsView; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters