intl-helpers.jsx 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. /*
  2. * Helpers for using enzyme and react-test-renderer with react-intl
  3. * Directly from https://github.com/yahoo/react-intl/wiki/Testing-with-React-Intl
  4. */
  5. import React from 'react';
  6. import renderer from 'react-test-renderer';
  7. import {IntlProvider, intlShape} from 'react-intl';
  8. import {mount, shallow} from 'enzyme';
  9. const intlProvider = new IntlProvider({locale: 'en'}, {});
  10. const {intl} = intlProvider.getChildContext();
  11. const nodeWithIntlProp = node => React.cloneElement(node, {intl});
  12. const shallowWithIntl = (node, {context} = {}) => shallow(
  13. nodeWithIntlProp(node),
  14. {
  15. context: Object.assign({}, context, {intl})
  16. }
  17. );
  18. const mountWithIntl = (node, {context, childContextTypes} = {}) => mount(
  19. nodeWithIntlProp(node),
  20. {
  21. context: Object.assign({}, context, {intl}),
  22. childContextTypes: Object.assign({}, {intl: intlShape}, childContextTypes)
  23. }
  24. );
  25. // react-test-renderer component for use with snapshot testing
  26. const componentWithIntl = (children, props = {locale: 'en'}) => renderer.create(
  27. <IntlProvider {...props}>{children}</IntlProvider>
  28. );
  29. export {
  30. componentWithIntl,
  31. shallowWithIntl,
  32. mountWithIntl
  33. };