Skip to content

Commit ada83a3

Browse files
Make storybook test more robust (#2822)
* Update storybook tests Co-authored-by: Katie Langerman <[email protected]> * Update tests * Update stories Co-authored-by: Katie Langerman <[email protected]>
1 parent 7403a47 commit ada83a3

7 files changed

+31
-19
lines changed

src/Button/Button.features.stories.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ export default {
66
title: 'Components/Button/Features',
77
}
88

9-
export const Default = () => <Button>Default</Button>
10-
119
export const Primary = () => <Button variant="primary">Primary</Button>
1210

1311
export const Danger = () => <Button variant="danger">Danger</Button>

src/Button/Button.stories.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,5 @@ export default {
3737
} as Meta<typeof Button>
3838

3939
export const Playground: Story<typeof Button> = args => <Button {...args}>Default</Button>
40+
41+
export const Default = () => <Button>Default</Button>

src/Button/IconButton.features.stories.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ export default {
66
title: 'Components/IconButton/Features',
77
}
88

9-
export const Default = () => <IconButton icon={HeartIcon} aria-label="Default" />
10-
119
export const Primary = () => <IconButton icon={HeartIcon} variant="primary" aria-label="Primary" />
1210

1311
export const Danger = () => <IconButton icon={HeartIcon} variant="danger" aria-label="Danger" />

src/Button/IconButton.stories.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,5 @@ const meta: Meta<ComponentProps<typeof IconButton>> = {
3838
export default meta
3939

4040
export const Playground: Story<ComponentProps<typeof IconButton>> = args => <IconButton {...args} />
41+
42+
export const Default = () => <IconButton icon={HeartIcon} aria-label="Default" />

src/Button/LinkButton.features.stories.tsx

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,6 @@ export default {
66
title: 'Components/LinkButton/Features',
77
}
88

9-
export const Default = () => (
10-
<Button as="a" href="/">
11-
Default
12-
</Button>
13-
)
14-
159
export const Primary = () => (
1610
<Button as="a" href="/" variant="primary">
1711
Primary

src/Button/LinkButton.stories.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,9 @@ export const Playground: Story<typeof Button> = args => (
3737
Default
3838
</Button>
3939
)
40+
41+
export const Default = () => (
42+
<Button as="a" href="/">
43+
Default
44+
</Button>
45+
)

src/__tests__/storybook.test.tsx

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,35 @@ import groupBy from 'lodash.groupby'
44

55
const ROOT_DIRECTORY = path.resolve(__dirname, '..', '..')
66
// Components opted into the new story format
7-
const allowlist = ['TreeView']
7+
const allowlist = ['ActionList', 'Button', 'IconButton', 'FilteredActionList', 'TreeView']
88
const stories = glob
99
.sync('src/**/*.stories.tsx', {
1010
cwd: ROOT_DIRECTORY,
1111
})
12-
.filter(file => allowlist.some(component => file.includes(component)))
12+
// Filter out deprecated stories
13+
.filter(file => !file.includes('deprecated'))
14+
.filter(file =>
15+
allowlist.some(
16+
component => file.includes(`/${component}.stories.tsx`) || file.includes(`/${component}.features.stories.tsx`),
17+
),
18+
)
1319
.map(file => {
1420
const filepath = path.join(ROOT_DIRECTORY, file)
1521
const type = path.basename(filepath, '.stories.tsx').endsWith('features') ? 'feature' : 'default'
1622
const name = type === 'feature' ? path.basename(file, '.features.stories.tsx') : path.basename(file, '.stories.tsx')
1723

18-
return [name, require(filepath), type]
24+
return {name, story: require(filepath), type, relativeFilepath: path.relative(ROOT_DIRECTORY, filepath)}
1925
})
2026

2127
const components = Object.entries(
22-
groupBy(stories, ([name]) => {
28+
groupBy(stories, ({name}) => {
2329
return name
2430
}),
2531
)
2632

2733
describe.each(components)('%s', (_component, stories) => {
28-
for (const [, story, type] of stories) {
29-
describe(`${story.default.title}`, () => {
34+
for (const {story, type, relativeFilepath} of stories) {
35+
describe(`${story.default.title} (${relativeFilepath})`, () => {
3036
test('title follows naming convention', () => {
3137
// Matches:
3238
// - title: 'Components/TreeView'
@@ -46,17 +52,23 @@ describe.each(components)('%s', (_component, stories) => {
4652
})
4753

4854
if (type === 'default') {
49-
test('exports a "Default" story', () => {
55+
test('exports a Default story', () => {
5056
expect(story.Default).toBeDefined()
5157
})
5258

53-
test('"Default" story does not use args', () => {
59+
test('Default story does not have `args`', () => {
5460
expect(story.Default.args).not.toBeDefined()
5561
})
5662

57-
test('"Default" story does not set `argTypes` on the `Default` story', () => {
63+
test('Default story does not have `argTypes`', () => {
5864
expect(story.Default.argTypes).not.toBeDefined()
5965
})
66+
67+
test('only exports Default and Playground stories', () => {
68+
for (const storyName of Object.keys(story)) {
69+
expect(/^Default$|^(.*)Playground$|^default$/.test(storyName)).toBe(true)
70+
}
71+
})
6072
}
6173
})
6274
}

0 commit comments

Comments
 (0)