-
Notifications
You must be signed in to change notification settings - Fork 0
fix(frontend): align paginated posts routing #656
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 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
21e1440
fix: align paginated posts routing
SebastianSchuetze f2ad1bd
Merge branch 'main' into codex/pagination-consistency-routing
SebastianSchuetze 9c9fa83
fix(frontend): decouple pagination molecule from router
SebastianSchuetze 6ec4a5a
fix: resolve posts pagination import and add bounds test
SebastianSchuetze 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
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,84 @@ | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest' | ||
|
|
||
| const mocks = vi.hoisted(() => ({ | ||
| findMock: vi.fn(), | ||
| countMock: vi.fn(), | ||
| notFoundMock: vi.fn(() => { | ||
| throw new Error('notFound') | ||
| }), | ||
| redirectMock: vi.fn((path: string) => { | ||
| throw new Error(`redirect:${path}`) | ||
| }), | ||
| })) | ||
|
|
||
| vi.mock('next/navigation', () => ({ | ||
| notFound: mocks.notFoundMock, | ||
| redirect: mocks.redirectMock, | ||
| })) | ||
|
|
||
| vi.mock('@payload-config', () => ({ | ||
| default: {}, | ||
| })) | ||
|
|
||
| vi.mock('payload', async (importOriginal) => { | ||
| const actual = await importOriginal<typeof import('payload')>() | ||
| return { | ||
| ...actual, | ||
| getPayload: vi.fn().mockResolvedValue({ | ||
| find: mocks.findMock, | ||
| count: mocks.countMock, | ||
| }), | ||
| } | ||
| }) | ||
|
|
||
| describe('Paginated posts page route', () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks() | ||
| }) | ||
|
|
||
| const getModule = async () => import('@/app/(frontend)/posts/page/[pageNumber]/page') | ||
|
|
||
| it('redirects page 1 to canonical /posts', async () => { | ||
| const pageModule = await getModule() | ||
|
|
||
| await expect(pageModule.default({ params: Promise.resolve({ pageNumber: '1' }) })).rejects.toThrow( | ||
| 'redirect:/posts', | ||
| ) | ||
|
|
||
| expect(mocks.redirectMock).toHaveBeenCalledWith('/posts') | ||
| expect(mocks.findMock).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('returns notFound for non-numeric page params', async () => { | ||
| const pageModule = await getModule() | ||
|
|
||
| await expect(pageModule.default({ params: Promise.resolve({ pageNumber: 'abc' }) })).rejects.toThrow('notFound') | ||
|
|
||
| expect(mocks.notFoundMock).toHaveBeenCalled() | ||
| expect(mocks.findMock).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('returns notFound for out-of-range pages', async () => { | ||
| mocks.findMock.mockResolvedValueOnce({ | ||
| docs: [], | ||
| totalPages: 2, | ||
| totalDocs: 24, | ||
| page: 3, | ||
| }) | ||
|
|
||
| const pageModule = await getModule() | ||
|
|
||
| await expect(pageModule.default({ params: Promise.resolve({ pageNumber: '3' }) })).rejects.toThrow('notFound') | ||
|
|
||
| expect(mocks.findMock).toHaveBeenCalled() | ||
| expect(mocks.notFoundMock).toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('builds static params with page size 12 and skips page 1', async () => { | ||
| mocks.countMock.mockResolvedValueOnce({ totalDocs: 25 }) | ||
|
|
||
| const pageModule = await getModule() | ||
|
|
||
| await expect(pageModule.generateStaticParams()).resolves.toEqual([{ pageNumber: '2' }, { pageNumber: '3' }]) | ||
| }) | ||
| }) |
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,35 @@ | ||
| // @vitest-environment jsdom | ||
| import React from 'react' | ||
| import '@testing-library/jest-dom' | ||
| import { fireEvent, render, screen } from '@testing-library/react' | ||
| import { describe, expect, it, vi } from 'vitest' | ||
|
|
||
| vi.mock('next/navigation', () => ({ | ||
| useRouter: () => ({ | ||
| push: vi.fn(), | ||
| }), | ||
| })) | ||
|
|
||
| import { Pagination } from '@/components/molecules/Pagination' | ||
|
|
||
| describe('Pagination molecule', () => { | ||
| it('navigates previous from page 2 to canonical /posts', () => { | ||
| const onNavigate = vi.fn() | ||
|
|
||
| render(<Pagination page={2} totalPages={5} onNavigate={onNavigate} />) | ||
|
|
||
| fireEvent.click(screen.getByLabelText('Go to previous page')) | ||
|
|
||
| expect(onNavigate).toHaveBeenCalledWith('/posts') | ||
| }) | ||
|
|
||
| it('navigates page 1 button to canonical /posts', () => { | ||
| const onNavigate = vi.fn() | ||
|
|
||
| render(<Pagination page={1} totalPages={3} onNavigate={onNavigate} />) | ||
|
|
||
| fireEvent.click(screen.getByRole('button', { name: '1' })) | ||
|
|
||
| expect(onNavigate).toHaveBeenCalledWith('/posts') | ||
| }) | ||
| }) |
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.