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>
This commit is contained in:
@@ -1,32 +1,33 @@
|
||||
// Feature-level test: exercises the full slice
|
||||
// tRPC procedure -> controller -> use-case -> mock repo
|
||||
// without going through a network or the actual Payload Local API.
|
||||
// Verifies that the layers are correctly wired through the per-feature DI container.
|
||||
// use case factory chain → mock repo
|
||||
// Tests the full dependency chain via direct injection (no container rebinding).
|
||||
|
||||
import { beforeEach, describe, expect, it } from "vitest";
|
||||
import { blogContainer } from "../src/di/container";
|
||||
import { BLOG_SYMBOLS } from "../src/di/symbols";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { MockArticlesRepository } from "../src/infrastructure/repositories/articles.repository.mock";
|
||||
import type { IArticlesRepository } from "../src/application/repositories/articles.repository.interface";
|
||||
import { blogRouter } from "../src/integrations/api/router";
|
||||
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-by-slug end-to-end", () => {
|
||||
let repo: MockArticlesRepository;
|
||||
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 };
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
if (blogContainer.isBound(BLOG_SYMBOLS.IArticlesRepository)) {
|
||||
blogContainer.unbind(BLOG_SYMBOLS.IArticlesRepository);
|
||||
}
|
||||
repo = new MockArticlesRepository();
|
||||
blogContainer
|
||||
.bind<IArticlesRepository>(BLOG_SYMBOLS.IArticlesRepository)
|
||||
.toConstantValue(repo);
|
||||
});
|
||||
it("creates an article via controller, then fetches it back by slug", async () => {
|
||||
const { createCtrl, bySlugCtrl } = buildChain();
|
||||
|
||||
it("creates an article via tRPC, then fetches it back by slug", async () => {
|
||||
const caller = blogRouter.createCaller({});
|
||||
|
||||
const created = await caller.createArticle({
|
||||
const created = await createCtrl({
|
||||
title: "The Vertical Refactor",
|
||||
content: { type: "doc", children: [] },
|
||||
authorId: "u1",
|
||||
@@ -35,22 +36,28 @@ describe("blog feature: article-by-slug end-to-end", () => {
|
||||
expect(created.id).toBeTruthy();
|
||||
expect(created.slug).toBe("vertical-refactor");
|
||||
|
||||
const fetched = await caller.articleBySlug({ slug: "vertical-refactor" });
|
||||
expect(fetched?.id).toBe(created.id);
|
||||
expect(fetched?.title).toBe("The 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 caller = blogRouter.createCaller({});
|
||||
await caller.createArticle({
|
||||
const { createCtrl, listCtrl } = buildChain();
|
||||
|
||||
await createCtrl({
|
||||
title: "Draft One",
|
||||
content: null,
|
||||
authorId: "u1",
|
||||
});
|
||||
const draftOnly = await caller.listArticles({ status: "draft" });
|
||||
const draftOnly = await listCtrl({ status: "draft" });
|
||||
expect(draftOnly).toHaveLength(1);
|
||||
|
||||
const publishedOnly = await caller.listArticles({ status: "published" });
|
||||
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);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user