diff --git a/app/feature/search/pagination.test.ts b/app/feature/search/pagination.test.ts index ed71d05..dbb1dc7 100644 --- a/app/feature/search/pagination.test.ts +++ b/app/feature/search/pagination.test.ts @@ -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; + + // 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; + + 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", + }); + }); }); diff --git a/app/feature/search/pagination.ts b/app/feature/search/pagination.ts index 2d8ab2c..b9a3bf1 100644 --- a/app/feature/search/pagination.ts +++ b/app/feature/search/pagination.ts @@ -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 }), }; };