Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Storybook: Use plop file generator for creating new Stories #200

Merged
merged 3 commits into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"main": "index.js",
"license": "MIT",
"scripts": {
"plop": "plop",
"dev:nextjs": "pnpm run --filter ./packages/nextjs dev",
"next:dev": "pnpm run --filter ./packages/nextjs next:dev",
"next:dev:mock": "pnpm run --filter ./packages/nextjs next:dev:mock",
Expand All @@ -30,8 +31,11 @@
"eslint": "^8.31.0",
"eslint-config-prettier": "^8.6.0",
"eslint-plugin-prettier": "^4.2.1",
"glob": "^10.3.4",
"husky": "^8.0.3",
"inquirer-autocomplete-prompt": "^3.0.0",
"lint-staged": "^13.1.0",
"plop": "^4.0.0",
"prettier": "^2.8.2",
"start-server-and-test": "^1.15.2",
"typescript": "4.9.4"
Expand Down
46 changes: 46 additions & 0 deletions packages/nextjs/__plop_templates/COMPONENT.stories.tsx.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import type { Meta, StoryObj } from ".storybook/types";

import { screen, userEvent, waitFor, within } from "@storybook/testing-library";
import { expect, jest } from "@storybook/jest";

import { {{COMPONENT.BASENAME}} } from "./{{COMPONENT.BASENAME}}";

const meta: Meta<typeof {{COMPONENT.BASENAME}}> = {
component: {{COMPONENT.BASENAME}},
args: {},
};

export default meta;

type Story = StoryObj<typeof {{COMPONENT.BASENAME}}>;

export const Default: Story = {
async play({ canvasElement, step }) {
const ui = wrap(canvasElement);
await step("TODO: add unit tests", async () => {
expect(ui.TODO).toBeVisible();
});
},
};

export const Variant: Story = {
args: {
// TODO: Customize the variant args here
},
async play({ canvasElement, step }) {
const ui = wrap(canvasElement);
await step("TODO: add unit tests", async () => {
expect(ui.TODO).toBeVisible();
});
},
};

/** Encapsulate all UI elements here for easier testing */
function wrap(canvasElement: HTMLElement) {
const container = within(canvasElement);
return {
get TODO() {
return container.getByRole("TODO");
},
};
}
56 changes: 56 additions & 0 deletions plopfile.mjs
scottrippey marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import path from "node:path";
import * as glob from "glob";
import autocompletePrompt from "inquirer-autocomplete-prompt";

const PACKAGES_NEXTJS = "./packages/nextjs";

/**
* @param {import("plop").NodePlopAPI} plop
*/
export default function (plop) {
plop.setPrompt("autocomplete", autocompletePrompt);
plop.setGenerator("stories", {
description: "Generates a Storybook stories file for a component",
prompts: [
{
name: "COMPONENT",
message: "Create a Stories file for which component? (you can search with wildcards)",
type: "autocomplete",
loop: false,
async source(answers, searchTerms) {
return glob
.sync(`components/**/*${searchTerms || ""}*`, {
cwd: PACKAGES_NEXTJS,
nodir: true,
nocase: true,
})
.map((file) => {
const COMPONENT = {
FILE: file,
DIRNAME: path.dirname(file),
BASENAME: path.basename(file, path.extname(file)),
};
return {
name: file,
value: COMPONENT,
};
});
},
validate(input, _answers) {
const COMPONENT = input.value;
if ([".stories", ".test"].some((end) => COMPONENT.BASENAME.endsWith(end))) {
return `This does not appear to be a Component file: "${input.name}"`;
}
return true;
},
},
],
actions: [
{
type: "add",
templateFile: `${PACKAGES_NEXTJS}/__plop_templates/COMPONENT.stories.tsx.hbs`,
path: `${PACKAGES_NEXTJS}/{{COMPONENT.DIRNAME}}/{{COMPONENT.BASENAME}}.stories.tsx`,
},
],
});
}
Loading