Skip to content

Commit

Permalink
dev(storybook): added plop for starting new stories
Browse files Browse the repository at this point in the history
  • Loading branch information
scottrippey committed Sep 20, 2023
1 parent 994f1b4 commit 3c4ded3
Show file tree
Hide file tree
Showing 4 changed files with 754 additions and 38 deletions.
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
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

0 comments on commit 3c4ded3

Please sign in to comment.