Skip to content

Commit

Permalink
feat(react-query): add unit test for useInfiniteQuery
Browse files Browse the repository at this point in the history
  • Loading branch information
jungwoo3490 committed Dec 14, 2024
1 parent 2d7495b commit 5b61eea
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 7 deletions.
52 changes: 52 additions & 0 deletions packages/openapi-react-query/test/fixtures/api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,58 @@
*/

export interface paths {
"/paginated-data": {
parameters: {
query?: never;
header?: never;
path?: never;
cookie?: never;
};
get: {
parameters: {
query: {
limit: number;
};
header?: never;
path?: never;
cookie?: never;
};
requestBody?: never;
responses: {
/** @description Successful response */
200: {
headers: {
[name: string]: unknown;
};
content: {
"application/json": {
items?: number[];
nextPage?: number;
};
};
};
/** @description Error response */
500: {
headers: {
[name: string]: unknown;
};
content: {
"application/json": {
code?: number;
message?: string;
};
};
};
};
};
put?: never;
post?: never;
delete?: never;
options?: never;
head?: never;
patch?: never;
trace?: never;
};
"/comment": {
parameters: {
query?: never;
Expand Down
33 changes: 33 additions & 0 deletions packages/openapi-react-query/test/fixtures/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,39 @@ info:
title: Test Specification
version: "1.0"
paths:
/paginated-data:
get:
parameters:
- in: query
name: limit
required: true
schema:
type: integer
responses:
'200':
description: Successful response
content:
application/json:
schema:
type: object
properties:
items:
type: array
items:
type: integer
nextPage:
type: integer
'500':
description: Error response
content:
application/json:
schema:
type: object
properties:
code:
type: integer
message:
type: string
/comment:
put:
requestBody:
Expand Down
75 changes: 68 additions & 7 deletions packages/openapi-react-query/test/index.test.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { afterAll, beforeAll, describe, expect, it } from "vitest";
import { server, baseUrl, useMockRequestHandler } from "./fixtures/mock-server.js";
import type { paths } from "./fixtures/api.js";
import createClient from "../src/index.js";
import createFetchClient from "openapi-fetch";
import { fireEvent, render, renderHook, screen, waitFor, act } from "@testing-library/react";
import {
QueryClient,
QueryClientProvider,
skipToken,
useQueries,
useQuery,
useSuspenseQuery,
skipToken,
} from "@tanstack/react-query";
import { act, fireEvent, render, renderHook, screen, waitFor } from "@testing-library/react";
import createFetchClient from "openapi-fetch";
import { Suspense, type ReactNode } from "react";
import { ErrorBoundary } from "react-error-boundary";
import { afterAll, beforeAll, describe, expect, it } from "vitest";
import createClient from "../src/index.js";
import type { paths } from "./fixtures/api.js";
import { baseUrl, server, useMockRequestHandler } from "./fixtures/mock-server.js";

type minimalGetPaths = {
// Without parameters.
Expand Down Expand Up @@ -789,4 +789,65 @@ describe("client", () => {
});
});
});
describe("useInfiniteQuery", () => {
it("should fetch data correctly with pagination", async () => {
const fetchClient = createFetchClient<paths>({ baseUrl });
const client = createClient(fetchClient);

useMockRequestHandler({
baseUrl,
method: "get",
path: "/paginated-data",
status: 200,
body: { items: [1, 2, 3], nextPage: 1 },
});

const { result } = renderHook(
() => client.useInfiniteQuery("get", "/paginated-data", { params: { query: { limit: 3 } } }),
{ wrapper },
);

await waitFor(() => expect(result.current.isSuccess).toBe(true));

expect((result.current.data as any).pages[0]).toEqual({ items: [1, 2, 3], nextPage: 1 });

// Set up mock for second page
useMockRequestHandler({
baseUrl,
method: "get",
path: "/paginated-data",
status: 200,
body: { items: [4, 5, 6], nextPage: 2 },
});

await result.current.fetchNextPage();

await waitFor(() => expect(result.current.isFetching).toBe(false));

expect((result.current.data as any).pages).toHaveLength(2);
expect((result.current.data as any).pages[1]).toEqual({ items: [4, 5, 6], nextPage: 2 });
});

it("should handle errors correctly", async () => {
const fetchClient = createFetchClient<paths>({ baseUrl });
const client = createClient(fetchClient);

useMockRequestHandler({
baseUrl,
method: "get",
path: "/paginated-data",
status: 500,
body: { code: 500, message: "Internal Server Error" },
});

const { result } = renderHook(
() => client.useInfiniteQuery("get", "/paginated-data", { params: { query: { limit: 3 } } }),
{ wrapper },
);

await waitFor(() => expect(result.current.isError).toBe(true));

expect(result.current.error).toEqual({ code: 500, message: "Internal Server Error" });
});
});
});

0 comments on commit 5b61eea

Please sign in to comment.