Add test helper to work with IntlProvider
Seto Elkahfi committed
Jan 31, 2020 at 19:34 UTC
192ff75587087f9e128d30b50e4f752222b20014
1 file changed
+37
src/helpers/intl-enzyme-test-helper.ts
new
+37
@@ -0,0 +1,37 @@
1
+/**
2
+ * Components using the react-intl module require access to the intl context.
3
+ * This is not available when mounting single components in Enzyme.
4
+ * These helper functions aim to address that and wrap a valid,
5
+ * English-locale intl context around them.
6
+ */
7
+
8
+ import React from 'react'
9
+ import { IntlProvider } from 'react-intl'
10
+ import { mount, shallow } from 'enzyme'
11
+
12
+ // You can pass your messages to the IntlProvider. Optional: remove if unneeded.
13
+ import messages_en from "./../components/translations/en.json";
14
+const defaultLocale = 'en'
15
+const locale = defaultLocale
16
+
17
+export function mountWithIntl(node: React.ReactElement) {
18
+ return mount(node, {
19
+ wrappingComponent: IntlProvider,
20
+ wrappingComponentProps: {
21
+ locale,
22
+ defaultLocale,
23
+ messages_en
24
+ }
25
+ })
26
+}
27
+
28
+export function shallowWithIntl(node: React.ReactElement) {
29
+ return shallow(node, {
30
+ wrappingComponent: IntlProvider,
31
+ wrappingComponentProps: {
32
+ locale,
33
+ defaultLocale,
34
+ messages_en
35
+ }
36
+ })
37
+}
\ No newline at end of file