-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
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
887 Generate snapshot per story file #1584
Changes from 7 commits
917f314
1d54e27
810a63b
5e60a9c
dc8bbd7
8f9926f
3a78efe
9e20cb7
0eb3093
9f6e573
1c1c9db
ca294e9
3eb8390
ab71413
ba02cc8
6bf56cb
e83b2fd
54cb5d1
70c2ebe
9b815a0
96a389b
571373d
7de3acc
be9ab35
8e87fb8
af02e15
fd3d5e5
07146a8
66c0d09
91e0105
6496454
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
*.storyshot |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,30 @@ | ||
import path from 'path'; | ||
import renderer from 'react-test-renderer'; | ||
import shallow from 'react-test-renderer/shallow'; | ||
import 'jest-specific-snapshot'; | ||
|
||
export const snapshotWithOptions = options => ({ story, context }) => { | ||
function getRenderedTree(story, context, options) { | ||
const storyElement = story.render(context); | ||
const tree = renderer.create(storyElement, options).toJSON(); | ||
return renderer.create(storyElement, options).toJSON(); | ||
} | ||
|
||
function getSnapshotFileName(context) { | ||
const fileName = context.storyFileName || __filename; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Surely There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh yeah, actually I need the test path.. I thought it will be a nice fallback.. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah this will just be the file |
||
const { dir, name } = path.parse(fileName); | ||
return path.format({ dir, name, ext: '.storyshot' }); | ||
} | ||
|
||
export const snapshotWithOptions = options => ({ story, context }) => { | ||
const tree = getRenderedTree(story, context, options); | ||
expect(tree).toMatchSnapshot(); | ||
}; | ||
|
||
export const multiSnapshotWithOptions = options => ({ story, context }) => { | ||
const tree = getRenderedTree(story, context, options); | ||
const snapshotFileName = getSnapshotFileName(context); | ||
expect(tree).toMatchSpecificSnapshot(snapshotFileName); | ||
}; | ||
|
||
export const snapshot = snapshotWithOptions({}); | ||
|
||
export function shallowSnapshot({ story, context }) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import initStoryshots, { multiSnapshotWithOptions } from '../src'; | ||
|
||
initStoryshots({ | ||
test: multiSnapshotWithOptions({}), | ||
}); |
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.
Could we just save
filename
alongside the story in core rather than doing this hack?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.
Indeed it's a better way.. I just didn't want to change the core only for the addon needs.. But maybe in this case it's a good solution.
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.
I think it's a pretty transparent change that could be useful for other purposes.