Skip to content

Commit

Permalink
Merge pull request #54 from codeforjapan/fix/manual-pagination
Browse files Browse the repository at this point in the history
fix: 同じクエリパラメータを 2 回以上指定した場合のページネーション計算
  • Loading branch information
sushichan044 authored Mar 7, 2025
2 parents 143afa0 + 94652d8 commit 8667fa9
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 4 deletions.
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 }),
};
};

0 comments on commit 8667fa9

Please sign in to comment.