feat(blog): add getArticlesUseCase (test red until DI + mock repo exist)

This commit is contained in:
2026-05-04 22:13:00 +02:00
parent ab11f42e8d
commit b0dab254d1
2 changed files with 80 additions and 0 deletions

View File

@@ -0,0 +1,64 @@
import { beforeEach, describe, expect, it } from "vitest";
import { blogContainer } from "@/di/container";
import { BLOG_SYMBOLS } from "@/di/symbols";
import type { IArticlesRepository } from "@/application/repositories/articles-repository.interface";
import { MockArticlesRepository } from "@/infrastructure/repositories/mock-articles.repository";
import { getArticlesUseCase } from "./get-articles.use-case";
describe("getArticlesUseCase", () => {
let repo: MockArticlesRepository;
beforeEach(() => {
if (blogContainer.isBound(BLOG_SYMBOLS.IArticlesRepository)) {
blogContainer.unbind(BLOG_SYMBOLS.IArticlesRepository);
}
repo = new MockArticlesRepository();
blogContainer
.bind<IArticlesRepository>(BLOG_SYMBOLS.IArticlesRepository)
.toConstantValue(repo);
});
it("returns all articles with no filters", async () => {
const now = new Date();
await repo.createArticle({
id: "1",
title: "A",
slug: "a",
content: null,
status: "draft",
authorId: "u1",
createdAt: now,
updatedAt: now,
});
const result = await getArticlesUseCase();
expect(result).toHaveLength(1);
expect(result[0]?.id).toBe("1");
});
it("filters by status", async () => {
const now = new Date();
await repo.createArticle({
id: "1",
title: "A",
slug: "a",
content: null,
status: "draft",
authorId: "u1",
createdAt: now,
updatedAt: now,
});
await repo.createArticle({
id: "2",
title: "B",
slug: "b",
content: null,
status: "published",
authorId: "u1",
createdAt: now,
updatedAt: now,
});
const result = await getArticlesUseCase({ status: "published" });
expect(result).toHaveLength(1);
expect(result[0]?.id).toBe("2");
});
});

View File

@@ -0,0 +1,16 @@
import type { Article } from "@/entities/article";
import { blogContainer } from "@/di/container";
import { BLOG_SYMBOLS } from "@/di/symbols";
import type { IArticlesRepository } from "@/application/repositories/articles-repository.interface";
export async function getArticlesUseCase(options?: {
status?: string;
authorId?: string;
limit?: number;
offset?: number;
}): Promise<Article[]> {
const repo = blogContainer.get<IArticlesRepository>(
BLOG_SYMBOLS.IArticlesRepository,
);
return repo.getArticles(options);
}