aboutsummaryrefslogtreecommitdiffstats
path: root/app/utils/helpers/intlTestHelper.js
blob: d00ef5992954d96b9a9d37a781956d586908e0cc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import React from 'react';
import { IntlProvider, intlShape } from 'react-intl';
import { mount, shallow } from 'enzyme'; // eslint-disable-line import/no-extraneous-dependencies
import { translationMessages } from '@/i18n';

// Create the IntlProvider to retrieve context for wrapping around.
export const generateIntlInstance = (locale) => {
  const intlProvider = new IntlProvider({
    locale,
    messages: translationMessages[locale],
  }, {});

  const { intl } = intlProvider.getChildContext();
  return intl;
};

/**
 * When using React-Intl `injectIntl` on components, props.intl is required.
 */
const nodeWithIntlProp = (node, intl) => (
  React.cloneElement(node, { intl })
);

export const shallowWithIntl = (node, { context = {}, locale = 'en' } = {}) => {
  const intl = generateIntlInstance(locale);

  return shallow(
    nodeWithIntlProp(node, intl),
    {
      context: {
        ...context,
        intl,
      },
    }
  );
};

export const mountWithIntl = (node, { context = {}, childContextTypes = {}, locale = 'en' } = {}) => {
  const intl = generateIntlInstance(locale);
  return mount(
    nodeWithIntlProp(node),
    {
      context: {
        ...context,
        intl,
      },
      childContextTypes: {
        intl: intlShape,
        ...childContextTypes,
      },
    }
  );
};