feat: Add unit tests for resolvers and services, update dependencies, and remove unused scripts.
This commit is contained in:
44
src/resolvers/queries/user.spec.ts
Normal file
44
src/resolvers/queries/user.spec.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
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" },
|
||||
}),
|
||||
);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user