45 lines
1.3 KiB
TypeScript
45 lines
1.3 KiB
TypeScript
import { GraphQLError } from "graphql";
|
|
import { describe, expect, it, vi } from "vitest";
|
|
|
|
import { getUser } from "~/services/auth/anilist/getUser";
|
|
|
|
import { user } from "./user";
|
|
|
|
vi.mock("~/services/auth/anilist/getUser", () => ({
|
|
getUser: vi.fn(),
|
|
}));
|
|
|
|
describe("user resolver", () => {
|
|
it("should throw GraphQLError (UNAUTHORIZED) if aniListToken is missing", async () => {
|
|
await expect(
|
|
user(null, {}, { aniListToken: undefined } as any),
|
|
).rejects.toThrow(
|
|
new GraphQLError("Unauthorized", {
|
|
extensions: { code: "UNAUTHORIZED" },
|
|
}),
|
|
);
|
|
});
|
|
|
|
it("should fetch user if token is present", async () => {
|
|
const mockUser = { id: 1, name: "test" };
|
|
vi.mocked(getUser).mockResolvedValue(mockUser as any);
|
|
|
|
const result = await user(null, {}, { aniListToken: "token" } as any);
|
|
|
|
expect(result).toEqual(mockUser);
|
|
expect(getUser).toHaveBeenCalledWith("token");
|
|
});
|
|
|
|
it("should throw GraphQLError if user service returns null", async () => {
|
|
vi.mocked(getUser).mockResolvedValue(null);
|
|
|
|
await expect(
|
|
user(null, {}, { aniListToken: "token" } as any),
|
|
).rejects.toThrow(
|
|
new GraphQLError("Failed to fetch user", {
|
|
extensions: { code: "INTERNAL_SERVER_ERROR" },
|
|
}),
|
|
);
|
|
});
|
|
});
|