Files
agentic-dev/packages/core-eslint/rules/component-must-have-story.test.js
2026-05-13 07:40:19 +02:00

85 lines
3.1 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: [],
});
});
});