-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ru Chern Chong
committed
Dec 31, 2023
1 parent
5c14d14
commit 9d8f72e
Showing
7 changed files
with
41 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,4 @@ | ||
import "@testing-library/jest-dom"; | ||
import fetchMock from "jest-fetch-mock"; | ||
|
||
fetchMock.enableMocks(); |
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import fetch from "jest-fetch-mock"; | ||
import { fetchApi } from "@/utils/fetchApi"; | ||
|
||
describe("fetchApi", () => { | ||
beforeEach(() => fetch.resetMocks()); | ||
|
||
it("should return data for a successful API call", async () => { | ||
fetch.mockResponseOnce(JSON.stringify({ data: "test" })); | ||
|
||
const url = "https://example.com/api/test"; | ||
const data = await fetchApi(url); | ||
|
||
expect(fetch).toHaveBeenCalledTimes(1); | ||
expect(data).toEqual({ data: "test" }); | ||
}); | ||
}); |
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,16 @@ | ||
interface Options extends RequestInit {} | ||
|
||
export const fetchApi = async <T>( | ||
url: string, | ||
options: Options = {}, | ||
): Promise<T> => { | ||
const response = await fetch(url, options); | ||
|
||
if (!response.ok) { | ||
throw new Error( | ||
`API call failed: ${response.status} - ${response.statusText}`, | ||
); | ||
} | ||
|
||
return await response.json(); | ||
}; |