Files
agentic-dev/packages/blog/tests/articles.feature.test.ts
Danijel Martinek 700d311052 refactor(blog): factory-style use cases + per-use-case controllers + getArticleBySlug
- Use cases (create-article, get-articles, get-article-by-slug NEW) → factory functions
- Controllers split: articles.controller.ts → 3 single-responsibility files
- DI module wires factories with .toDynamicValue()
- tRPC router resolves controllers via container

Refactor log: §2, §3, §4.1, §4.2, §5.1
Spec: §6.2

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-06 00:08:15 +02:00

64 lines
2.7 KiB
TypeScript

// Feature-level test: exercises the full slice
// use case factory chain → mock repo
// Tests the full dependency chain via direct injection (no container rebinding).
import { describe, expect, it } from "vitest";
import { MockArticlesRepository } from "../src/infrastructure/repositories/articles.repository.mock";
import { getArticlesUseCase } from "../src/application/use-cases/get-articles.use-case";
import { createArticleUseCase } from "../src/application/use-cases/create-article.use-case";
import { getArticleBySlugUseCase } from "../src/application/use-cases/get-article-by-slug.use-case";
import { getArticlesController } from "../src/interface-adapters/controllers/get-articles.controller";
import { createArticleController } from "../src/interface-adapters/controllers/create-article.controller";
import { getArticleBySlugController } from "../src/interface-adapters/controllers/get-article-by-slug.controller";
import { ArticleNotFoundError } from "../src/entities/errors/article";
describe("blog feature: article end-to-end via direct injection", () => {
function buildChain() {
const repo = new MockArticlesRepository();
const createUseCase = createArticleUseCase(repo);
const getArticlesUC = getArticlesUseCase(repo);
const getBySlugUC = getArticleBySlugUseCase(repo);
const createCtrl = createArticleController(createUseCase);
const listCtrl = getArticlesController(getArticlesUC);
const bySlugCtrl = getArticleBySlugController(getBySlugUC);
return { createCtrl, listCtrl, bySlugCtrl };
}
it("creates an article via controller, then fetches it back by slug", async () => {
const { createCtrl, bySlugCtrl } = buildChain();
const created = await createCtrl({
title: "The Vertical Refactor",
content: { type: "doc", children: [] },
authorId: "u1",
slug: "vertical-refactor",
});
expect(created.id).toBeTruthy();
expect(created.slug).toBe("vertical-refactor");
const fetched = await bySlugCtrl({ slug: "vertical-refactor" });
expect(fetched.id).toBe(created.id);
expect(fetched.title).toBe("The Vertical Refactor");
});
it("listArticles filters by status", async () => {
const { createCtrl, listCtrl } = buildChain();
await createCtrl({
title: "Draft One",
content: null,
authorId: "u1",
});
const draftOnly = await listCtrl({ status: "draft" });
expect(draftOnly).toHaveLength(1);
const publishedOnly = await listCtrl({ status: "published" });
expect(publishedOnly).toHaveLength(0);
});
it("getArticleBySlugController throws ArticleNotFoundError for missing article", async () => {
const { bySlugCtrl } = buildChain();
await expect(bySlugCtrl({ slug: "missing-slug" })).rejects.toBeInstanceOf(ArticleNotFoundError);
});
});