-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support stories of asynchronously rendered components passing Jest do…
…ne callback to custom test method
- Loading branch information
Showing
11 changed files
with
254 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 28 additions & 10 deletions
38
addons/storyshots/storyshots-core/src/api/snapshotsTestsTemplate.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
addons/storyshots/storyshots-core/stories/required_with_context/Async.stories.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import React from 'react'; | ||
import { storiesOf } from '@storybook/react'; | ||
|
||
export const EXPECTED_VALUE = 'THIS IS SO DONE'; | ||
export const TIMEOUT = 5; | ||
|
||
class AsyncTestComponent extends React.Component { | ||
constructor(props) { | ||
super(props); | ||
this.state = { | ||
value: '', | ||
}; | ||
} | ||
|
||
componentDidMount() { | ||
setTimeout(() => { | ||
this.setState({ | ||
value: EXPECTED_VALUE, | ||
}); | ||
}, TIMEOUT); | ||
} | ||
|
||
render() { | ||
const { value } = this.state; | ||
return <h1>{value}</h1>; | ||
} | ||
} | ||
|
||
storiesOf('Async', module).add(`with ${TIMEOUT}ms timeout simulating async operation`, () => ( | ||
<AsyncTestComponent /> | ||
)); |
7 changes: 7 additions & 0 deletions
7
.../storyshots/storyshots-core/stories/required_with_context/__snapshots__/Async.stories.foo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`Storyshots Async with 5ms timeout simulating async operation 1`] = ` | ||
<h1> | ||
|
||
</h1> | ||
`; |
7 changes: 7 additions & 0 deletions
7
...shots/storyshots-core/stories/required_with_context/__snapshots__/Async.stories.storyshot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`Storyshots Async with 5ms timeout simulating async operation 1`] = ` | ||
<h1> | ||
|
||
</h1> | ||
`; |
44 changes: 44 additions & 0 deletions
44
addons/storyshots/storyshots-core/stories/storyshot.async.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import path from 'path'; | ||
import { mount } from 'enzyme'; | ||
import toJson from 'enzyme-to-json'; | ||
import initStoryshots, { Stories2SnapsConverter } from '../src'; | ||
import { TIMEOUT, EXPECTED_VALUE } from './required_with_context/Async.stories'; | ||
|
||
initStoryshots({ | ||
asyncJest: true, | ||
framework: 'react', | ||
configPath: path.join(__dirname, '..', '.storybook'), | ||
// When async is true we need to provide a test method that | ||
// calls done() when at the end of the test method | ||
test: ({ story, context, done }) => { | ||
expect(done).not.toBeUndefined(); | ||
const converter = new Stories2SnapsConverter(); | ||
const snapshotFilename = converter.getSnapshotFileName(context); | ||
const storyElement = story.render(context); | ||
|
||
// Mount the component | ||
let wrapper = mount(storyElement); | ||
|
||
// This is a storyOf Async (see ./required_with_context/Async.stories) | ||
if (context.kind === 'Async') { | ||
// The Async component should not contain the expected value | ||
expect(wrapper.find('AsyncTestComponent').contains(EXPECTED_VALUE)).toBe(false); | ||
|
||
// wait until the "Async" component is updated | ||
setTimeout(() => { | ||
// Update the wrapper with the changes in the underlying component | ||
wrapper = wrapper.update(); | ||
|
||
// Assert the expected value and the corresponding snapshot | ||
expect(wrapper.find('AsyncTestComponent').contains(EXPECTED_VALUE)).toBe(true); | ||
expect(toJson(wrapper)).toMatchSpecificSnapshot(snapshotFilename); | ||
|
||
// finally mark test as done | ||
done(); | ||
}, TIMEOUT); | ||
} else { | ||
// If not async just mark the test as done | ||
done(); | ||
} | ||
}, | ||
}); |