scratch-www/test/helpers/intl-helpers.jsx

55 lines
1.4 KiB
React
Raw Permalink Normal View History

/*
* Helpers for using enzyme and react-test-renderer with react-intl
*/
import React from 'react';
import renderer from 'react-test-renderer';
2023-01-06 15:55:16 -05:00
import {createIntl, IntlProvider} from 'react-intl';
import {mount, shallow} from 'enzyme';
2023-01-06 15:55:16 -05:00
import intlShape from '../../src/lib/intl-shape';
2023-01-06 15:55:16 -05:00
const shallowWithIntl = (node, {context} = {}) => {
return shallow(
node,
{
context: Object.assign({}, context),
wrappingComponent: IntlProvider,
wrappingComponentProps: {
locale: 'en',
messages: {}
}
}
).dive();
};
2023-01-06 15:55:16 -05:00
const mountWithIntl = (node, {context, childContextTypes} = {}) => {
const intl = createIntl({locale: 'en', messages: {}});
return mount(
node,
{
context: Object.assign({}, context, {intl}),
childContextTypes: Object.assign({}, {intl: intlShape}, childContextTypes),
wrappingComponent: IntlProvider,
wrappingComponentProps: {
locale: 'en',
messages: {}
}
}
);
};
// react-test-renderer component for use with snapshot testing
const componentWithIntl = (children, props = {locale: 'en'}) => renderer.create(
2023-02-22 09:28:58 -05:00
<IntlProvider
textComponent="span"
{...props}
>
{children}
</IntlProvider>
);
export {
componentWithIntl,
shallowWithIntl,
mountWithIntl
};