Skip to content

Instantly share code, notes, and snippets.

@alanhg
Created October 2, 2019 13:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alanhg/93445ee840866586dac30a67cf4f6822 to your computer and use it in GitHub Desktop.
Save alanhg/93445ee840866586dac30a67cf4f6822 to your computer and use it in GitHub Desktop.
intl-enzyme-test-helper.ts
import React from 'react';
import { IntlProvider, intlShape } from 'react-intl';
import { mount, render, shallow } from 'enzyme';
const messages = {}; // en.json
const intlProvider = new IntlProvider({ locale: 'en', messages, onError: () => '' }, {});
const { intl } = intlProvider.getChildContext();
function nodeWithIntlProp(node) {
return React.cloneElement(node, { intl });
}
export function shallowWithIntl(node) {
return shallow(nodeWithIntlProp(node), { context: { intl } });
}
export function mountWithIntl(node) {
return mount(nodeWithIntlProp(node), {
context: { intl },
childContextTypes: { intl: intlShape }
});
}
export function renderWithIntl(node) {
return render(nodeWithIntlProp(node), {
context: { intl },
childContextTypes: { intl: intlShape }
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment