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,22 +1,18 @@
|
||||
import { beforeEach, describe, expect, it } from "vitest";
|
||||
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
||||
import { blogContainer } from "../../di/container";
|
||||
import { BLOG_SYMBOLS } from "../../di/symbols";
|
||||
import { MockArticlesRepository } from "../../infrastructure/repositories/articles.repository.mock";
|
||||
import type { IArticlesRepository } from "../../application/repositories/articles.repository.interface";
|
||||
import { BlogModule } from "../../di/module";
|
||||
import { blogRouter } from "./router";
|
||||
import { articleFactory } from "../../__factories__/article.factory.js";
|
||||
|
||||
// The router resolves controllers from blogContainer (a singleton).
|
||||
// We reload the module between tests to get a fresh MockArticlesRepository.
|
||||
describe("blogRouter", () => {
|
||||
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);
|
||||
blogContainer.unbindAll();
|
||||
blogContainer.load(BlogModule);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
blogContainer.unbindAll();
|
||||
});
|
||||
|
||||
it("exposes articleBySlug, listArticles, createArticle procedures", () => {
|
||||
@@ -26,19 +22,24 @@ describe("blogRouter", () => {
|
||||
expect(procedureNames).toContain("createArticle");
|
||||
});
|
||||
|
||||
it("articleBySlug returns the article when present", async () => {
|
||||
await repo.createArticle(
|
||||
articleFactory.build({ id: "1", title: "T", slug: "t", authorId: "u1" }),
|
||||
);
|
||||
|
||||
const caller = blogRouter.createCaller({});
|
||||
const result = await caller.articleBySlug({ slug: "t" });
|
||||
expect(result?.id).toBe("1");
|
||||
});
|
||||
|
||||
it("listArticles returns all articles when no input is given", async () => {
|
||||
it("listArticles returns empty array by default", async () => {
|
||||
const caller = blogRouter.createCaller({});
|
||||
const result = await caller.listArticles();
|
||||
expect(result).toEqual([]);
|
||||
});
|
||||
|
||||
it("createArticle then articleBySlug returns the article", async () => {
|
||||
const caller = blogRouter.createCaller({});
|
||||
|
||||
const created = await caller.createArticle({
|
||||
title: "Router Test Article",
|
||||
content: null,
|
||||
authorId: "u1",
|
||||
slug: "router-test",
|
||||
});
|
||||
expect(created.slug).toBe("router-test");
|
||||
|
||||
const fetched = await caller.articleBySlug({ slug: "router-test" });
|
||||
expect(fetched.id).toBe(created.id);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user