Stories isolate and capture component states in a structured manner. While developing a component, you can quickly cycle through the stories to verify the look and feel. Each story specifies all the inputs required to reproduce a specific state. You can even mock context and API calls, allowing you to handle most use cases of a component. But what about states that require user interaction?
For example, clicking a button to open/close a dialog box, dragging a list item to reorder it, or filling out a form to check for validation errors. To test those behaviors, you have to interact with the components as a user would. Interactive stories enable you to automate these interactions using a play function. They are small snippets of code that run once the story finishes rendering, emulating the exact steps a user would take to interact with the component.
The interactions are written using a Storybook-instrumented version of Testing Library and Jest. That gives you a familiar developer-friendly syntax to interact with the DOM and make assertions, but with extra telemetry to help with debugging.
By default, the
@storybook/addon-interactions is already installed and configured if you're adding Storybook for new projects. If you're migrating from a previous version of Storybook, you'll need to install it manually.
Run the following command to install the interactions addon and related dependencies.
.storybook/main.js|ts to the following:
Now when you run Storybook, the Interactions addon will be enabled.
Interactions run as part of the
play function of your stories. We rely on Testing Library to do the heavy lifting.
Make sure to import the Storybook wrappers for Jest and Testing Library rather than importing Jest and Testing Library directly.
The above example uses the
canvasElement to scope your element queries to the current story. It's essential if you want your play functions to eventually be compatible with Storybook Docs, which renders multiple components on the same page. Additionally, the
step function can be used to create labeled groups of interactions.
While you can refer to the Testing Library documentation for details on how to use it, there's an important detail that's different when using the Storybook wrapper: method invocations must be
await-ed. It allows you to step back and forth through your interactions using the debugger.
args that have been marked as an Action, either using the argTypes annotation or the argTypesRegex, will be automatically converted to a Jest mock function (spy). This allows you to make assertions about calls to these functions.
To mock functions in your Storybook stories for reliable and isolated component testing, use the
jest import from
@storybook/jest. This allows you to avoid configuring Jest globally in your project.
This addon contributes the following parameters to Storybook, under the
Disable this addon's behavior. If you wish to disable this addon for the entire Storybook, you should do so when registering
addon-essentials. See the essential addon's docs for more information.
This parameter is most useful to allow overriding at more specific levels. For example, if this parameter is set to
true at the project level, it could then be re-enabled by setting it to
false at the meta (component) or story level.