generated from alexstojda/go-react-app
-
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.
test(tournaments-frontend): Test list tournaments page
- Loading branch information
1 parent
2ca7624
commit 3656634
Showing
5 changed files
with
118 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import {Api, TournamentsApi} from "../../api"; | ||
import {fake} from '../../test'; | ||
import {render} from "@testing-library/react"; | ||
import {MemoryRouter} from "react-router-dom"; | ||
import TournamentsListPage from "./index"; | ||
|
||
jest.mock('../../api') | ||
const mockApi = jest.mocked(Api) | ||
|
||
beforeEach(() => { | ||
mockApi.prototype.leaguesApi.mockReset() | ||
mockApi.prototype.parseError.mockReset() | ||
}) | ||
|
||
describe('TournamentsListPage', () => { | ||
it('renders tournaments from API', async () => { | ||
const mockTournaments = Array.from({length: 5}, () => fake.tournament()) | ||
|
||
jest.mocked(TournamentsApi).prototype.tournamentsGet.mockResolvedValue({ | ||
config: {}, | ||
data: { | ||
tournaments: mockTournaments | ||
}, | ||
headers: {}, | ||
request: {}, | ||
status: 200, | ||
statusText: "", | ||
}) | ||
mockApi.prototype.tournamentsApi.mockReturnValue(new TournamentsApi()) | ||
|
||
const result = render( | ||
<MemoryRouter> | ||
<TournamentsListPage/> | ||
</MemoryRouter> | ||
) | ||
|
||
const header = await result.findByText("Tournaments") | ||
expect(header).toBeInTheDocument() | ||
|
||
const tournamentCards = await result.findAllByTestId("tournament-card") | ||
expect(tournamentCards).toHaveLength(mockTournaments.length) | ||
|
||
mockTournaments.forEach((tournament) => { | ||
expect(result.getByText(tournament.name)).toBeInTheDocument() | ||
expect(result.getByText(tournament.location!.name)).toBeInTheDocument() | ||
}) | ||
}) | ||
it('renders error from API', async () => { | ||
const mockError = fake.errorResponse() | ||
mockApi.prototype.parseError.mockReturnValue(mockError) | ||
|
||
jest.mocked(TournamentsApi).prototype.tournamentsGet.mockRejectedValue(false) | ||
mockApi.prototype.tournamentsApi.mockReturnValue(new TournamentsApi()) | ||
|
||
const result = render( | ||
<MemoryRouter> | ||
<TournamentsListPage/> | ||
</MemoryRouter> | ||
) | ||
|
||
const header = await result.findByText("Tournaments") | ||
expect(header).toBeInTheDocument() | ||
|
||
const error = await result.findByText(mockError.detail) | ||
expect(error).toBeInTheDocument() | ||
}) | ||
it('shows unknown error when parseError fails', async () => { | ||
mockApi.prototype.parseError.mockImplementation((e) => { | ||
throw e | ||
}) | ||
|
||
mockApi.prototype.tournamentsApi.mockReturnValue(new TournamentsApi()) | ||
jest.mocked(TournamentsApi).prototype.tournamentsGet.mockRejectedValue(false) | ||
|
||
const result = render( | ||
<MemoryRouter> | ||
<TournamentsListPage/> | ||
</MemoryRouter> | ||
) | ||
|
||
const header = await result.findByText("Tournaments") | ||
expect(header).toBeInTheDocument() | ||
|
||
const error = await result.findByText("Unknown error") | ||
expect(error).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
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,25 @@ | ||
import {faker} from "@faker-js/faker"; | ||
import {Tournament} from "../../api"; | ||
import * as helpers from "../../helpers"; | ||
import {location} from "./location"; | ||
import {league} from "./league"; | ||
|
||
export function tournament(): Tournament { | ||
const name = faker.hacker.phrase(); | ||
|
||
return { | ||
name: name, | ||
slug: helpers.slugify(name), | ||
id: faker.datatype.uuid(), | ||
type: "multi_round_tournament", | ||
settings: { | ||
rounds: 8, | ||
games_per_round: 4, | ||
lowest_scores_dropped: 3 | ||
}, | ||
location: location(), | ||
league: league(), | ||
created_at: faker.date.recent(5).toISOString(), | ||
updated_at: faker.date.recent(1).toISOString() | ||
} | ||
} |