61 lines
2.0 KiB
TypeScript
61 lines
2.0 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
import { ZodError } from "zod";
|
|
import {
|
|
getArticlesUseCase,
|
|
getArticlesOutputSchema,
|
|
} from "@/application/use-cases/get-articles.use-case";
|
|
import { MockArticlesRepository } from "@/infrastructure/repositories/articles.repository.mock";
|
|
import { articleFactory } from "@/__factories__/article.factory";
|
|
|
|
describe("getArticlesUseCase", () => {
|
|
it("returns all articles with no filters", async () => {
|
|
const repo = new MockArticlesRepository();
|
|
articleFactory.reset();
|
|
await repo.createArticle(
|
|
articleFactory.build({ id: "1", title: "A", slug: "a" }),
|
|
);
|
|
|
|
const useCase = getArticlesUseCase(repo);
|
|
const result = await useCase({});
|
|
expect(result).toHaveLength(1);
|
|
expect(result[0]?.id).toBe("1");
|
|
});
|
|
|
|
it("filters by status", async () => {
|
|
const repo = new MockArticlesRepository();
|
|
articleFactory.reset();
|
|
await repo.createArticle(
|
|
articleFactory.build({ id: "1", title: "A", slug: "a", status: "draft" }),
|
|
);
|
|
await repo.createArticle(
|
|
articleFactory.build({
|
|
id: "2",
|
|
title: "B",
|
|
slug: "b",
|
|
status: "published",
|
|
}),
|
|
);
|
|
|
|
const useCase = getArticlesUseCase(repo);
|
|
const result = await useCase({ status: "published" });
|
|
expect(result).toHaveLength(1);
|
|
expect(result[0]?.id).toBe("2");
|
|
});
|
|
});
|
|
|
|
describe("getArticlesUseCase output validation", () => {
|
|
it("throws when the repository returns a malformed article", async () => {
|
|
const repo = new MockArticlesRepository();
|
|
// bypass the mock's createArticle (which is typed) by reaching into _articles directly
|
|
(repo as unknown as { _articles: unknown[] })._articles.push({ id: 123 });
|
|
|
|
const useCase = getArticlesUseCase(repo);
|
|
await expect(useCase({})).rejects.toBeInstanceOf(ZodError);
|
|
});
|
|
|
|
it("exports an output schema that mirrors Article[]", () => {
|
|
expect(getArticlesOutputSchema).toBeDefined();
|
|
expect(getArticlesOutputSchema.safeParse([]).success).toBe(true);
|
|
});
|
|
});
|