|
| 1 | +import { getQuery } from "ufo"; |
| 2 | +import { describe, expect, test } from "vitest"; |
| 3 | +import type { z } from "zod"; |
| 4 | + |
| 5 | +import type { PaginationMeta } from "../../generated/api/schemas/paginationMeta"; |
| 6 | +import { buildPaginationMeta } from "./pagination"; |
| 7 | +import type { noteSearchParamSchema } from "./validation"; |
| 8 | + |
| 9 | +describe("buildPaginationMeta", () => { |
| 10 | + test("API 修正前ロジック: 現在のクエリパラメータから PaginationMeta を生成できる", () => { |
| 11 | + const currentQuery = { |
| 12 | + post_includes_text: "地震", |
| 13 | + limit: 10, |
| 14 | + offset: 10, |
| 15 | + } satisfies z.infer<typeof noteSearchParamSchema>; |
| 16 | + |
| 17 | + // API が limit, offset 以外のクエリパラメータを削除してしまう挙動を再現 |
| 18 | + const currentBrokenMeta = { |
| 19 | + next: "https://example.com/api/v1/data/search?offset=20&limit=10", |
| 20 | + prev: "https://example.com/api/v1/data/search?offset=0&limit=10", |
| 21 | + } satisfies PaginationMeta; |
| 22 | + |
| 23 | + const paginationMeta = buildPaginationMeta(currentBrokenMeta, currentQuery); |
| 24 | + const prevQuery = paginationMeta.prev |
| 25 | + ? getQuery(paginationMeta.prev) |
| 26 | + : null; |
| 27 | + const nextQuery = paginationMeta.next |
| 28 | + ? getQuery(paginationMeta.next) |
| 29 | + : null; |
| 30 | + |
| 31 | + expect(prevQuery).toStrictEqual({ |
| 32 | + limit: "10", |
| 33 | + offset: "0", |
| 34 | + post_includes_text: "地震", |
| 35 | + }); |
| 36 | + expect(nextQuery).toStrictEqual({ |
| 37 | + limit: "10", |
| 38 | + offset: "20", |
| 39 | + post_includes_text: "地震", |
| 40 | + }); |
| 41 | + }); |
| 42 | + |
| 43 | + test("API 修正前ロジック: offset が 0 より大きい AND limit より小さい場合も prev を生成できる", () => { |
| 44 | + const currentQuery = { |
| 45 | + post_includes_text: "地震", |
| 46 | + limit: 15, |
| 47 | + offset: 10, |
| 48 | + } satisfies z.infer<typeof noteSearchParamSchema>; |
| 49 | + |
| 50 | + // API が limit, offset 以外のクエリパラメータを削除してしまう挙動を再現 |
| 51 | + const currentBrokenMeta = { |
| 52 | + next: "https://example.com/api/v1/data/search?offset=25&limit=15", |
| 53 | + prev: "https://example.com/api/v1/data/search?offset=0&limit=15", |
| 54 | + } satisfies PaginationMeta; |
| 55 | + |
| 56 | + const fixedMeta = buildPaginationMeta(currentBrokenMeta, currentQuery); |
| 57 | + const prevQuery = fixedMeta.prev ? getQuery(fixedMeta.prev) : null; |
| 58 | + |
| 59 | + expect(prevQuery).toStrictEqual({ |
| 60 | + limit: "15", |
| 61 | + offset: "0", |
| 62 | + post_includes_text: "地震", |
| 63 | + }); |
| 64 | + }); |
| 65 | + |
| 66 | + test("API 修正前ロジック: 前のページが存在しない場合に prev が null になる", () => { |
| 67 | + const currentQuery = { |
| 68 | + post_includes_text: "地震", |
| 69 | + limit: 10, |
| 70 | + offset: 0, |
| 71 | + } satisfies z.infer<typeof noteSearchParamSchema>; |
| 72 | + |
| 73 | + // API が limit, offset 以外のクエリパラメータを削除してしまう挙動を再現 |
| 74 | + const currentBrokenMeta = { |
| 75 | + next: "https://example.com/api/v1/data/search?offset=10&limit=10", |
| 76 | + prev: null, |
| 77 | + } satisfies PaginationMeta; |
| 78 | + |
| 79 | + const fixedMeta = buildPaginationMeta(currentBrokenMeta, currentQuery); |
| 80 | + |
| 81 | + const prevQuery = fixedMeta.prev ? getQuery(fixedMeta.prev) : null; |
| 82 | + |
| 83 | + expect(prevQuery).toBe(null); |
| 84 | + }); |
| 85 | +}); |
0 commit comments