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
React: Add renderStory export #11478
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
app/react/render.js
Outdated
const React = require('react'); | ||
|
||
module.exports = { | ||
renderStory(Story) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if I want to add extra args? Consider the following example:
import { render, fireEvent } from '@testing-library/react';
import { Text } from './Button.stories';
it('should respond to click events', () => {
const handleClick = jest.fn();
const instance = render(
<Text {...Text.args} onClick={handleClick} />
);
fireEvent.click(instance.container.firstChild);
expect(handleClick).toHaveBeenCalled();
});
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do you think it should be renderStory(Story, args?)
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that makes more sense to me
@shilman I've realized some things about this that make it not quite bulletproof:
The second problem could be fix I suppose. The first is trickier. WDYT? |
I’m not sure what we do with decorators TBH. Would one of those options be |
Hmm, I think we over-simplified this and should have remembered the earlier conversation with @lifeiscontent. There's a lot to think about here in terms of reliably rendering stories in a external context:
I guess this PR is a first step that only deals with simple args (at this stage). Here's my strawman:
WDYT @shilman? |
This might be a silly question but is |
Also one great benefit of using a component in JSX form with typescript is that you can do something like:
Then both you get autocompletion for props but you get error for missing mandatory props. In the Just trying to bring this to the table, although this will be something for the future. |
Maybe what we want is a function to turn a story into something renderable? Ideally that’d be the export in the first place but all the complexities of args, decorators + parameters makes that sort of impossible. So something like: import storyToComponent from ‘@storybook/react/render’;
import { WithText } from ‘./button.stories’;
const WithTextComponent = storyToComponent(WithText);
render(<WithTextComponent prop={foo}>other text</WithTextComponent>); |
Although that'd be nice, it's not really possible, right? If the component returned is a result of bunch of decorators wrapping the story, you wouldn't be able to easily pass the props and children like |
Hi everyone! Seems like there hasn't been much going on in this issue lately. If there are still questions, comments, or bugs, please feel free to continue the discussion. Unfortunately, we don't have time to get to every issue. We are always open to contributions so please send us a pull request if you would like to help. Inactive issues will be closed after 30 days. Thanks! |
Hey there, it's me again! I am going close this issue to help our maintainers focus on the current development roadmap instead. If the issue mentioned is still a concern, please open a new ticket and mention this old one. Cheers and thanks for using Storybook! |
Issue: we want to make it easy to use CSF exports in testing frameworks. This adds it for React.
What I did
Added
@storybook/react/render
with arenderStory
export. You can use as per the test.How to test
Yes, there is a test.