Skip to content

Commit

Permalink
Implements unit tests for getMe and getOrg
Browse files Browse the repository at this point in the history
  • Loading branch information
allanlasser committed Apr 3, 2024
1 parent 0b311f9 commit ea5f6cf
Showing 1 changed file with 61 additions and 6 deletions.
67 changes: 61 additions & 6 deletions src/lib/api/accounts.test.ts
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");

0 comments on commit ea5f6cf

Please sign in to comment.