Skip to content

Commit 8667fa9

Browse files
authored
Merge pull request #54 from codeforjapan/fix/manual-pagination
fix: 同じクエリパラメータを 2 回以上指定した場合のページネーション計算
2 parents 143afa0 + 94652d8 commit 8667fa9

File tree

2 files changed

+56
-4
lines changed

2 files changed

+56
-4
lines changed

app/feature/search/pagination.test.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,52 @@ describe("buildPaginationMeta", () => {
8282

8383
expect(prevQuery).toBe(null);
8484
});
85+
86+
test("API 修正前ロジック: 次のページが存在しない場合に next が null になる", () => {
87+
const currentQuery = {
88+
post_includes_text: "地震",
89+
limit: 10,
90+
offset: 10,
91+
} satisfies z.infer<typeof noteSearchParamSchema>;
92+
93+
// API が limit, offset 以外のクエリパラメータを削除してしまう挙動を再現
94+
const currentBrokenMeta = {
95+
next: null,
96+
prev: "https://example.com/api/v1/data/search?offset=0&limit=10",
97+
} satisfies PaginationMeta;
98+
99+
const fixedMeta = buildPaginationMeta(currentBrokenMeta, currentQuery);
100+
const nextQuery = fixedMeta.next ? getQuery(fixedMeta.next) : null;
101+
102+
expect(nextQuery).toBe(null);
103+
});
104+
105+
test("API修正前ロジック: 同じクエリパラメータが 2 回以上指定されていてもそのまま処理できる", () => {
106+
const currentQuery = {
107+
note_status: ["CURRENTLY_RATED_HELPFUL", "NEEDS_MORE_RATINGS"],
108+
limit: 10,
109+
offset: 10,
110+
} satisfies z.infer<typeof noteSearchParamSchema>;
111+
112+
const currentBrokenMeta = {
113+
// API が note_status を複数回指定した際、最後のもの以外を削除してしまう挙動を再現
114+
next: "https://example.com/api/v1/data/search?note_status=NEEDS_MORE_RATINGS&limit=10&offset=20",
115+
prev: "https://example.com/api/v1/data/search?note_status=NEEDS_MORE_RATINGS&limit=10&offset=0",
116+
} satisfies PaginationMeta;
117+
118+
const fixedMeta = buildPaginationMeta(currentBrokenMeta, currentQuery);
119+
const prevQuery = fixedMeta.prev ? getQuery(fixedMeta.prev) : null;
120+
const nextQuery = fixedMeta.next ? getQuery(fixedMeta.next) : null;
121+
122+
expect(prevQuery).toStrictEqual({
123+
note_status: ["CURRENTLY_RATED_HELPFUL", "NEEDS_MORE_RATINGS"],
124+
limit: "10",
125+
offset: "0",
126+
});
127+
expect(nextQuery).toStrictEqual({
128+
note_status: ["CURRENTLY_RATED_HELPFUL", "NEEDS_MORE_RATINGS"],
129+
limit: "10",
130+
offset: "20",
131+
});
132+
});
85133
});

app/feature/search/pagination.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,13 @@ export const buildPaginationMeta = (
4242
const baseUrl = stringifyParsedURL(url);
4343

4444
return {
45-
next: withQuery(baseUrl, { ...rest, limit, offset: nextOffset }),
46-
prev: isFirstPage
47-
? null
48-
: withQuery(baseUrl, { ...rest, limit, offset: prevOffset }),
45+
next:
46+
meta.next != null
47+
? withQuery(baseUrl, { ...rest, limit, offset: nextOffset })
48+
: null,
49+
prev:
50+
isFirstPage || meta.prev == null
51+
? null
52+
: withQuery(baseUrl, { ...rest, limit, offset: prevOffset }),
4953
};
5054
};

0 commit comments

Comments
 (0)