Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide user = userEvent.setup() to Play function #17988

Open
ph-fritsche opened this issue Apr 17, 2022 · 3 comments
Open

Provide user = userEvent.setup() to Play function #17988

ph-fritsche opened this issue Apr 17, 2022 · 3 comments

Comments

@ph-fritsche
Copy link

Is your feature request related to a problem? Please describe

user-event@14 allows to create a session per userEvent.setup().
The methods on the object share an input device state. I.e.:

  const user = userEvent.setup()
  await user.keyboard('{Shift>}')
  await user.click(screen.getElementByRole('button')) // This would be a click with shiftKey=true

Applying .setup() and using these methods is recommended.

Describe the solution you'd like

Add user to renderContext.storyContext during the rendering phase.

This should probably be injected by @storybook/testing-library.

Being able to get user per play: ({user}) => { /*... */ } would encourage people to use the recommended approach because it would also be the easiest.

Are you able to assist to bring the feature to reality?

Yes, I can add the necessary code if someone can guide me through your codebase and package structure.

Additional context

I've experimented with Storybook 6.5-alpha and the developer experience improved a lot as many things work out of the box.
It would be great if this change could be added in 6.5 along with the upgrade in storybookjs/testing-library#18 .

@mmirus
Copy link

mmirus commented Aug 21, 2024

👋 hey there! Should this be reopened? From what I can tell, the changes that completed this were rolled back before #28368 was merged.

@kasperpeulen kasperpeulen reopened this Aug 22, 2024
@kasperpeulen
Copy link
Contributor

That is correct. We have to make @storybook/test a proper addon first, as now the loaders of @storybook/test are only applied if you import @storybook/test in your stories file. However, this change makes sure that you often don't need to import @storybook/test.

This is an example, how you can do this in userland:
https://github.com/storybookjs/storybook-rsc-demo/blob/7e8e835f4d9df81fd8d0c033016ff16e064c3cee/.storybook/preview.tsx#L22-L30

https://github.com/storybookjs/storybook-rsc-demo/blob/7e8e835f4d9df81fd8d0c033016ff16e064c3cee/.storybook/preview.tsx#L38-L42

Which also has the advantage that you can set your own userEvent options.

@mmirus
Copy link

mmirus commented Aug 22, 2024

Thank you! As a stopgap, I just plopped this into the play function where I needed it:

  play: async ({ canvasElement, step, parameters }) => {
    const canvas = within(canvasElement);

    /**
     * TODO: refactor to destructure userEvent from context; then we will not
     * need to handle `setup()` ourselves.
     * See https://github.com/storybookjs/storybook/issues/17988
     */
    const user = userEvent.setup();

    // ... the rest of the test

PS - that userEvent is just the one imported from @storybook/test.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants