feat(features): add test factories to all 5 features
Adds src/__factories__/<entity>.factory.ts to auth, blog, marketing-pages, navigation, media. Each factory uses defineFactory from @repo/core-testing with stable date defaults (2026-01-01) so snapshot diffs reflect SUT behavior only. Refactors mechanical inline-fixture tests to use factories. Also adds vitest.config.ts and tsconfig path alias to @repo/media (lacked both), and adds @repo/core-testing devDependency to @repo/media. Spec: §5.1, §6.3
This commit is contained in:
27
packages/blog/src/__factories__/article.factory.test.ts
Normal file
27
packages/blog/src/__factories__/article.factory.test.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import { describe, it, expect, beforeEach } from "vitest";
|
||||
import { articleFactory } from "@/__factories__/article.factory";
|
||||
|
||||
describe("articleFactory", () => {
|
||||
beforeEach(() => articleFactory.reset());
|
||||
|
||||
it("returns an Article with stable defaults", () => {
|
||||
const a = articleFactory.build();
|
||||
expect(a.title).toBe("Article 1");
|
||||
expect(a.slug).toBe("article-1");
|
||||
expect(a.status).toBe("draft");
|
||||
expect(a.createdAt).toEqual(new Date("2026-01-01T00:00:00Z"));
|
||||
});
|
||||
|
||||
it("applies overrides", () => {
|
||||
const a = articleFactory.build({ status: "published", title: "X" });
|
||||
expect(a.status).toBe("published");
|
||||
expect(a.title).toBe("X");
|
||||
});
|
||||
|
||||
it("increments sequence per build", () => {
|
||||
const a = articleFactory.build();
|
||||
const b = articleFactory.build();
|
||||
expect(a.id).toBe("article-1");
|
||||
expect(b.id).toBe("article-2");
|
||||
});
|
||||
});
|
||||
13
packages/blog/src/__factories__/article.factory.ts
Normal file
13
packages/blog/src/__factories__/article.factory.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { defineFactory } from "@repo/core-testing/factory";
|
||||
import type { Article } from "../entities/article.js";
|
||||
|
||||
export const articleFactory = defineFactory<Article>(({ sequence }) => ({
|
||||
id: `article-${sequence}`,
|
||||
title: `Article ${sequence}`,
|
||||
slug: `article-${sequence}`,
|
||||
content: null,
|
||||
status: "draft",
|
||||
authorId: "user-1",
|
||||
createdAt: new Date("2026-01-01T00:00:00Z"),
|
||||
updatedAt: new Date("2026-01-01T00:00:00Z"),
|
||||
}));
|
||||
1
packages/blog/src/__factories__/index.ts
Normal file
1
packages/blog/src/__factories__/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export { articleFactory } from "./article.factory.js";
|
||||
@@ -3,6 +3,7 @@ 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 { articleFactory } from "../../__factories__/article.factory";
|
||||
import { getArticlesUseCase } from "./get-articles.use-case";
|
||||
|
||||
describe("getArticlesUseCase", () => {
|
||||
@@ -16,47 +17,19 @@ describe("getArticlesUseCase", () => {
|
||||
blogContainer
|
||||
.bind<IArticlesRepository>(BLOG_SYMBOLS.IArticlesRepository)
|
||||
.toConstantValue(repo);
|
||||
articleFactory.reset();
|
||||
});
|
||||
|
||||
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,
|
||||
});
|
||||
await repo.createArticle(articleFactory.build({ id: "1", title: "A", slug: "a" }));
|
||||
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,
|
||||
});
|
||||
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 result = await getArticlesUseCase({ status: "published" });
|
||||
expect(result).toHaveLength(1);
|
||||
expect(result[0]?.id).toBe("2");
|
||||
|
||||
Reference in New Issue
Block a user