Created
May 6, 2016 11:25
-
-
Save joncursi/01a01b230f69e698e0bea07f301f9db7 to your computer and use it in GitHub Desktop.
React Intl Error within Enzyme Test
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
1) <NotFoundPage /> renders the correct content: | |
TypeError: (0 , _intl.mountWithIntl) is not a function | |
at Context.<anonymous> (NotFoundPage-test.js:14:21) |
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
/** | |
* Components using the react-intl module require access to the intl context. | |
* This is not available when mounting single components in Enzyme. | |
* These helper functions aim to address that and wrap a valid, | |
* English-locale intl context around them. | |
*/ | |
import React from 'react'; | |
import { IntlProvider, intlShape } from 'react-intl'; | |
import { mount, shallow } from 'enzyme'; | |
const messages = require('../../i18n/en'); | |
const intlProvider = new IntlProvider({ locale: 'en', messages }, {}); | |
const { intl } = intlProvider.getChildContext(); | |
/** | |
* When using React-Intl `injectIntl` on components, props.intl is required. | |
*/ | |
function nodeWithIntlProp(node) { | |
return React.cloneElement(node, { intl }); | |
} | |
export default { | |
shallowWithIntl(node) { | |
return shallow(nodeWithIntlProp(node), { context: { intl } }); | |
}, | |
mountWithIntl(node) { | |
return mount(nodeWithIntlProp(node), { | |
context: { intl }, | |
childContextTypes: { intl: intlShape }, | |
}); | |
}, | |
}; |
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
import { describe, it } from 'mocha'; | |
import expect from 'expect'; | |
import React from 'react'; | |
import { mountWithIntl } from '../helpers/intl-test'; | |
import NotFoundPage from '../../imports/ui/pages/NotFoundPage.jsx'; | |
describe('<NotFoundPage />', () => { | |
// As a user | |
// If I navigate to the wrong page | |
// I will be confused and want to be told how to get there | |
it('renders the correct content', () => { | |
const wrapper = mountWithIntl(<NotFoundPage />); | |
expect(wrapper.text()).toEqual('Page not found'); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Try this package enzyme-react-intl