-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add msw library for mocking API requests (#5492)
Signed-off-by: Peter Makowski <[email protected]>
- Loading branch information
1 parent
8c17eb1
commit 2c64499
Showing
7 changed files
with
289 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import { ROOT_API, fetchWithAuth } from "@/app/api/base"; | ||
import { fetchWithAuth, getFullApiUrl } from "@/app/api/base"; | ||
import type { Zone } from "@/app/store/zone/types"; | ||
|
||
export const fetchZones = (): Promise<Zone[]> => | ||
fetchWithAuth(`${ROOT_API}zones/`); | ||
fetchWithAuth(getFullApiUrl("zones")); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import type { JsonBodyType } from "msw"; | ||
|
||
import { useZonesCount } from "./zones"; | ||
|
||
import { getFullApiUrl } from "@/app/api/base"; | ||
import * as factory from "@/testing/factories"; | ||
import { | ||
renderHookWithQueryClient, | ||
setupMockServer, | ||
waitFor, | ||
} from "@/testing/utils"; | ||
|
||
const { server, http, HttpResponse } = setupMockServer(); | ||
|
||
const setupZonesTest = (mockData: JsonBodyType) => { | ||
server.use( | ||
http.get(getFullApiUrl("zones"), () => HttpResponse.json(mockData)) | ||
); | ||
return renderHookWithQueryClient(() => useZonesCount()); | ||
}; | ||
|
||
it("should return 0 when zones data is undefined", async () => { | ||
const { result } = setupZonesTest(null); | ||
await waitFor(() => expect(result.current).toBe(0)); | ||
}); | ||
|
||
it("should return the correct count when zones data is available", async () => { | ||
const mockZonesData = [factory.zone(), factory.zone(), factory.zone()]; | ||
const { result } = setupZonesTest(mockZonesData); | ||
await waitFor(() => expect(result.current).toBe(3)); | ||
}); | ||
|
||
it("should return 0 when zones data is an empty array", async () => { | ||
const { result } = setupZonesTest([]); | ||
await waitFor(() => expect(result.current).toBe(0)); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.