Created
July 25, 2019 07:09
-
-
Save cheshirecode/a8aad0a39d75c00504a172727952ee7c to your computer and use it in GitHub Desktop.
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 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