/
withIntl.js
168 lines (137 loc) · 5.03 KB
/
withIntl.js
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import expect, {spyOn} from 'expect';
import React from 'react';
import {mount} from 'enzyme';
import {intlShape} from '../../../src/types';
import IntlProvider from '../../../src/components/provider';
import withIntl from '../../../src/components/withIntl';
const mountWithProvider = el =>
mount(<IntlProvider locale="en">{el}</IntlProvider>);
describe('withIntl()', () => {
let Wrapped;
let rendered;
beforeEach(() => {
Wrapped = () => <div />;
Wrapped.displayName = 'Wrapped';
Wrapped.propTypes = {
intl: intlShape.isRequired,
};
Wrapped.someNonReactStatic = {
foo: true,
};
rendered = null;
});
afterEach(() => {
rendered && rendered.unmount();
});
it('allows introspection access to the wrapped component', () => {
expect(withIntl(Wrapped).WrappedComponent).toBe(Wrapped);
});
it('hoists non-react statics', () => {
expect(withIntl(Wrapped).someNonReactStatic.foo).toBe(true);
});
describe('displayName', () => {
it('is descriptive by default', () => {
expect(withIntl(() => null).displayName).toBe('withIntl(Component)');
});
it("includes `WrappedComponent`'s `displayName`", () => {
Wrapped.displayName = 'Foo';
expect(withIntl(Wrapped).displayName).toBe('withIntl(Foo)');
});
});
it('throws when <IntlProvider> is missing from ancestry', () => {
const Injected = withIntl(Wrapped);
const consoleError = spyOn(console, 'error'); // surpress console error from JSDom
expect(() => (rendered = mount(<Injected />))).toThrow(
'[React Intl] Could not find required `intl` object. <IntlProvider> needs to exist in the component ancestry.'
);
consoleError.restore();
});
it('renders <WrappedComponent> with `intl` prop', () => {
const Injected = withIntl(Wrapped);
rendered = mountWithProvider(<Injected />);
const wrappedComponent = rendered.find(Wrapped);
// React 16 renders different in the wrapper
const intlProvider = rendered.find(IntlProvider).childAt(0);
expect(wrappedComponent.prop('intl')).toBe(
intlProvider.instance().getContext()
);
});
it('propagates all props to <WrappedComponent>', () => {
const Injected = withIntl(Wrapped);
const props = {
foo: 'bar',
};
rendered = mountWithProvider(<Injected {...props} />);
const wrappedComponent = rendered.find(Wrapped);
Object.keys(props).forEach(key => {
expect(wrappedComponent.prop(key)).toBe(props[key]);
});
});
describe('options', () => {
describe('intlPropName', () => {
it("sets <WrappedComponent>'s `props[intlPropName]` to `context.intl`", () => {
const propName = 'myIntl';
Wrapped.propTypes = {
[propName]: intlShape.isRequired,
};
const Injected = withIntl(Wrapped, {
intlPropName: propName,
});
rendered = mountWithProvider(<Injected />);
const wrapped = rendered.find(Wrapped);
const intlProvider = rendered.find(IntlProvider).childAt(0);
expect(wrapped.prop(propName)).toBe(
intlProvider.instance().getContext()
);
});
});
describe('withRef', () => {
it('throws when true', () => {
expect(() => withIntl(Wrapped, {withRef: true})).toThrow(
'[React Intl] withRef and getWrappedInstance() are deprecated, ' +
"instead use the 'forwardRef' option and create a ref directly on the wrapped component."
);
});
});
describe('forwardRef', () => {
it("doesn't forward the ref when forwardRef is `false`", () => {
const Injected = withIntl(Wrapped);
const wrapperRef = React.createRef();
rendered = mountWithProvider(<Injected ref={wrapperRef} />);
const wrapper = rendered.find(Injected);
expect(wrapperRef.current).toBe(wrapper.instance());
});
it('forwards the ref properly to the wrapped component', () => {
Wrapped = class extends React.Component {
render() {
return null;
}
};
const Injected = withIntl(Wrapped, {forwardRef: true});
const wrapperRef = React.createRef();
rendered = mountWithProvider(<Injected ref={wrapperRef} />);
const wrapped = rendered.find(Wrapped);
expect(wrapperRef.current).toBe(wrapped.instance());
});
});
it('can also be used with a bound function', () => {
Wrapped = class extends React.Component {
render() {
return null;
}
};
const propName = 'myPropName';
const customWithIntl = withIntl({
forwardRef: true,
intlPropName: propName,
});
const wrapperRef = React.createRef();
const Injected = customWithIntl(Wrapped);
rendered = mountWithProvider(<Injected ref={wrapperRef} />);
const wrapped = rendered.find(Wrapped);
expect(wrapperRef.current).toBe(wrapped.instance());
const intlProvider = rendered.find(IntlProvider).childAt(0);
expect(wrapped.prop(propName)).toBe(intlProvider.instance().getContext());
});
});
});