|
@@ -1,130 +0,0 @@
|
1
|
|
-import {AppRegistry, Text} from 'react-native';
|
2
|
|
-import React, {Component} from 'react';
|
3
|
|
-import renderer from 'react-test-renderer';
|
4
|
|
-
|
5
|
|
-describe('ComponentRegistry', () => {
|
6
|
|
- let uut;
|
7
|
|
- let myContainerRef;
|
8
|
|
- let testParentRef;
|
9
|
|
- let containerStore;
|
10
|
|
-
|
11
|
|
- class MyContainer extends Component {
|
12
|
|
- constructor(props) {
|
13
|
|
- super(props);
|
14
|
|
- myContainerRef = this; //eslint-disable-line
|
15
|
|
- }
|
16
|
|
-
|
17
|
|
- render() {
|
18
|
|
- return <Text>{'Hello, World!'}</Text>;
|
19
|
|
- }
|
20
|
|
- }
|
21
|
|
-
|
22
|
|
- class TestParent extends Component { //eslint-disable-line
|
23
|
|
- constructor(props) {
|
24
|
|
- super(props);
|
25
|
|
- testParentRef = this; //eslint-disable-line
|
26
|
|
- this.ChildClass = props.ChildClass;
|
27
|
|
- this.state = {propsFromState: {}};
|
28
|
|
- }
|
29
|
|
-
|
30
|
|
- render() {
|
31
|
|
- const Child = this.ChildClass;
|
32
|
|
- return (
|
33
|
|
- <Child screenId="screen1" {...this.state.propsFromState}/>
|
34
|
|
- );
|
35
|
|
- }
|
36
|
|
- }
|
37
|
|
-
|
38
|
|
- beforeEach(() => {
|
39
|
|
- uut = require('./ContainerRegistry');
|
40
|
|
- containerStore = require('./ContainerStore');
|
41
|
|
- });
|
42
|
|
-
|
43
|
|
- afterEach(() => {
|
44
|
|
- myContainerRef = null;
|
45
|
|
- testParentRef = null;
|
46
|
|
- });
|
47
|
|
-
|
48
|
|
- describe('registerContainer', () => {
|
49
|
|
- beforeEach(() => {
|
50
|
|
- AppRegistry.registerComponent = jest.fn(AppRegistry.registerComponent);
|
51
|
|
- });
|
52
|
|
-
|
53
|
|
- it('registers container component by containerKey into AppRegistry', () => {
|
54
|
|
- expect(AppRegistry.registerComponent).not.toHaveBeenCalled();
|
55
|
|
- uut.registerContainer('example.MyContainer.key', () => MyContainer);
|
56
|
|
- expect(AppRegistry.registerComponent).toHaveBeenCalledTimes(1);
|
57
|
|
- expect(AppRegistry.registerComponent.mock.calls[0][0]).toEqual('example.MyContainer.key');
|
58
|
|
- });
|
59
|
|
-
|
60
|
|
- it('resulting in a normal component', () => {
|
61
|
|
- uut.registerContainer('example.MyContainer.key', () => MyContainer);
|
62
|
|
- const Container = AppRegistry.registerComponent.mock.calls[0][1]();
|
63
|
|
- const tree = renderer.create(<Container screenId="123"/>);
|
64
|
|
- expect(tree.toJSON().children).toEqual(['Hello, World!']);
|
65
|
|
- });
|
66
|
|
- });
|
67
|
|
-
|
68
|
|
- describe('NavigationContainer wrapping', () => {
|
69
|
|
- const containerKey = 'example.MyContainer';
|
70
|
|
-
|
71
|
|
- beforeEach(() => {
|
72
|
|
- uut.registerContainer(containerKey, () => MyContainer);
|
73
|
|
- });
|
74
|
|
-
|
75
|
|
- it('must have screenId as prop', () => {
|
76
|
|
- const NavigationContainer = containerStore.getContainerClass(containerKey);
|
77
|
|
- expect(() => {
|
78
|
|
- renderer.create(<NavigationContainer/>);
|
79
|
|
- }).toThrow(new Error('Screen example.MyContainer does not have a screenId!'));
|
80
|
|
- });
|
81
|
|
-
|
82
|
|
- it('wraps the container and saves to store', () => {
|
83
|
|
- const NavigationContainer = containerStore.getContainerClass(containerKey);
|
84
|
|
- expect(NavigationContainer).not.toBeInstanceOf(MyContainer);
|
85
|
|
- const tree = renderer.create(<NavigationContainer screenId={'screen1'}/>);
|
86
|
|
- expect(tree.toJSON().children).toEqual(['Hello, World!']);
|
87
|
|
- expect(myContainerRef).toBeInstanceOf(MyContainer);
|
88
|
|
- });
|
89
|
|
-
|
90
|
|
- it('injects props from wrapper into original container', () => {
|
91
|
|
- const NavigationContainer = containerStore.getContainerClass(containerKey);
|
92
|
|
- renderer.create(<NavigationContainer screenId={'screen1'} myProp={'yo'}/>);
|
93
|
|
- expect(myContainerRef.props.myProp).toEqual('yo');
|
94
|
|
- });
|
95
|
|
-
|
96
|
|
- it('updates props from wrapper into original container', () => {
|
97
|
|
- const NavigationContainer = containerStore.getContainerClass(containerKey);
|
98
|
|
- renderer.create(<TestParent ChildClass={NavigationContainer}/>);
|
99
|
|
- expect(myContainerRef.props.foo).toEqual(undefined);
|
100
|
|
- testParentRef.setState({propsFromState: {foo: 'yo'}});
|
101
|
|
- expect(myContainerRef.props.foo).toEqual('yo');
|
102
|
|
- });
|
103
|
|
-
|
104
|
|
- it('pulls props from the PropsStore and injects them into the inner container', () => {
|
105
|
|
- require('./PropsStore').setPropsForScreenId('screen123', {numberProp: 1, stringProp: 'hello', objectProp: {a: 2}});
|
106
|
|
- const NavigationContainer = containerStore.getContainerClass(containerKey);
|
107
|
|
- renderer.create(<NavigationContainer screenId={'screen123'}/>);
|
108
|
|
- expect(myContainerRef.props).toEqual({screenId: 'screen123', numberProp: 1, stringProp: 'hello', objectProp: {a: 2}});
|
109
|
|
- });
|
110
|
|
-
|
111
|
|
- it('updates props from PropsStore into inner container', () => {
|
112
|
|
- const NavigationContainer = containerStore.getContainerClass(containerKey);
|
113
|
|
- renderer.create(<TestParent ChildClass={NavigationContainer}/>);
|
114
|
|
- require('./PropsStore').setPropsForScreenId('screen1', {myProp: 'hello'});
|
115
|
|
- expect(myContainerRef.props.foo).toEqual(undefined);
|
116
|
|
- expect(myContainerRef.props.myProp).toEqual(undefined);
|
117
|
|
- testParentRef.setState({propsFromState: {foo: 'yo'}});
|
118
|
|
- expect(myContainerRef.props.foo).toEqual('yo');
|
119
|
|
- expect(myContainerRef.props.myProp).toEqual('hello');
|
120
|
|
- });
|
121
|
|
-
|
122
|
|
- it('protects screenId from change', () => {
|
123
|
|
- const NavigationContainer = containerStore.getContainerClass(containerKey);
|
124
|
|
- renderer.create(<TestParent ChildClass={NavigationContainer}/>);
|
125
|
|
- expect(myContainerRef.props.screenId).toEqual('screen1');
|
126
|
|
- testParentRef.setState({propsFromState: {screenId: 'ERROR'}});
|
127
|
|
- expect(myContainerRef.props.screenId).toEqual('screen1');
|
128
|
|
- });
|
129
|
|
- });
|
130
|
|
-});
|