-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
dev(storybook): added plop for starting new stories
- Loading branch information
scottrippey
committed
Sep 20, 2023
1 parent
994f1b4
commit 3c4ded3
Showing
4 changed files
with
754 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
packages/nextjs/__plop_templates/COMPONENT.stories.tsx.hbs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`, | ||
} | ||
] | ||
}); | ||
} | ||
|
||
|
Oops, something went wrong.