Last active
June 16, 2017 09:36
-
-
Save stipsan/dc1d6e2924ac88523898278274493e07 to your computer and use it in GitHub Desktop.
Testing with Jest: Tip #3
This file contains hidden or 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
import renderer from 'react-test-renderer' | |
import Select from '../Select' | |
jest.mock('react-select', () => { | |
const { createElement } = require('react') | |
// Note that since 'react-select' is mocked we have to use | |
// require.requireActual, even inside the factory function that mocks it! | |
// Just watch the movie Inception and skip to the Dream within a Dream and you'll get it | |
const ReactSelect = require.requireActual('react-select') | |
// Since we need proptype validation working we need to wrap the mock in a | |
// stateless function (or Component, whatever React is able to render) | |
const MockedAsync = props => createElement('Async', props) | |
MockedAsync.propTypes = ReactSelect.Async.propTypes | |
MockedAsync.defaultProps = ReactSelect.Async.defaultProps | |
ReactSelect.Async = MockedAsync | |
return ReactSelect | |
}) | |
it('should render correctly', () => { | |
const component = renderer.create(<Select />) | |
expect(component.toJSON()).toMatchSnapshot() | |
}) |
This file contains hidden or 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
import { Async } from 'react-select' | |
// just decorating react-select with default props | |
// to reduce duplication in the codebase | |
export default props => <Async labelKey="title" valueKey="uuid" {...props} /> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment