Skip to content

Commit 2938095

Browse files
committed
Add testing library add-on for Storybook
Storybook's play function allows initializing the components to specific states by mimicking user actions. Ideally components' hard-to-reach states can be shown without user having to manually interact with the component. The examples included in this commit are rather lame, but they work as an showcase for what can be done with Storybook. Storybook also provides tools for visual, accessibility and snapshot testing, but the current components didn't entice me to check these out at this time. Maybe later when more suitable components are created.
1 parent c23f39e commit 2938095

File tree

4 files changed

+360
-7
lines changed

4 files changed

+360
-7
lines changed

apps/storybook/.storybook/main.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ module.exports = {
1010
"addons": [
1111
"@storybook/addon-links",
1212
"@storybook/addon-essentials",
13+
"@storybook/addon-interactions",
1314
"@snek-at/storybook-addon-chakra-ui", // Must come after @storybook addons
1415
],
1516
"framework": "@storybook/react",

apps/storybook/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,11 @@
2323
"@babel/plugin-transform-spread": "^7.16.0",
2424
"@storybook/addon-actions": "^6.4.3",
2525
"@storybook/addon-essentials": "^6.4.3",
26+
"@storybook/addon-interactions": "^6.4.3",
2627
"@storybook/addon-links": "^6.4.3",
28+
"@storybook/jest": "^0.0.5",
2729
"@storybook/react": "^6.4.3",
30+
"@storybook/testing-library": "^0.0.7",
2831
"babel-loader": "^8.2.3",
2932
"babel-plugin-macros": "^3.1.0",
3033
"babel-plugin-polyfill-corejs3": "^0.4.0",
Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { MultiSelect } from "@thunderstore/components";
22
import { ComponentStory, ComponentMeta } from "@storybook/react";
3+
import { within, userEvent } from "@storybook/testing-library";
34
import React from "react";
45

56
import * as SelectStories from "./Select.stories";
@@ -10,7 +11,29 @@ const Template: ComponentStory<typeof MultiSelect> = (args) => (
1011
<MultiSelect {...args} />
1112
);
1213

13-
const Multi = Template.bind({});
14-
Multi.args = { ...SelectStories.Select.args };
14+
const NoneSelected = Template.bind({});
15+
NoneSelected.args = { ...SelectStories.Select.args };
1516

16-
export { meta as default, Multi as MultiSelect };
17+
const OneSelected = Template.bind({});
18+
OneSelected.args = { ...SelectStories.Select.args };
19+
OneSelected.play = ({ canvasElement }) => {
20+
const canvas = within(canvasElement);
21+
const input = canvas.getByPlaceholderText("Search...");
22+
userEvent.click(input);
23+
const options = canvas.getAllByRole("option");
24+
userEvent.click(options[1]);
25+
};
26+
27+
const MultipleSelected = Template.bind({});
28+
MultipleSelected.args = { ...SelectStories.Select.args };
29+
MultipleSelected.play = ({ canvasElement }) => {
30+
const canvas = within(canvasElement);
31+
const input = canvas.getByPlaceholderText("Search...");
32+
userEvent.click(input);
33+
const options = canvas.getAllByRole("option");
34+
userEvent.click(options[1]);
35+
userEvent.click(input);
36+
userEvent.click(options[2]);
37+
};
38+
39+
export { meta as default, NoneSelected, OneSelected, MultipleSelected };

0 commit comments

Comments
 (0)