feat: Add unit tests for resolvers and services, update dependencies, and remove unused scripts.
This commit is contained in:
64
src/services/episodes/markEpisodeAsWatched/anilist.spec.ts
Normal file
64
src/services/episodes/markEpisodeAsWatched/anilist.spec.ts
Normal file
@@ -0,0 +1,64 @@
|
||||
import { env } from "cloudflare:workers";
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
import { markEpisodeAsWatched } from "./anilist";
|
||||
|
||||
describe("markEpisodeAsWatched service", () => {
|
||||
const mockStub = {
|
||||
markTitleAsWatched: vi.fn(),
|
||||
markEpisodeAsWatched: vi.fn(),
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
// @ts-expect-error - Partial mock
|
||||
env.ANILIST_DO = {
|
||||
idFromName: vi.fn().mockReturnValue("global-id"),
|
||||
get: vi.fn().mockReturnValue(mockStub),
|
||||
};
|
||||
});
|
||||
|
||||
it("should call markTitleAsWatched on DO if markTitleAsComplete is true", async () => {
|
||||
mockStub.markTitleAsWatched.mockResolvedValue({
|
||||
user: { id: 1, statistics: { anime: {} } },
|
||||
});
|
||||
|
||||
const result = await markEpisodeAsWatched("token", 1, 12, true);
|
||||
|
||||
expect(result).toBeDefined();
|
||||
expect(mockStub.markTitleAsWatched).toHaveBeenCalledWith(1, "token");
|
||||
expect(mockStub.markEpisodeAsWatched).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("should call markEpisodeAsWatched on DO if markTitleAsComplete is false", async () => {
|
||||
mockStub.markEpisodeAsWatched.mockResolvedValue({
|
||||
user: { id: 1, statistics: { anime: {} } },
|
||||
});
|
||||
|
||||
const result = await markEpisodeAsWatched("token", 1, 12, false);
|
||||
|
||||
expect(result).toBeDefined();
|
||||
expect(mockStub.markEpisodeAsWatched).toHaveBeenCalledWith(1, 12, "token");
|
||||
expect(mockStub.markTitleAsWatched).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("should throw error if DO returns null", async () => {
|
||||
mockStub.markEpisodeAsWatched.mockResolvedValue(null);
|
||||
|
||||
await expect(markEpisodeAsWatched("token", 1, 12, false)).rejects.toThrow(
|
||||
"Failed to mark episode as watched",
|
||||
);
|
||||
});
|
||||
|
||||
it("should return formatted user data", async () => {
|
||||
const mockUser = { id: 1, statistics: { anime: { count: 10 } } };
|
||||
mockStub.markEpisodeAsWatched.mockResolvedValue({ user: mockUser });
|
||||
|
||||
const result = await markEpisodeAsWatched("token", 1, 12, false);
|
||||
|
||||
expect(result).toEqual({
|
||||
...mockUser,
|
||||
statistics: { count: 10 },
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user