Socket
Socket
Sign inDemoInstall

@storybook/addon-interactions

Package Overview
Dependencies
15
Maintainers
29
Versions
773
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @storybook/addon-interactions

Automate, test and debug user interactions


Version published
Maintainers
29
Created

Package description

What is @storybook/addon-interactions?

The @storybook/addon-interactions package is an addon for Storybook that allows developers to test and debug user interactions and play functions within their stories. It provides tools to simulate user events, debug play functions, and track the state of interactions over time.

What are @storybook/addon-interactions's main functionalities?

Simulating user interactions

This feature allows developers to simulate user events such as clicks, typing, and more within their stories. The code sample demonstrates how to simulate a click event on a button with the text 'Click me' using the `userEvent` object from '@storybook/testing-library'.

import { userEvent, within } from '@storybook/testing-library';
export const MyStory = {...};
MyStory.play = async ({ canvasElement }) => {
  const canvas = within(canvasElement);
  await userEvent.click(canvas.getByRole('button', { name: /click me/i }));
};

Debugging play functions

This feature enables developers to debug their play functions by using assertions to test the results of interactions. The code sample shows how to use `expect` from '@storybook/jest' to assert that an `onClick` handler was called during the play function execution.

import { expect } from '@storybook/jest';
export const MyStory = {...};
MyStory.play = async ({ args }) => {
  // Simulate interactions and check the results
  expect(args.onClick).toHaveBeenCalled();
};

Tracking interaction state

This feature provides a way to track the state of interactions over time. Developers can use `useAddonState` from '@storybook/addons' to get and set the state within their play functions. The code sample illustrates how to set a state indicating that an element was clicked.

import { useAddonState } from '@storybook/addons';
export const MyStory = {...};
MyStory.play = async () => {
  const [interactionsState, setInteractionsState] = useAddonState('interactions', {});
  // Update the state based on interactions
  setInteractionsState({ clicked: true });
};

Other packages similar to @storybook/addon-interactions

Changelog

Source

6.5.15 (December 20, 2022)

Bug Fixes
  • Support Angular 15.0.4 #20287
  • CLI: execute automigrations when pressing enter in the prompts #20208

Readme

Source

Storybook Addon Interactions

Storybook Addon Interactions enables visual debugging of interactions and tests in Storybook.

Screenshot

Installation

Install this addon by adding the @storybook/addon-interactions dependency:

yarn add -D @storybook/addon-interactions @storybook/jest @storybook/testing-library

within .storybook/main.js:

module.exports = {
  addons: ['@storybook/addon-interactions'],
};

Note that @storybook/addon-interactions must be listed after @storybook/addon-actions or @storybook/addon-essentials.

Experimental step debugging

Step debugging features are experimental and disabled by default. To enable them:

// main.js
module.exports = {
  features: {
    interactionsDebugger: true,
  },
};

Usage

Interactions relies on "instrumented" versions of Jest and Testing Library, that you import from @storybook/jest and @storybook/testing-library instead of their original package. You can then use these libraries in your play function.

import { Button } from './Button';
import { expect } from '@storybook/jest';
import { within, userEvent } from '@storybook/testing-library';

export default {
  title: 'Button',
  component: Button,
  argTypes: {
    onClick: { action: true },
  },
};

const Template = (args) => <Button {...args} />;

export const Demo = Template.bind({});
Demo.play = async ({ args, canvasElement }) => {
  const canvas = within(canvasElement);
  await userEvent.click(canvas.getByRole('button'));
  await expect(args.onClick).toHaveBeenCalled();
};

In order to enable step-through debugging, calls to userEvent.*, fireEvent, findBy*, waitFor* and expect have to be await-ed. While debugging, these functions return a Promise that won't resolve until you continue to the next step.

While you can technically use screen, it's recommended to use within(canvasElement). Besides giving you a better error message when a DOM element can't be found, it will also ensure your play function is compatible with Storybook Docs.

Any args that are marked as an action (typically via argTypes or argTypesRegex) will be automatically wrapped in a Jest mock function so you can assert invocations. See addon-actions for how to setup actions.

Keywords

FAQs

Last updated on 20 Dec 2022

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc