This repository has been archived by the owner on Jul 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
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
Showing
5 changed files
with
61 additions
and
68 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 was deleted.
Oops, something went wrong.
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,48 @@ | ||
import { post } from "./request"; | ||
|
||
describe("post", () => { | ||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it("should make a POST request and return the response for a successful request", async () => { | ||
const url = "/api/resource"; | ||
const body = { key: "value" }; | ||
const fetchMock = jest.fn().mockResolvedValue({ | ||
ok: true, | ||
}); | ||
|
||
jest.spyOn(global, "fetch").mockImplementation(fetchMock); | ||
|
||
const response = await post(url, body); | ||
|
||
expect(response).toStrictEqual({ ok: true }); | ||
expect(fetchMock).toHaveBeenCalledWith("/api/resource", { | ||
method: "POST", | ||
body: JSON.stringify(body), | ||
credentials: "include", | ||
}); | ||
}); | ||
|
||
it("should handle and report an error for a failed request", async () => { | ||
const url = "/api/resource"; | ||
const body = { key: "value" }; | ||
const fetchMock = jest.fn().mockResolvedValue({ | ||
ok: false, | ||
status: 500, | ||
statusText: "Internal Server Error", | ||
}); | ||
const errorReportingMock = jest.fn(); | ||
jest.spyOn(console, "error").mockImplementation(errorReportingMock); | ||
jest.spyOn(global, "fetch").mockImplementation(fetchMock); | ||
|
||
await post(url, body); | ||
|
||
expect(fetchMock).toHaveBeenCalledWith("/api/resource", { | ||
method: "POST", | ||
body: JSON.stringify(body), | ||
credentials: "include", | ||
}); | ||
expect(errorReportingMock).toHaveBeenCalledTimes(1); | ||
}); | ||
}); |
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 |
---|---|---|
|
@@ -7000,15 +7000,6 @@ axe-core@=4.7.0: | |
resolved "https://registry.yarnpkg.com/axe-core/-/axe-core-4.7.0.tgz#34ba5a48a8b564f67e103f0aa5768d76e15bbbbf" | ||
integrity sha512-M0JtH+hlOL5pLQwHOLNYZaXuhqmvS8oExsqB1SBYgA4Dk7u/xx+YdGHXaK5pyUfed5mYXdlYiphWq3G8cRi5JQ== | ||
|
||
[email protected]: | ||
version "1.6.1" | ||
resolved "https://registry.yarnpkg.com/axios/-/axios-1.6.1.tgz#76550d644bf0a2d469a01f9244db6753208397d7" | ||
integrity sha512-vfBmhDpKafglh0EldBEbVuoe7DyAavGSLWhuSm5ZSEKQnHhBf0xAAwybbNH1IkrJNGnS/VG4I5yxig1pCEXE4g== | ||
dependencies: | ||
follow-redirects "^1.15.0" | ||
form-data "^4.0.0" | ||
proxy-from-env "^1.1.0" | ||
|
||
axobject-query@^3.2.1: | ||
version "3.2.1" | ||
resolved "https://registry.yarnpkg.com/axobject-query/-/axobject-query-3.2.1.tgz#39c378a6e3b06ca679f29138151e45b2b32da62a" | ||
|
@@ -9674,7 +9665,7 @@ [email protected], focus-trap@^6.9.4: | |
dependencies: | ||
tabbable "^5.3.3" | ||
|
||
follow-redirects@^1.0.0, follow-redirects@^1.15.0: | ||
follow-redirects@^1.0.0: | ||
version "1.15.4" | ||
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.15.4.tgz#cdc7d308bf6493126b17ea2191ea0ccf3e535adf" | ||
integrity sha512-Cr4D/5wlrb0z9dgERpUL3LrmPKVDsETIJhaCMeDfuFYcqa5bldGV6wBsAN6X/vxlXQtFBMrXdXxdL8CbDTGniw== | ||
|