Skip to content

Instantly share code, notes, and snippets.

@cheshirecode
Created July 25, 2019 07:09
Show Gist options
  • Save cheshirecode/a8aad0a39d75c00504a172727952ee7c to your computer and use it in GitHub Desktop.
Save cheshirecode/a8aad0a39d75c00504a172727952ee7c to your computer and use it in GitHub Desktop.
import React from 'react';
import { IntlProvider, intlShape, injectIntl } from 'react-intl';
import { mount, shallow } from 'enzyme';
// You can pass your messages to the IntlProvider. Optional: remove if unneeded.
const messages = { foo: 'bar'} // en.json
const intlProps = { locale: 'en', messages };
const Child = () => null;
const IntlChild = injectIntl(Child);
const intl = mount(
<IntlProvider {...intlProps}>
<IntlChild />
</IntlProvider>,
)
.find(Child)
.prop('intl');
export function shallowWithIntl(
node,
{ context, ...additionalOptions } = {},
withProvider = false,
) {
const comp = React.cloneElement(node, { intl });
return withProvider
? shallow(<IntlProvider {...intlProps}>{comp}</IntlProvider>)
: shallow(comp, {
context: Object.assign({}, context, { intl }),
...additionalOptions,
});
}
export function mountWithIntl(
node,
{ context, childContextTypes, ...additionalOptions } = {},
) {
return mount(
<IntlProvider {...intlProps}>
{React.cloneElement(node, { intl })}
</IntlProvider>,
{
childContextTypes: Object.assign(
{},
{ intl: intlShape },
childContextTypes,
),
...additionalOptions,
},
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment