Skip to content

Instantly share code, notes, and snippets.

@joncursi
Created May 6, 2016 11:25
Show Gist options
  • Save joncursi/01a01b230f69e698e0bea07f301f9db7 to your computer and use it in GitHub Desktop.
Save joncursi/01a01b230f69e698e0bea07f301f9db7 to your computer and use it in GitHub Desktop.
React Intl Error within Enzyme Test
1) <NotFoundPage /> renders the correct content:
TypeError: (0 , _intl.mountWithIntl) is not a function
at Context.<anonymous> (NotFoundPage-test.js:14:21)
/**
* 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 },
});
},
};
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');
});
});
@joetidee
Copy link

joetidee commented Apr 3, 2017

Try this package enzyme-react-intl

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment