-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implements unit tests for
getMe
and getOrg
- Loading branch information
1 parent
0b311f9
commit ea5f6cf
Showing
1 changed file
with
61 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,64 @@ | ||
import { test, describe } from "vitest"; | ||
import { vi, test, describe, it, expect, beforeEach, afterEach } from "vitest"; | ||
|
||
import * as accounts from "./accounts"; | ||
import { BASE_API_URL } from "@/config/config"; | ||
import * as fixtures from "@/test/fixtures/accounts"; | ||
|
||
describe("users and orgs tests", () => { | ||
test.todo("users.get"); | ||
test.todo("users.list"); | ||
test.todo("users.getMe"); | ||
import { getMe, getOrg } from "./accounts"; | ||
|
||
describe("getMe", async () => { | ||
let mockFetch; | ||
beforeEach(() => { | ||
mockFetch = vi.fn().mockImplementation(async () => ({ | ||
ok: true, | ||
json: vi.fn().mockReturnValue(fixtures.me), | ||
})); | ||
}); | ||
afterEach(() => { | ||
vi.restoreAllMocks(); | ||
}); | ||
|
||
it("returns the expected data", async () => { | ||
const resp = await getMe(mockFetch); | ||
expect(resp).toBe(fixtures.me); | ||
}); | ||
|
||
it("calls the expected endpoint", async () => { | ||
await getMe(mockFetch); | ||
const expectedEndpoint = new URL(`users/me/`, BASE_API_URL); | ||
expectedEndpoint.searchParams.set("expand", "organization"); | ||
expect(mockFetch).toHaveBeenCalledWith(expectedEndpoint, { | ||
credentials: "include", | ||
}); | ||
}); | ||
|
||
it("returns undefined upon server error", async () => { | ||
mockFetch = vi.fn().mockImplementation(async () => ({ | ||
ok: false, | ||
json: vi.fn().mockReturnValue(fixtures.me), | ||
})); | ||
const resp = await getMe(mockFetch); | ||
expect(resp).toBeUndefined(); | ||
}); | ||
|
||
it("returns undefined upon fetch error", async () => { | ||
mockFetch = vi.fn().mockRejectedValue(new Error("Fetch Error")); | ||
const resp = await getMe(mockFetch); | ||
expect(resp).toBeUndefined(); | ||
}); | ||
}); | ||
|
||
test("getOrg", async () => { | ||
const mockFetch = vi.fn().mockImplementation(async () => ({ | ||
ok: true, | ||
json: vi.fn().mockReturnValue(fixtures.organization), | ||
})); | ||
const resp = await getOrg(mockFetch, 1); | ||
expect(resp).toEqual(fixtures.organization); | ||
expect(mockFetch).toHaveBeenCalledWith( | ||
new URL("organizations/1/", BASE_API_URL), | ||
{ credentials: "include" }, | ||
); | ||
}); | ||
|
||
test.todo("users.get"); | ||
test.todo("users.list"); |