-
Notifications
You must be signed in to change notification settings - Fork 582
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(site): use axios in non-swr endpoints (#460)
Summary: Applies axios to login, logout and getApiKey Impact: POC of axios (#453) and testing axios Additional details: * add test:watch script resolves: #453
- Loading branch information
G r e y
committed
Mar 16, 2022
1 parent
95160d0
commit 810b2d2
Showing
4 changed files
with
160 additions
and
37 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import axios from "axios" | ||
import { APIKeyResponse, getApiKey, login, LoginResponse, logout } from "./api" | ||
|
||
// Mock the axios module so that no real network requests are made, but rather | ||
// we swap in a resolved or rejected value | ||
// | ||
// See: https://jestjs.io/docs/mock-functions#mocking-modules | ||
jest.mock("axios") | ||
|
||
describe("api.ts", () => { | ||
describe("login", () => { | ||
it("should return LoginResponse", async () => { | ||
// given | ||
const loginResponse: LoginResponse = { | ||
session_token: "abc_123_test", | ||
} | ||
const axiosMockPost = jest.fn().mockImplementationOnce(() => { | ||
return Promise.resolve({ data: loginResponse }) | ||
}) | ||
axios.post = axiosMockPost | ||
|
||
// when | ||
const result = await login("test", "123") | ||
|
||
// then | ||
expect(axiosMockPost).toHaveBeenCalled() | ||
expect(result).toStrictEqual(loginResponse) | ||
}) | ||
|
||
it("should throw an error on 401", async () => { | ||
// given | ||
// ..ensure that we await our expect assertion in async/await test | ||
expect.assertions(1) | ||
const expectedError = { | ||
message: "Validation failed", | ||
errors: [{ field: "email", code: "email" }], | ||
} | ||
const axiosMockPost = jest.fn().mockImplementationOnce(() => { | ||
return Promise.reject(expectedError) | ||
}) | ||
axios.post = axiosMockPost | ||
|
||
try { | ||
await login("test", "123") | ||
} catch (error) { | ||
expect(error).toStrictEqual(expectedError) | ||
} | ||
}) | ||
}) | ||
|
||
describe("logout", () => { | ||
it("should return without erroring", async () => { | ||
// given | ||
const axiosMockPost = jest.fn().mockImplementationOnce(() => { | ||
return Promise.resolve() | ||
}) | ||
axios.post = axiosMockPost | ||
|
||
// when | ||
await logout() | ||
|
||
// then | ||
expect(axiosMockPost).toHaveBeenCalled() | ||
}) | ||
|
||
it("should throw an error on 500", async () => { | ||
// given | ||
// ..ensure that we await our expect assertion in async/await test | ||
expect.assertions(1) | ||
const expectedError = { | ||
message: "Failed to logout.", | ||
} | ||
const axiosMockPost = jest.fn().mockImplementationOnce(() => { | ||
return Promise.reject(expectedError) | ||
}) | ||
axios.post = axiosMockPost | ||
|
||
try { | ||
await logout() | ||
} catch (error) { | ||
expect(error).toStrictEqual(expectedError) | ||
} | ||
}) | ||
}) | ||
|
||
describe("getApiKey", () => { | ||
it("should return APIKeyResponse", async () => { | ||
// given | ||
const apiKeyResponse: APIKeyResponse = { | ||
key: "abc_123_test", | ||
} | ||
const axiosMockPost = jest.fn().mockImplementationOnce(() => { | ||
return Promise.resolve({ data: apiKeyResponse }) | ||
}) | ||
axios.post = axiosMockPost | ||
|
||
// when | ||
const result = await getApiKey() | ||
|
||
// then | ||
expect(axiosMockPost).toHaveBeenCalled() | ||
expect(result).toStrictEqual(apiKeyResponse) | ||
}) | ||
|
||
it("should throw an error on 401", async () => { | ||
// given | ||
// ..ensure that we await our expect assertion in async/await test | ||
expect.assertions(1) | ||
const expectedError = { | ||
message: "No Cookie!", | ||
} | ||
const axiosMockPost = jest.fn().mockImplementationOnce(() => { | ||
return Promise.reject(expectedError) | ||
}) | ||
axios.post = axiosMockPost | ||
|
||
try { | ||
await getApiKey() | ||
} catch (error) { | ||
expect(error).toStrictEqual(expectedError) | ||
} | ||
}) | ||
}) | ||
}) |
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