Skip to content

Commit 7267764

Browse files
author
Evgenii Perminov
committed
add tests
1 parent 66071a7 commit 7267764

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed

packages/openapi-react-query/test/index.test.tsx

+92
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,50 @@ describe("client", () => {
283283
expect(data).toBeUndefined();
284284
});
285285

286+
it("should resolve data properly and have error as null when queryFn returns null", async () => {
287+
const fetchClient = createFetchClient<paths>({ baseUrl });
288+
const client = createClient(fetchClient);
289+
290+
useMockRequestHandler({
291+
baseUrl,
292+
method: "get",
293+
path: "/string-array",
294+
status: 200,
295+
body: null,
296+
});
297+
298+
const { result } = renderHook(() => client.useQuery("get", "/string-array"), { wrapper });
299+
300+
await waitFor(() => expect(result.current.isFetching).toBe(false));
301+
302+
const { data, error } = result.current;
303+
304+
expect(data).toBeNull();
305+
expect(error).toBeNull();
306+
});
307+
308+
it("should resolve error properly and have undefined data when queryFn returns undefined", async () => {
309+
const fetchClient = createFetchClient<paths>({ baseUrl });
310+
const client = createClient(fetchClient);
311+
312+
useMockRequestHandler({
313+
baseUrl,
314+
method: "get",
315+
path: "/string-array",
316+
status: 200,
317+
body: undefined,
318+
});
319+
320+
const { result } = renderHook(() => client.useQuery("get", "/string-array"), { wrapper });
321+
322+
await waitFor(() => expect(result.current.isFetching).toBe(false));
323+
324+
const { data, error } = result.current;
325+
326+
expect(error).toBeInstanceOf(Error);
327+
expect(data).toBeUndefined();
328+
});
329+
286330
it("should infer correct data and error type", async () => {
287331
const fetchClient = createFetchClient<paths>({ baseUrl, fetch: fetchInfinite });
288332
const client = createClient(fetchClient);
@@ -560,6 +604,54 @@ describe("client", () => {
560604
expect(error?.message).toBe("Something went wrong");
561605
});
562606

607+
it("should resolve data properly and have error as null when mutationFn returns null", async () => {
608+
const fetchClient = createFetchClient<paths>({ baseUrl });
609+
const client = createClient(fetchClient);
610+
611+
useMockRequestHandler({
612+
baseUrl,
613+
method: "put",
614+
path: "/comment",
615+
status: 200,
616+
body: null,
617+
});
618+
619+
const { result } = renderHook(() => client.useMutation("put", "/comment"), { wrapper });
620+
621+
result.current.mutate({ body: { message: "Hello", replied_at: 0 } });
622+
623+
await waitFor(() => expect(result.current.isPending).toBe(false));
624+
625+
const { data, error } = result.current;
626+
627+
expect(data).toBeNull();
628+
expect(error).toBeNull();
629+
});
630+
631+
it("should resolve data properly and have error as null when mutationFn returns undefined", async () => {
632+
const fetchClient = createFetchClient<paths>({ baseUrl });
633+
const client = createClient(fetchClient);
634+
635+
useMockRequestHandler({
636+
baseUrl,
637+
method: "put",
638+
path: "/comment",
639+
status: 200,
640+
body: undefined,
641+
});
642+
643+
const { result } = renderHook(() => client.useMutation("put", "/comment"), { wrapper });
644+
645+
result.current.mutate({ body: { message: "Hello", replied_at: 0 } });
646+
647+
await waitFor(() => expect(result.current.isPending).toBe(false));
648+
649+
const { data, error } = result.current;
650+
651+
expect(error).toBeNull();
652+
expect(data).toBeUndefined();
653+
});
654+
563655
it("should use provided custom queryClient", async () => {
564656
const fetchClient = createFetchClient<paths>({ baseUrl });
565657
const client = createClient(fetchClient);

0 commit comments

Comments
 (0)