8

I have looked into react-intl for suggestions but it doesn't leave any explicit documentation for enzyme.

This is how I have been trying to write my tests.

import {IntlProvider} from 'react-intl';

const intlProvider = new IntlProvider({locale: 'en'}, {});
const intl = intlProvider.getChildContext();
const customMessage = shallow(<CustomMessage />, { options: { context: intl } });

But I keep getting the error

Invariant Violation: [React Intl] Could not find required intl object. needs to exist in the component ancestry.

I looked into their repo and they seems to have made it work with 'react-addons-test-utils'.

Am I doing something wrong?

Deadfish
  • 2,047
  • 16
  • 17

3 Answers3

4

I've posted an answer to a similar question:

Injecting react-intl object into mounted Enzyme components for testing

You would be able to import { shallowWithIntl } from 'intl-helper' and then use shallowWithIntl() instead of Enzyme's shallow().

Community
  • 1
  • 1
Mirage
  • 992
  • 1
  • 7
  • 26
2

I got it working by using

const customMessage = shallow(<CustomMessage />, { context: intl });

instead.

Martin B
  • 21
  • 1
  • That is a good approach for `shallow` instantiations. Note this won't work when using Enzyme's `mount` method. – Mirage May 23 '16 at 07:07
0

Thats how I achieve the things:


import React from 'react';
import StandardFilterIntl, {StandardFilter} from 'bundles/components/Filter/StandardFilter';
import {mountWithIntl} from 'enzyme-react-intl';

const FilterComponent = mountWithIntl(<StandardFilterIntl {...standardFilterProps} />);
FilterComponent.find(StandardFilter).state()

user1288338
  • 109
  • 1
  • 10