-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add API thunks for JIMM endpoints (#1767)
- Loading branch information
Showing
8 changed files
with
197 additions
and
1 deletion.
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 @@ | ||
import "vitest-fetch-mock"; |
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,5 @@ | ||
export const endpoints = { | ||
login: "/auth/login", | ||
logout: "/auth/logout", | ||
whoami: "/auth/whoami", | ||
}; |
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,67 @@ | ||
import { unwrapResult } from "@reduxjs/toolkit"; | ||
import { vi } from "vitest"; | ||
|
||
import { endpoints } from "./api"; | ||
import { logout, whoami } from "./thunks"; | ||
|
||
describe("thunks", () => { | ||
beforeEach(() => { | ||
fetchMock.resetMocks(); | ||
}); | ||
|
||
it("logout", async () => { | ||
const action = logout(); | ||
await action(vi.fn(), vi.fn(), null); | ||
expect(global.fetch).toHaveBeenCalledWith(endpoints.logout); | ||
}); | ||
|
||
it("logout handles unsuccessful requests", async () => { | ||
fetchMock.mockResponseOnce(JSON.stringify({}), { status: 500 }); | ||
const action = logout(); | ||
const response = await action(vi.fn(), vi.fn(), null); | ||
expect("error" in response ? response.error.message : null).toBe( | ||
"Unable to log out: non-success response", | ||
); | ||
}); | ||
|
||
it("logout handles errors", async () => { | ||
fetchMock.mockRejectedValue("404"); | ||
const action = logout(); | ||
const response = await action(vi.fn(), vi.fn(), null); | ||
expect("error" in response ? response.error.message : null).toBe( | ||
"Unable to log out: 404", | ||
); | ||
}); | ||
|
||
it("whoami returns a user", async () => { | ||
const action = whoami(); | ||
await action(vi.fn(), vi.fn(), null); | ||
expect(global.fetch).toHaveBeenCalledWith(endpoints.whoami); | ||
}); | ||
|
||
it("whoami handles non-authenticated user", async () => { | ||
fetchMock.mockResponseOnce(JSON.stringify({}), { status: 403 }); | ||
const action = whoami(); | ||
const response = await action(vi.fn(), vi.fn(), null); | ||
expect(global.fetch).toHaveBeenCalledWith(endpoints.whoami); | ||
expect(unwrapResult(response)).toBeNull(); | ||
}); | ||
|
||
it("whoami unsuccessful requests", async () => { | ||
fetchMock.mockResponse(JSON.stringify({}), { status: 500 }); | ||
const action = whoami(); | ||
const response = await action(vi.fn(), vi.fn(), null); | ||
expect("error" in response ? response.error.message : null).toBe( | ||
"Unable to get user details: non-success response", | ||
); | ||
}); | ||
|
||
it("whoami handles errors", async () => { | ||
fetchMock.mockRejectedValue("404"); | ||
const action = whoami(); | ||
const response = await action(vi.fn(), vi.fn(), null); | ||
expect("error" in response ? response.error.message : null).toBe( | ||
"Unable to get user details: 404", | ||
); | ||
}); | ||
}); |
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,51 @@ | ||
import { createAsyncThunk } from "@reduxjs/toolkit"; | ||
|
||
import type { RootState } from "store/store"; | ||
import { toErrorString } from "utils"; | ||
|
||
import { endpoints } from "./api"; | ||
|
||
/** | ||
Log out of the JIMM API. | ||
*/ | ||
export const logout = createAsyncThunk< | ||
void, | ||
void, | ||
{ | ||
state: RootState; | ||
} | ||
>("jimm/logout", async () => { | ||
try { | ||
const response = await fetch(endpoints.logout); | ||
if (!response.ok) { | ||
throw new Error("non-success response"); | ||
} | ||
} catch (error) { | ||
throw new Error(`Unable to log out: ${toErrorString(error)}`); | ||
} | ||
}); | ||
|
||
/** | ||
Get the authenticated user from the JIMM API. | ||
*/ | ||
export const whoami = createAsyncThunk< | ||
void, | ||
void, | ||
{ | ||
state: RootState; | ||
} | ||
>("jimm/whoami", async () => { | ||
try { | ||
const response = await fetch(endpoints.whoami); | ||
if (response.status === 403) { | ||
// The user is not authenticated so return null instead of throwing an error. | ||
return null; | ||
} | ||
if (!response.ok) { | ||
throw new Error("non-success response"); | ||
} | ||
return await response.json(); | ||
} catch (error) { | ||
throw new Error(`Unable to get user details: ${toErrorString(error)}`); | ||
} | ||
}); |
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