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

fix: 同じクエリパラメータを 2 回以上指定した場合のページネーション計算 #54

Merged
merged 2 commits into from
Mar 7, 2025
Merged
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
48 changes: 48 additions & 0 deletions app/feature/search/pagination.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,52 @@ describe("buildPaginationMeta", () => {

expect(prevQuery).toBe(null);
});

test("API 修正前ロジック: 次のページが存在しない場合に next が null になる", () => {
const currentQuery = {
post_includes_text: "地震",
limit: 10,
offset: 10,
} satisfies z.infer<typeof noteSearchParamSchema>;

// API が limit, offset 以外のクエリパラメータを削除してしまう挙動を再現
const currentBrokenMeta = {
next: null,
prev: "https://example.com/api/v1/data/search?offset=0&limit=10",
} satisfies PaginationMeta;

const fixedMeta = buildPaginationMeta(currentBrokenMeta, currentQuery);
const nextQuery = fixedMeta.next ? getQuery(fixedMeta.next) : null;

expect(nextQuery).toBe(null);
});

test("API修正前ロジック: 同じクエリパラメータが 2 回以上指定されていてもそのまま処理できる", () => {
const currentQuery = {
note_status: ["CURRENTLY_RATED_HELPFUL", "NEEDS_MORE_RATINGS"],
limit: 10,
offset: 10,
} satisfies z.infer<typeof noteSearchParamSchema>;

const currentBrokenMeta = {
// API が note_status を複数回指定した際、最後のもの以外を削除してしまう挙動を再現
next: "https://example.com/api/v1/data/search?note_status=NEEDS_MORE_RATINGS&limit=10&offset=20",
prev: "https://example.com/api/v1/data/search?note_status=NEEDS_MORE_RATINGS&limit=10&offset=0",
} satisfies PaginationMeta;

const fixedMeta = buildPaginationMeta(currentBrokenMeta, currentQuery);
const prevQuery = fixedMeta.prev ? getQuery(fixedMeta.prev) : null;
const nextQuery = fixedMeta.next ? getQuery(fixedMeta.next) : null;

expect(prevQuery).toStrictEqual({
note_status: ["CURRENTLY_RATED_HELPFUL", "NEEDS_MORE_RATINGS"],
limit: "10",
offset: "0",
});
expect(nextQuery).toStrictEqual({
note_status: ["CURRENTLY_RATED_HELPFUL", "NEEDS_MORE_RATINGS"],
limit: "10",
offset: "20",
});
});
});
12 changes: 8 additions & 4 deletions app/feature/search/pagination.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,13 @@ export const buildPaginationMeta = (
const baseUrl = stringifyParsedURL(url);

return {
next: withQuery(baseUrl, { ...rest, limit, offset: nextOffset }),
prev: isFirstPage
? null
: withQuery(baseUrl, { ...rest, limit, offset: prevOffset }),
next:
meta.next != null
? withQuery(baseUrl, { ...rest, limit, offset: nextOffset })
: null,
prev:
isFirstPage || meta.prev == null
? null
: withQuery(baseUrl, { ...rest, limit, offset: prevOffset }),
};
};