69 lines
2.1 KiB
TypeScript
69 lines
2.1 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
import { ZodError } from "zod";
|
|
import {
|
|
createArticleUseCase,
|
|
createArticleOutputSchema,
|
|
} from "@/application/use-cases/create-article.use-case";
|
|
import { MockArticlesRepository } from "@/infrastructure/repositories/articles.repository.mock";
|
|
import type { IArticlesRepository } from "@/application/repositories/articles.repository.interface";
|
|
|
|
describe("createArticleUseCase", () => {
|
|
it("creates an article in draft status with auto-generated slug", async () => {
|
|
const repo = new MockArticlesRepository();
|
|
const useCase = createArticleUseCase(repo);
|
|
|
|
const result = await useCase({
|
|
title: "Hello World",
|
|
content: "body",
|
|
authorId: "u1",
|
|
});
|
|
expect(result.title).toBe("Hello World");
|
|
expect(result.slug).toBe("hello-world");
|
|
expect(result.status).toBe("draft");
|
|
expect(result.id).toBeTruthy();
|
|
|
|
const stored = await repo.getArticle(result.id);
|
|
expect(stored).toBeDefined();
|
|
});
|
|
|
|
it("uses provided slug when supplied", async () => {
|
|
const repo = new MockArticlesRepository();
|
|
const useCase = createArticleUseCase(repo);
|
|
|
|
const result = await useCase({
|
|
title: "Whatever",
|
|
content: "body",
|
|
authorId: "u1",
|
|
slug: "custom-slug",
|
|
});
|
|
expect(result.slug).toBe("custom-slug");
|
|
});
|
|
});
|
|
|
|
describe("createArticleUseCase output validation", () => {
|
|
it("throws when repository returns a malformed article", async () => {
|
|
const repo = {
|
|
createArticle: async () => ({ id: 1 }) as unknown as never,
|
|
} as unknown as IArticlesRepository;
|
|
const useCase = createArticleUseCase(repo);
|
|
await expect(
|
|
useCase({ title: "X", authorId: "u1" }),
|
|
).rejects.toBeInstanceOf(ZodError);
|
|
});
|
|
|
|
it("exports an output schema that accepts a valid article shape", () => {
|
|
expect(createArticleOutputSchema).toBeDefined();
|
|
const result = createArticleOutputSchema.safeParse({
|
|
id: "a1",
|
|
title: "Test",
|
|
slug: "test",
|
|
content: null,
|
|
status: "draft",
|
|
authorId: "u1",
|
|
createdAt: new Date(),
|
|
updatedAt: new Date(),
|
|
});
|
|
expect(result.success).toBe(true);
|
|
});
|
|
});
|