Repository navigation
Add championship progress chart #4
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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,44 @@ | ||
| import { NextRequest } from 'next/server'; | ||
| import { describe, expect, it } from 'vitest'; | ||
|
|
||
| import { GET } from './route'; | ||
|
|
||
| describe('GET /api/championships', () => { | ||
| it('returns championship data for valid year query', async () => { | ||
| const request = new NextRequest( | ||
| 'http://localhost:3000/api/championships?year=2024', | ||
| ); | ||
|
|
||
| const response = await GET(request); | ||
| const payload = await response.json(); | ||
|
|
||
| expect(response.status).toBe(200); | ||
| expect(payload.data.year).toBe(2024); | ||
| expect(Array.isArray(payload.data.drivers)).toBe(true); | ||
| expect(payload.availableYears[0]).toBe(2024); | ||
| }); | ||
|
|
||
| it('returns 400 for an invalid year value', async () => { | ||
| const request = new NextRequest( | ||
| 'http://localhost:3000/api/championships?year=twenty-twenty-four', | ||
| ); | ||
|
|
||
| const response = await GET(request); | ||
| const payload = await response.json(); | ||
|
|
||
| expect(response.status).toBe(400); | ||
| expect(payload.message).toMatch(/invalid year/i); | ||
| }); | ||
|
|
||
| it('uses 2024 as default year when year is omitted', async () => { | ||
| const request = new NextRequest( | ||
| 'http://localhost:3000/api/championships', | ||
| ); | ||
|
|
||
| const response = await GET(request); | ||
| const payload = await response.json(); | ||
|
|
||
| expect(response.status).toBe(200); | ||
| expect(payload.data.year).toBe(2024); | ||
| }); | ||
| }); |
This file contains hidden or 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,44 @@ | ||
| import { NextRequest, NextResponse } from 'next/server'; | ||
|
|
||
| import { | ||
| getChampionshipByYear, | ||
| isSupportedYear, | ||
| toServiceError, | ||
| } from '@/lib/server/championships'; | ||
|
|
||
| export const dynamic = 'force-dynamic'; | ||
|
|
||
| const defaultYear = 2024; | ||
|
|
||
| export async function GET(request: NextRequest) { | ||
| const yearParam = request.nextUrl.searchParams.get('year'); | ||
|
|
||
| const year = yearParam ? Number(yearParam) : defaultYear; | ||
|
|
||
| if (Number.isNaN(year) || !Number.isInteger(year)) { | ||
| return NextResponse.json( | ||
| { message: 'Invalid year query parameter.' }, | ||
| { status: 400 }, | ||
| ); | ||
| } | ||
|
|
||
| if (!isSupportedYear(year)) { | ||
| return NextResponse.json( | ||
| { message: 'Unsupported championship year.' }, | ||
| { status: 400 }, | ||
| ); | ||
| } | ||
|
|
||
| try { | ||
| const response = await getChampionshipByYear({ year }); | ||
| return NextResponse.json(response, { status: 200 }); | ||
| } catch (error) { | ||
| const serviceError = toServiceError(error); | ||
| return NextResponse.json( | ||
| { | ||
| message: serviceError.message, | ||
| }, | ||
| { status: serviceError.status }, | ||
| ); | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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,44 @@ | ||
| import type { ChampionshipCategory } from '@/types/championship'; | ||
|
|
||
| interface CategoryTabsProps { | ||
| selectedCategory: ChampionshipCategory; | ||
| onChange: (nextCategory: ChampionshipCategory) => void; | ||
| disabled?: boolean; | ||
| } | ||
|
|
||
| const tabItems: Array<{ id: ChampionshipCategory; label: string }> = [ | ||
| { id: 'drivers', label: 'Drivers' }, | ||
| { id: 'constructors', label: 'Constructors' }, | ||
| ]; | ||
|
|
||
| export default function CategoryTabs({ | ||
| selectedCategory, | ||
| onChange, | ||
| disabled = false, | ||
| }: CategoryTabsProps) { | ||
| return ( | ||
| <div className="bg-surface-hover inline-flex rounded-lg p-1"> | ||
| {tabItems.map((item) => { | ||
| const isActive = selectedCategory === item.id; | ||
|
|
||
| return ( | ||
| <button | ||
| key={item.id} | ||
| type="button" | ||
| aria-pressed={isActive} | ||
| disabled={disabled} | ||
| onClick={() => onChange(item.id)} | ||
| className={[ | ||
| 'rounded-md px-4 py-2 text-sm font-medium transition-colors disabled:opacity-60', | ||
| isActive | ||
| ? 'bg-surface text-foreground' | ||
| : 'text-muted hover:text-foreground', | ||
| ].join(' ')} | ||
| > | ||
| {item.label} | ||
| </button> | ||
| ); | ||
| })} | ||
| </div> | ||
| ); | ||
| } |
206 changes: 206 additions & 0 deletions
206
src/components/championships/ChampionshipsExplorer.spec.tsx
This file contains hidden or 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,206 @@ | ||
| import { render, screen, waitFor } from '@testing-library/react'; | ||
| import userEvent from '@testing-library/user-event'; | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| import ChampionshipsExplorer from './ChampionshipsExplorer'; | ||
|
|
||
| vi.mock('./PointsProgressChart', () => ({ | ||
| default: ({ | ||
| progress, | ||
| }: { | ||
| progress: { | ||
| year: number; | ||
| category: string; | ||
| series: Array<{ name: string }>; | ||
| }; | ||
| }) => ( | ||
| <div> | ||
| Chart for {progress.year} {progress.category}: {progress.series[0]?.name} | ||
| </div> | ||
| ), | ||
| })); | ||
|
|
||
| const yearPayload = { | ||
| availableYears: [2024, 2023, 2022], | ||
| data: { | ||
| year: 2024, | ||
| races: [ | ||
| { round: 1, name: 'Bahrain GP' }, | ||
| { round: 2, name: 'Saudi Arabian GP' }, | ||
| ], | ||
| drivers: [ | ||
| { | ||
| id: '1', | ||
| name: 'Max Verstappen', | ||
| color: '#3671C6', | ||
| cumulativePoints: [25, 43], | ||
| raceResults: [ | ||
| { | ||
| constructorId: '9', | ||
| constructorName: 'Red Bull', | ||
| constructorColor: '#3671C6', | ||
| racePoints: 25, | ||
| }, | ||
| { | ||
| constructorId: '9', | ||
| constructorName: 'Red Bull', | ||
| constructorColor: '#3671C6', | ||
| racePoints: 18, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| id: '2', | ||
| name: 'Sergio Perez', | ||
| color: '#3671C6', | ||
| cumulativePoints: [18, 33], | ||
| raceResults: [ | ||
| { | ||
| constructorId: '9', | ||
| constructorName: 'Red Bull', | ||
| constructorColor: '#3671C6', | ||
| racePoints: 18, | ||
| }, | ||
| { | ||
| constructorId: '9', | ||
| constructorName: 'Red Bull', | ||
| constructorColor: '#3671C6', | ||
| racePoints: 15, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| id: '3', | ||
| name: 'Charles Leclerc', | ||
| color: '#DC0000', | ||
| cumulativePoints: [15, 30], | ||
| raceResults: [ | ||
| { | ||
| constructorId: '6', | ||
| constructorName: 'Ferrari', | ||
| constructorColor: '#DC0000', | ||
| racePoints: 15, | ||
| }, | ||
| { | ||
| constructorId: '6', | ||
| constructorName: 'Ferrari', | ||
| constructorColor: '#DC0000', | ||
| racePoints: 15, | ||
| }, | ||
| ], | ||
| }, | ||
| ], | ||
| }, | ||
| }; | ||
|
|
||
| describe('ChampionshipsExplorer', () => { | ||
| beforeEach(() => { | ||
| vi.restoreAllMocks(); | ||
| }); | ||
|
|
||
| it('loads 2024 drivers data by default', async () => { | ||
| const fetchMock = vi.fn().mockResolvedValue({ | ||
| ok: true, | ||
| json: async () => yearPayload, | ||
| }); | ||
|
|
||
| vi.stubGlobal('fetch', fetchMock); | ||
|
|
||
| render(<ChampionshipsExplorer />); | ||
|
|
||
| await waitFor(() => { | ||
| expect(fetchMock).toHaveBeenCalledWith( | ||
| '/api/championships?year=2024', | ||
| expect.objectContaining({ cache: 'no-store', method: 'GET' }), | ||
| ); | ||
| }); | ||
|
|
||
| expect( | ||
| await screen.findByText(/chart for 2024 drivers: max verstappen/i), | ||
| ).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('does not refetch when switching to constructors tab', async () => { | ||
| const user = userEvent.setup(); | ||
| const fetchMock = vi.fn().mockResolvedValue({ | ||
| ok: true, | ||
| json: async () => yearPayload, | ||
| }); | ||
|
|
||
| vi.stubGlobal('fetch', fetchMock); | ||
|
|
||
| render(<ChampionshipsExplorer />); | ||
|
|
||
| expect( | ||
| await screen.findByText(/chart for 2024 drivers: max verstappen/i), | ||
| ).toBeInTheDocument(); | ||
|
|
||
| const constructorsToggle = screen.getByRole('button', { | ||
| name: /constructors/i, | ||
| }); | ||
|
|
||
| expect(constructorsToggle).toHaveAttribute('aria-pressed', 'false'); | ||
|
|
||
| await user.click(constructorsToggle); | ||
|
|
||
| expect(constructorsToggle).toHaveAttribute('aria-pressed', 'true'); | ||
|
|
||
| expect( | ||
| await screen.findByText(/chart for 2024 constructors: red bull/i), | ||
| ).toBeInTheDocument(); | ||
| expect(fetchMock).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('refetches when year changes', async () => { | ||
| const user = userEvent.setup(); | ||
| const fetchMock = vi | ||
| .fn() | ||
| .mockResolvedValueOnce({ | ||
| ok: true, | ||
| json: async () => yearPayload, | ||
| }) | ||
| .mockResolvedValueOnce({ | ||
| ok: true, | ||
| json: async () => ({ | ||
| ...yearPayload, | ||
| data: { | ||
| ...yearPayload.data, | ||
| year: 2023, | ||
| }, | ||
| }), | ||
| }); | ||
|
|
||
| vi.stubGlobal('fetch', fetchMock); | ||
|
|
||
| render(<ChampionshipsExplorer />); | ||
|
|
||
| expect( | ||
| await screen.findByText(/chart for 2024 drivers: max verstappen/i), | ||
| ).toBeInTheDocument(); | ||
|
|
||
| await user.selectOptions(screen.getByLabelText(/season/i), '2023'); | ||
|
|
||
| expect( | ||
| await screen.findByText(/chart for 2023 drivers/i), | ||
| ).toBeInTheDocument(); | ||
| expect(fetchMock).toHaveBeenLastCalledWith( | ||
| '/api/championships?year=2023', | ||
| expect.objectContaining({ cache: 'no-store', method: 'GET' }), | ||
| ); | ||
| }); | ||
|
|
||
| it('shows an error state when request fails', async () => { | ||
| const fetchMock = vi.fn().mockResolvedValue({ | ||
| ok: false, | ||
| json: async () => ({ message: 'Unsupported championship year.' }), | ||
| }); | ||
|
|
||
| vi.stubGlobal('fetch', fetchMock); | ||
|
|
||
| render(<ChampionshipsExplorer />); | ||
|
|
||
| expect(await screen.findByRole('alert')).toHaveTextContent( | ||
| /unsupported championship year/i, | ||
| ); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.