Skip to content

Commit 402829c

Browse files
committed
fix: null のハンドリングが甘かったので治す
1 parent 143afa0 commit 402829c

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

app/feature/search/pagination.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,23 @@ 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+
});
85104
});

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)