Skip to content

Commit f666269

Browse files
authored
fix(openapi-react-query): updated and added 204 & zero Content-Length queryFn tests
1 parent 1bab84e commit f666269

File tree

1 file changed

+51
-1
lines changed

1 file changed

+51
-1
lines changed

Diff for: packages/openapi-react-query/test/index.test.tsx

+51-1
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ describe("client", () => {
315315
expect(error).toBeNull();
316316
});
317317

318-
it("should resolve error properly and have undefined data when queryFn returns undefined", async () => {
318+
it("handles undefined response with non-zero Content-Length (status 200) by setting error and undefined data", async () => {
319319
const fetchClient = createFetchClient<paths>({ baseUrl });
320320
const client = createClient(fetchClient);
321321

@@ -324,6 +324,9 @@ describe("client", () => {
324324
method: "get",
325325
path: "/string-array",
326326
status: 200,
327+
headers: {
328+
"Content-Length": "10"
329+
},
327330
body: undefined,
328331
});
329332

@@ -337,6 +340,53 @@ describe("client", () => {
337340
expect(data).toBeUndefined();
338341
});
339342

343+
it("handles undefined response with zero Content-Length by setting data and error to null", async () => {
344+
const fetchClient = createFetchClient<paths>({ baseUrl });
345+
const client = createClient(fetchClient);
346+
347+
useMockRequestHandler({
348+
baseUrl,
349+
method: "get",
350+
path: "/string-array",
351+
status: 200,
352+
headers: {
353+
"Content-Length": "0"
354+
},
355+
body: undefined,
356+
});
357+
358+
const { result } = renderHook(() => client.useQuery("get", "/string-array"), { wrapper });
359+
360+
await waitFor(() => expect(result.current.isFetching).toBe(false));
361+
362+
const { data, error } = result.current;
363+
364+
expect(error).toBeNull();
365+
expect(data).toBeNull();
366+
});
367+
368+
it("handles undefined response with 204 No Content status by setting data and error to null", async () => {
369+
const fetchClient = createFetchClient<paths>({ baseUrl });
370+
const client = createClient(fetchClient);
371+
372+
useMockRequestHandler({
373+
baseUrl,
374+
method: "get",
375+
path: "/string-array",
376+
status: 204,
377+
body: undefined,
378+
});
379+
380+
const { result } = renderHook(() => client.useQuery("get", "/string-array"), { wrapper });
381+
382+
await waitFor(() => expect(result.current.isFetching).toBe(false));
383+
384+
const { data, error } = result.current;
385+
386+
expect(error).toBeNull();
387+
expect(data).toBeNull();
388+
});
389+
340390
it("should infer correct data and error type", async () => {
341391
const fetchClient = createFetchClient<paths>({ baseUrl, fetch: fetchInfinite });
342392
const client = createClient(fetchClient);

0 commit comments

Comments
 (0)