Files
agentic-dev/packages/core-eslint/rules/component-must-have-story.test.js
Danijel Martinek f77e6ea881 chore(template): clean-slate template snapshot from bb4a0c7
Curated, product-agnostic snapshot of the post-story-04 tree: demo
content deleted, auth-only reference feature, web-next shell, all gates
green. Product-specific docs, ADRs 027-029, PRDs/epics/archive, editor
library traces, and product naming are curated out; generic template
repairs (coverage provider devDeps, root test:coverage script, live
lint fixes, root-only release-please) are kept. See TEMPLATE.md for
provenance, curation list, and usage.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016j8z4VHjedXDTjEDNg7qHK
2026-07-12 20:40:54 +02:00

111 lines
3.3 KiB
JavaScript

import { describe, it } from "vitest";
import { RuleTester } from "eslint";
import path from "node:path";
import os from "node:os";
import fs from "node:fs";
import rule from "./component-must-have-story.js";
function makeFixture({ withStory, location = "core-ui" }) {
const root = fs.mkdtempSync(path.join(os.tmpdir(), "cmhs-"));
const dir =
location === "core-ui"
? path.join(root, "packages", "core-ui", "src", "atoms", "Button")
: path.join(root, "packages", "demo", "src", "ui", "atoms", "Button");
fs.mkdirSync(dir, { recursive: true });
const component = path.join(dir, "Button.tsx");
fs.writeFileSync(
component,
`export const Button = () => <button>x</button>;`,
);
if (withStory) {
fs.writeFileSync(
path.join(dir, "Button.stories.tsx"),
`export default { title: "Button" };`,
);
}
return { component };
}
const tester = new RuleTester({
languageOptions: {
parser: await import("@typescript-eslint/parser"),
ecmaVersion: "latest",
sourceType: "module",
parserOptions: { ecmaFeatures: { jsx: true } },
},
});
describe("component-must-have-story", () => {
it("passes for a core-ui component with a sibling .stories.tsx", () => {
const { component } = makeFixture({ withStory: true });
tester.run("component-must-have-story", rule, {
valid: [
{ filename: component, code: fs.readFileSync(component, "utf8") },
],
invalid: [],
});
});
it("fires for a core-ui component without a sibling .stories.tsx", () => {
const { component } = makeFixture({ withStory: false });
tester.run("component-must-have-story", rule, {
valid: [],
invalid: [
{
filename: component,
code: fs.readFileSync(component, "utf8"),
errors: [{ messageId: "missingStory" }],
},
],
});
});
it("passes for a feature ui component with a sibling .stories.tsx", () => {
const { component } = makeFixture({ withStory: true, location: "feature" });
tester.run("component-must-have-story", rule, {
valid: [
{ filename: component, code: fs.readFileSync(component, "utf8") },
],
invalid: [],
});
});
it("is a no-op for index.tsx", () => {
const root = fs.mkdtempSync(path.join(os.tmpdir(), "cmhs-"));
const dir = path.join(
root,
"packages",
"core-ui",
"src",
"atoms",
"Button",
);
fs.mkdirSync(dir, { recursive: true });
const file = path.join(dir, "index.tsx");
fs.writeFileSync(file, `export * from "./Button";`);
tester.run("component-must-have-story", rule, {
valid: [{ filename: file, code: fs.readFileSync(file, "utf8") }],
invalid: [],
});
});
it("is a no-op for files outside packages/core-ui/ and packages/*/src/ui/", () => {
const root = fs.mkdtempSync(path.join(os.tmpdir(), "cmhs-"));
const dir = path.join(
root,
"packages",
"auth",
"src",
"application",
"use-cases",
);
fs.mkdirSync(dir, { recursive: true });
const file = path.join(dir, "sign-in.use-case.tsx");
fs.writeFileSync(file, `export const x = 1;`);
tester.run("component-must-have-story", rule, {
valid: [{ filename: file, code: fs.readFileSync(file, "utf8") }],
invalid: [],
});
});
});