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

Add renderer option to storyshots #2414

Merged
merged 15 commits into from
Dec 2, 2017
Merged

Add renderer option to storyshots #2414

merged 15 commits into from
Dec 2, 2017

Conversation

tobilen
Copy link
Contributor

@tobilen tobilen commented Dec 1, 2017

Issue: #2408

What I did

Added two options to the options passed to initStoryshots, renderer and serializer. Their usage is documented in the readme, but in short, you can pass custom renderer and serializer functions to the storyshot addon.
This is probably a bit easier to use than the test option, which requires you to pass an entire snapshotting test.

How to test

In addition to the storyshot tests currently in the addon, i added one where enzymes mount renderer and the serializer from enzyme-to-json are passed.

Does this need a new example in the kitchen sink apps?
No

Does this need an update to the documentation?
Yes, which has been added.

@codecov
Copy link

codecov bot commented Dec 1, 2017

Codecov Report

Merging #2414 into release/3.3 will increase coverage by 0.05%.
The diff coverage is 58.33%.

Impacted file tree graph

@@               Coverage Diff               @@
##           release/3.3    #2414      +/-   ##
===============================================
+ Coverage        18.72%   18.77%   +0.05%     
===============================================
  Files              348      348              
  Lines             8198     8202       +4     
  Branches           906      917      +11     
===============================================
+ Hits              1535     1540       +5     
+ Misses            5995     5932      -63     
- Partials           668      730      +62
Impacted Files Coverage Δ
addons/storyshots/src/index.js 80% <100%> (+0.4%) ⬆️
addons/storyshots/src/test-bodies.js 61.9% <37.5%> (+11.9%) ⬆️
app/react/src/server/utils.js 0% <0%> (-53.58%) ⬇️
lib/components/src/table/table.js 0% <0%> (ø) ⬆️
addons/a11y/src/components/Report/Rules.js 0% <0%> (ø) ⬆️
...ddons/actions/src/containers/ActionLogger/index.js 2.89% <0%> (ø) ⬆️
lib/ui/src/modules/ui/configs/handle_routing.js 24.21% <0%> (ø) ⬆️
addons/knobs/src/components/PropForm.js 9.3% <0%> (ø) ⬆️
lib/ui/src/modules/api/actions/api.js 51.85% <0%> (ø) ⬆️
.../ui/src/modules/ui/components/addon_panel/index.js 34.78% <0%> (ø) ⬆️
... and 52 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 02b566a...11800f8. Read the comment docs.

@@ -1,5 +1,5 @@
{
"presets": ["env", "react"],
"presets": ["env", "stage-0", "react"],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is it needed? Can we include the particular transform instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for the destructuring assignment here: https://github.com/storybooks/storybook/pull/2414/files#diff-75fdf0f2c6757366489b97c6898ea585R6

i would have used stage-2, but storybook itself pulls in stage-0, so i went with that to stay consistent

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I see. I wonder if we can remove this nested .babelrc to avoid confusion

@ndelangen what do you think?

@Hypnosphi
Copy link
Member

Looks great! Let's use this feature for storyshots of example/cra-kitchen-sink

Copy link
Contributor Author

@tobilen tobilen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i've extended the demo project to use it. i went for jest configuration in package.json over initializing everything in the initSnapshot file, since that is the cleaner way to do it imo

@@ -1,5 +1,5 @@
{
"presets": ["env", "react"],
"presets": ["env", "stage-0", "react"],
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for the destructuring assignment here: https://github.com/storybooks/storybook/pull/2414/files#diff-75fdf0f2c6757366489b97c6898ea585R6

i would have used stage-2, but storybook itself pulls in stage-0, so i went with that to stay consistent

import Enzyme from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';

Enzyme.configure({ adapter: new Adapter() });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have this setup on root level, no need to duplicate it here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as #2414 (comment)

@@ -39,6 +39,9 @@
"@storybook/components": "^3.3.0-alpha.4",
"@storybook/react": "^3.3.0-alpha.4",
"babel-jest": "^21.2.0",
"enzyme": "^3.2.0",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to have a single enzyme instance across the codebase, so it shouldn’t be added to package dependencies

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i know. but i figured an "example project" should include the necessary dependencies? its a lot more difficult to understand if i need to look outside of the project for its code to work. thats why i included enzyme setup there

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, makes sense

test: multiSnapshotWithOptions({
createNodeMock,
}),
renderer,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we support custom renderers for multisnapshots as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah sure. done

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like it still stores all the snapshots in one file

Copy link
Contributor Author

@tobilen tobilen Dec 2, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it does. i'm currently debugging it. looks like it did not work in the first place. the multisnapshot function doesnt seem to be called correctly

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should work now. we cant use enzymes mount since that renders out the context prop (includes the file path, which is different on every machine). We also need to pass a serializer since jest-specific-snapshots doesnt use the projects serializer

mount includes context props, which means current file paths will be printed to snapshots. those are machine-dependant and differ on every enviroment. we dont want that.
onClick={[Function]}
style={Object {}}
>
<button>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have an idea why Enzyme strips attributes?

Copy link
Contributor Author

@tobilen tobilen Dec 2, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

its the renderer specified in the tests. you have shallow which shallowrenders, mount which more or less renders the entire vdom, and render (uses cheerio under the hood) which just prints out html. see https://github.com/airbnb/enzyme/blob/master/docs/api/render.md

like i said, we cant use mount with multisnapshot because the snapshots then include the context prop, and part of that is the file path. and the file path changes depending on what the absolute path on the system the snapshot is stored at is.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

im not very familiar with the internals of jest-specific-snapshots, but if there was a way for it to pass a relative path in the context, that would eliminate the problem. its where i see the solution to the problem at least.
i wouldnt want to modify the output of the renderer (like replacing the filepath with an empty string or somesuch), which is the only alternative i can think of

@Hypnosphi Hypnosphi merged commit 16cd900 into storybookjs:release/3.3 Dec 2, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants