forked from wix/react-native-navigation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathComponentWrapper.test.tsx
353 lines (319 loc) · 11.1 KB
/
ComponentWrapper.test.tsx
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
import * as React from 'react';
import { View, Text } from 'react-native';
import * as renderer from 'react-test-renderer';
import { ComponentWrapper } from './ComponentWrapper';
import { Store } from './Store';
import { mock, verify, instance } from 'ts-mockito';
import { ComponentEventsObserver } from '../events/ComponentEventsObserver';
describe('ComponentWrapper', () => {
const componentName = 'example.MyComponent';
let store: Store;
let myComponentProps: any;
let mockedComponentEventsObserver: ComponentEventsObserver;
let componentEventsObserver: ComponentEventsObserver;
let uut: ComponentWrapper;
class MyComponent extends React.Component<any, any> {
static options = {
title: 'MyComponentTitle',
};
render() {
myComponentProps = this.props;
if (this.props.renderCount) {
this.props.renderCount();
}
return <Text>{this.props.text || 'Hello, World!'}</Text>;
}
}
class TestParent extends React.Component<any, any> {
private ChildClass: any;
constructor(props: any) {
super(props);
this.ChildClass = props.ChildClass;
this.state = { propsFromState: {} };
}
render() {
const { ChildClass } = this;
return <ChildClass componentId="component1" {...this.state.propsFromState} />;
}
}
beforeEach(() => {
store = new Store();
mockedComponentEventsObserver = mock(ComponentEventsObserver);
componentEventsObserver = instance(mockedComponentEventsObserver);
uut = new ComponentWrapper();
});
it('must have componentId as prop', () => {
const NavigationComponent = uut.wrap(
componentName,
() => MyComponent,
store,
componentEventsObserver
);
const orig = console.error;
console.error = (a: any) => a;
expect(() => {
renderer.create(<NavigationComponent />);
}).toThrowError('Component example.MyComponent does not have a componentId!');
console.error = orig;
});
it('wraps the component', () => {
const NavigationComponent = uut.wrap(
componentName,
() => MyComponent,
store,
componentEventsObserver
);
expect(NavigationComponent).not.toBeInstanceOf(MyComponent);
const tree = renderer.create(<NavigationComponent componentId={'component1'} />);
expect(tree.toJSON()!.children).toEqual(['Hello, World!']);
});
it('injects props from wrapper into original component', () => {
const renderCount = jest.fn();
const NavigationComponent = uut.wrap(
componentName,
() => MyComponent,
store,
componentEventsObserver
);
const tree = renderer.create(
<NavigationComponent componentId={'component1'} text={'yo'} renderCount={renderCount} />
);
expect(tree.toJSON()!.children).toEqual(['yo']);
expect(renderCount).toHaveBeenCalledTimes(1);
});
it('updates props from wrapper into original component on state change', () => {
const NavigationComponent = uut.wrap(
componentName,
() => MyComponent,
store,
componentEventsObserver
);
const tree = renderer.create(<TestParent ChildClass={NavigationComponent} />);
expect(myComponentProps.foo).toEqual(undefined);
(tree.getInstance() as any).setState({ propsFromState: { foo: 'yo' } });
expect(myComponentProps.foo).toEqual('yo');
});
it('pulls props from the store and injects them into the inner component', () => {
store.updateProps('component123', { numberProp: 1, stringProp: 'hello', objectProp: { a: 2 } });
const NavigationComponent = uut.wrap(
componentName,
() => MyComponent,
store,
componentEventsObserver
);
renderer.create(<NavigationComponent componentId={'component123'} />);
expect(myComponentProps).toEqual({
componentId: 'component123',
componentName,
numberProp: 1,
stringProp: 'hello',
objectProp: { a: 2 },
});
});
it('update props with callback', (done) => {
const NavigationComponent = uut.wrap(
componentName,
() => MyComponent,
store,
componentEventsObserver
);
renderer.create(<NavigationComponent componentId={'component123'} />);
function callback() {
try {
expect(true).toBe(true);
done();
} catch (error) {
done(error);
}
}
store.updateProps('component123', { someProp: 'someValue' }, callback);
});
it('updates props from store into inner component', () => {
const NavigationComponent = uut.wrap(
componentName,
() => MyComponent,
store,
componentEventsObserver
);
renderer.create(<TestParent ChildClass={NavigationComponent} />);
expect(myComponentProps.myProp).toEqual(undefined);
store.updateProps('component1', { myProp: 'hello' });
expect(myComponentProps.myProp).toEqual('hello');
});
it('updates props from state into inner component', () => {
const NavigationComponent = uut.wrap(
componentName,
() => MyComponent,
store,
componentEventsObserver
);
const tree = renderer.create(<TestParent ChildClass={NavigationComponent} />);
expect(myComponentProps.foo).toEqual(undefined);
(tree.getInstance() as any).setState({ propsFromState: { foo: 'yo' } });
expect(myComponentProps.foo).toEqual('yo');
});
it('protects id from change', () => {
const NavigationComponent = uut.wrap(
componentName,
() => MyComponent,
store,
componentEventsObserver
);
const tree = renderer.create(<TestParent ChildClass={NavigationComponent} />);
expect(myComponentProps.componentId).toEqual('component1');
(tree.getInstance() as any).setState({ propsFromState: { id: 'ERROR' } });
expect(myComponentProps.componentId).toEqual('component1');
});
xit('assigns key by componentId', () => {
const NavigationComponent = uut.wrap(
componentName,
() => MyComponent,
store,
componentEventsObserver
);
const tree = renderer.create(<NavigationComponent componentId={'component1'} />);
expect(myComponentProps.componentId).toEqual('component1');
console.log(
Object.keys(tree.root.findByType(NavigationComponent).instance._reactInternalFiber)
);
console.log(
tree.root.findByType(NavigationComponent).instance._reactInternalFiber.child.child.child
.return.return.key
);
expect((tree.getInstance() as any)._reactInternalInstance.child.child.Fibernode.key).toEqual(
'component1'
);
});
it('cleans props from store on unMount', () => {
store.updateProps('component123', { foo: 'bar' });
const NavigationComponent = uut.wrap(
componentName,
() => MyComponent,
store,
componentEventsObserver
);
const tree = renderer.create(<NavigationComponent componentId={'component123'} />);
expect(store.getPropsForId('component123')).toEqual({ foo: 'bar' });
tree.unmount();
expect(store.getPropsForId('component123')).toEqual({});
});
it('merges static members from wrapped component when generated', () => {
const NavigationComponent = uut.wrap(
componentName,
() => MyComponent,
store,
componentEventsObserver
) as any;
expect(NavigationComponent.options).toEqual({ title: 'MyComponentTitle' });
});
it('calls unmounted on componentEventsObserver', () => {
const NavigationComponent = uut.wrap(
componentName,
() => MyComponent,
store,
componentEventsObserver
);
const tree = renderer.create(<NavigationComponent componentId={'component123'} />);
verify(mockedComponentEventsObserver.unmounted('component123')).never();
tree.unmount();
verify(mockedComponentEventsObserver.unmounted('component123')).once();
});
it('renders HOC components correctly', () => {
const generator = () => (props: any) => (
<View>
<MyComponent {...props} />
</View>
);
uut = new ComponentWrapper();
const NavigationComponent = uut.wrap(componentName, generator, store, componentEventsObserver);
const tree = renderer.create(<NavigationComponent componentId={'component123'} />);
expect(tree.root.findByType(View)).toBeDefined();
expect(tree.root.findByType(MyComponent).props).toEqual({
componentId: 'component123',
componentName,
});
});
it('sets component instance in store when constructed', () => {
const NavigationComponent = uut.wrap(
componentName,
() => MyComponent,
store,
componentEventsObserver
);
renderer.create(<NavigationComponent componentId={'component1'} />);
expect(store.getComponentInstance('component1')).toBeTruthy();
});
it('Component generator is invoked only once', () => {
const componentGenerator = jest.fn(() => MyComponent);
uut.wrap(componentName, componentGenerator, store, componentEventsObserver);
expect(componentGenerator.mock.calls.length).toBe(1);
});
describe(`register with redux store`, () => {
class MyReduxComp extends React.Component<any> {
static options() {
return { foo: 123 };
}
render() {
return <Text>{this.props.txt}</Text>;
}
}
interface RootState {
txt: string;
}
function mapStateToProps(state: RootState) {
return {
txt: state.txt,
};
}
const ConnectedComp = require('react-redux').connect(mapStateToProps)(MyReduxComp);
const ReduxProvider = require('react-redux').Provider;
const initialState: RootState = { txt: 'it just works' };
const reduxStore = require('redux').createStore((state: RootState = initialState) => state);
it(`wraps the component with a react-redux provider with passed store`, () => {
const NavigationComponent = uut.wrap(
componentName,
() => ConnectedComp,
store,
componentEventsObserver,
undefined,
ReduxProvider,
reduxStore
);
const tree = renderer.create(<NavigationComponent componentId={'theCompId'} />);
expect(tree.toJSON()!.children).toEqual(['it just works']);
expect((NavigationComponent as any).options()).toEqual({ foo: 123 });
});
});
it('initialize isMounted with false value', () => {
const NavigationComponent = uut.wrap(
componentName,
() => MyComponent,
store,
componentEventsObserver
);
new NavigationComponent({ componentId: 'component123' });
expect(store.getComponentInstance('component123')?.isMounted).toEqual(false);
});
it('updates isMounted on componentDidMount', () => {
const NavigationComponent = uut.wrap(
componentName,
() => MyComponent,
store,
componentEventsObserver
);
renderer.create(<NavigationComponent componentId={'component123'} />);
expect(store.getComponentInstance('component123')?.isMounted).toEqual(true);
});
it('clears isMounted on componentDidUnmount', () => {
const NavigationComponent = uut.wrap(
componentName,
() => MyComponent,
store,
componentEventsObserver
);
const tree = renderer.create(<NavigationComponent componentId={'component123'} />);
expect(store.getComponentInstance('component123')?.isMounted).toEqual(true);
tree.unmount();
expect(store.getComponentInstance('component123')?.isMounted).toBeUndefined();
});
});