|
@@ -2,209 +2,130 @@ import { OptionsProcessor } from './OptionsProcessor';
|
2
|
2
|
import { UniqueIdProvider } from '../adapters/UniqueIdProvider';
|
3
|
3
|
import { Store } from '../components/Store';
|
4
|
4
|
import * as _ from 'lodash';
|
|
5
|
+import { Options, OptionsModalPresentationStyle } from '../interfaces/Options';
|
|
6
|
+import { mock, when, anyString, instance, anyNumber, verify } from 'ts-mockito';
|
|
7
|
+import { ColorService } from '../adapters/ColorService';
|
|
8
|
+import { AssetService } from '../adapters/AssetResolver';
|
5
|
9
|
|
6
|
10
|
describe('navigation options', () => {
|
7
|
11
|
let uut: OptionsProcessor;
|
8
|
|
- let options: Record<string, any>;
|
9
|
|
- let store: Store;
|
|
12
|
+ const mockedStore = mock(Store);
|
|
13
|
+ const store = instance(mockedStore);
|
|
14
|
+
|
10
|
15
|
beforeEach(() => {
|
11
|
|
- options = {};
|
12
|
|
- store = new Store();
|
13
|
|
- uut = new OptionsProcessor(store, new UniqueIdProvider());
|
14
|
|
- });
|
|
16
|
+ const mockedAssetService = mock(AssetService);
|
|
17
|
+ when(mockedAssetService.resolveFromRequire(anyNumber())).thenReturn('lol');
|
|
18
|
+ const assetService = instance(mockedAssetService);
|
15
|
19
|
|
16
|
|
- it('processes colors into numeric AARRGGBB', () => {
|
17
|
|
- options.someKeyColor = 'red';
|
18
|
|
- options.color = 'blue';
|
19
|
|
- uut.processOptions(options);
|
20
|
|
- expect(options.someKeyColor).toEqual(0xffff0000);
|
21
|
|
- expect(options.color).toEqual(0xff0000ff);
|
|
20
|
+ const mockedColorService = mock(ColorService);
|
|
21
|
+ when(mockedColorService.toNativeColor(anyString())).thenReturn(666);
|
|
22
|
+ const colorService = instance(mockedColorService);
|
22
|
23
|
|
23
|
|
- options.someKeyColor = 'yellow';
|
24
|
|
- uut.processOptions(options);
|
25
|
|
- expect(options.someKeyColor).toEqual(0xffffff00);
|
|
24
|
+ uut = new OptionsProcessor(store, new UniqueIdProvider(), colorService, assetService);
|
26
|
25
|
});
|
27
|
26
|
|
28
|
|
- it('processes numeric colors', () => {
|
29
|
|
- options.someKeyColor = '#123456';
|
|
27
|
+ it('keeps original values if values were not processed', () => {
|
|
28
|
+ const options: Options = {
|
|
29
|
+ blurOnUnmount: false,
|
|
30
|
+ popGesture: false,
|
|
31
|
+ modalPresentationStyle: OptionsModalPresentationStyle.fullScreen,
|
|
32
|
+ animations: { dismissModal: { alpha: { from: 0, to: 1 } } },
|
|
33
|
+ };
|
30
|
34
|
uut.processOptions(options);
|
31
|
|
- expect(options.someKeyColor).toEqual(0xff123456);
|
32
|
|
-
|
33
|
|
- options.someKeyColor = 0x123456ff; // wut
|
|
35
|
+ expect(options).toEqual({
|
|
36
|
+ blurOnUnmount: false,
|
|
37
|
+ popGesture: false,
|
|
38
|
+ modalPresentationStyle: OptionsModalPresentationStyle.fullScreen,
|
|
39
|
+ animations: { dismissModal: { alpha: { from: 0, to: 1 } } },
|
|
40
|
+ });
|
|
41
|
+ });
|
|
42
|
+
|
|
43
|
+ it('processes color keys', () => {
|
|
44
|
+ const options: Options = {
|
|
45
|
+ statusBar: { backgroundColor: 'red' },
|
|
46
|
+ topBar: { background: { color: 'blue' } },
|
|
47
|
+ };
|
34
|
48
|
uut.processOptions(options);
|
35
|
|
- expect(options.someKeyColor).toEqual(0xff123456);
|
|
49
|
+ expect(options).toEqual({
|
|
50
|
+ statusBar: { backgroundColor: 666 },
|
|
51
|
+ topBar: { background: { color: 666 } },
|
|
52
|
+ });
|
36
|
53
|
});
|
37
|
54
|
|
38
|
|
- it('process colors with rgb functions', () => {
|
39
|
|
- options.someKeyColor = 'rgb(255, 0, 255)';
|
|
55
|
+ it('processes image keys', () => {
|
|
56
|
+ const options: Options = {
|
|
57
|
+ backgroundImage: 123,
|
|
58
|
+ rootBackgroundImage: 234,
|
|
59
|
+ bottomTab: { icon: 345, selectedIcon: 345 },
|
|
60
|
+ };
|
40
|
61
|
uut.processOptions(options);
|
41
|
|
- expect(options.someKeyColor).toEqual(0xffff00ff);
|
|
62
|
+ expect(options).toEqual({
|
|
63
|
+ backgroundImage: 'lol',
|
|
64
|
+ rootBackgroundImage: 'lol',
|
|
65
|
+ bottomTab: { icon: 'lol', selectedIcon: 'lol' },
|
|
66
|
+ });
|
42
|
67
|
});
|
43
|
68
|
|
44
|
|
- it('process colors with special words', () => {
|
45
|
|
- options.someKeyColor = 'fuchsia';
|
46
|
|
- uut.processOptions(options);
|
47
|
|
- expect(options.someKeyColor).toEqual(0xffff00ff);
|
48
|
|
- });
|
|
69
|
+ it('calls store if component has passProps', () => {
|
|
70
|
+ const passProps = { some: 'thing' };
|
|
71
|
+ const options = { topBar: { title: { component: { passProps, name: 'a' } } } };
|
49
|
72
|
|
50
|
|
- it('process colors with hsla functions', () => {
|
51
|
|
- options.someKeyColor = 'hsla(360, 100%, 100%, 1.0)';
|
52
|
73
|
uut.processOptions(options);
|
53
|
74
|
|
54
|
|
- expect(options.someKeyColor).toEqual(0xffffffff);
|
|
75
|
+ verify(mockedStore.setPropsForId('CustomComponent1', passProps)).called();
|
55
|
76
|
});
|
56
|
77
|
|
57
|
|
- it('unknown colors return undefined', () => {
|
58
|
|
- options.someKeyColor = 'wut';
|
59
|
|
- uut.processOptions(options);
|
60
|
|
- expect(options.someKeyColor).toEqual(undefined);
|
61
|
|
- });
|
|
78
|
+ it('generates componentId for component id was not passed', () => {
|
|
79
|
+ const options = { topBar: { title: { component: { name: 'a' } } } };
|
62
|
80
|
|
63
|
|
- it('any keys ending with Color', () => {
|
64
|
|
- options.otherKeyColor = 'red';
|
65
|
|
- options.yetAnotherColor = 'blue';
|
66
|
|
- options.andAnotherColor = 'rgb(0, 255, 0)';
|
67
|
81
|
uut.processOptions(options);
|
68
|
|
- expect(options.otherKeyColor).toEqual(0xffff0000);
|
69
|
|
- expect(options.yetAnotherColor).toEqual(0xff0000ff);
|
70
|
|
- expect(options.andAnotherColor).toEqual(0xff00ff00);
|
71
|
|
- });
|
72
|
82
|
|
73
|
|
- it('keys ending with Color case sensitive', () => {
|
74
|
|
- options.otherKey_color = 'red'; // eslint-disable-line camelcase
|
75
|
|
- uut.processOptions(options);
|
76
|
|
- expect(options.otherKey_color).toEqual('red');
|
|
83
|
+ expect(options).toEqual({
|
|
84
|
+ topBar: { title: { component: { name: 'a', componentId: 'CustomComponent1' } } },
|
|
85
|
+ });
|
77
|
86
|
});
|
78
|
87
|
|
79
|
|
- it('any nested recursive keys ending with Color', () => {
|
80
|
|
- options.topBar = { textColor: 'red' };
|
81
|
|
- options.topBar.innerMostObj = { anotherColor: 'yellow' };
|
82
|
|
- uut.processOptions(options);
|
83
|
|
- expect(options.topBar.textColor).toEqual(0xffff0000);
|
84
|
|
- expect(options.topBar.innerMostObj.anotherColor).toEqual(0xffffff00);
|
85
|
|
- });
|
|
88
|
+ it('copies passed id to componentId key', () => {
|
|
89
|
+ const options = { topBar: { title: { component: { name: 'a', id: 'Component1' } } } };
|
86
|
90
|
|
87
|
|
- it('resolve image sources with name/ending with icon', () => {
|
88
|
|
- options.icon = 'require("https://wix.github.io/react-native-navigation/_images/logo.png");';
|
89
|
|
- options.image = 'require("https://wix.github.io/react-native-navigation/_images/logo.png");';
|
90
|
|
- options.myImage = 'require("https://wix.github.io/react-native-navigation/_images/logo.png");';
|
91
|
|
- options.topBar = {
|
92
|
|
- myIcon: 'require("https://wix.github.io/react-native-navigation/_images/logo.png");',
|
93
|
|
- myOtherValue: 'value'
|
94
|
|
- };
|
95
|
91
|
uut.processOptions(options);
|
96
|
92
|
|
97
|
|
- // As we can't import external images and we don't want to add an image here
|
98
|
|
- // I assign the icons to strings (what the require would generally look like)
|
99
|
|
- // and expect the value to be resolved, in this case it doesn't find anything and returns null
|
100
|
|
- expect(options.icon).toEqual(null);
|
101
|
|
- expect(options.topBar.myIcon).toEqual(null);
|
102
|
|
- expect(options.image).toEqual(null);
|
103
|
|
- expect(options.myImage).toEqual(null);
|
104
|
|
- expect(options.topBar.myOtherValue).toEqual('value');
|
|
93
|
+ expect(options).toEqual({
|
|
94
|
+ topBar: { title: { component: { name: 'a', id: 'Component1', componentId: 'Component1' } } },
|
|
95
|
+ });
|
105
|
96
|
});
|
106
|
97
|
|
107
|
|
- it('passProps for Buttons options', () => {
|
|
98
|
+ it('calls store when button has passProps and id', () => {
|
108
|
99
|
const passProps = { prop: 'prop' };
|
109
|
|
- options.rightButtons = [{ passProps, id: '1' }];
|
110
|
|
-
|
111
|
|
- uut.processOptions({ o: options });
|
112
|
|
-
|
113
|
|
- expect(store.getPropsForId('1')).toEqual(passProps);
|
114
|
|
- });
|
115
|
|
-
|
116
|
|
- it('passProps for custom component', () => {
|
117
|
|
- const passProps = { color: '#ff0000', some: 'thing' };
|
118
|
|
- options.component = { passProps, name: 'a' };
|
119
|
|
-
|
120
|
|
- uut.processOptions({ o: options });
|
121
|
|
-
|
122
|
|
- expect(store.getPropsForId(options.component.componentId)).toEqual(passProps);
|
123
|
|
- expect(Object.keys(options.component)).not.toContain('passProps');
|
124
|
|
- });
|
125
|
|
-
|
126
|
|
- it('generate component id for component in options', () => {
|
127
|
|
- options.component = { name: 'a' };
|
128
|
|
-
|
129
|
|
- uut.processOptions({ o: options });
|
130
|
|
-
|
131
|
|
- expect(options.component.componentId).toBeDefined();
|
132
|
|
- });
|
133
|
|
-
|
134
|
|
- it('passProps from options are not processed', () => {
|
135
|
|
- const passProps = { color: '#ff0000', some: 'thing' };
|
136
|
|
- const clonedProps = _.cloneDeep(passProps);
|
137
|
|
- options.component = { passProps, name: 'a' };
|
|
100
|
+ const options = { topBar: { rightButtons: [{ passProps, id: '1' }] } };
|
138
|
101
|
|
139
|
102
|
uut.processOptions(options);
|
140
|
|
- expect(store.getPropsForId(options.component.componentId)).toEqual(clonedProps);
|
141
|
|
- });
|
142
|
103
|
|
143
|
|
- it('pass supplied componentId for component in options', () => {
|
144
|
|
- options.component = { name: 'a', id: 'Component1' };
|
145
|
|
-
|
146
|
|
- uut.processOptions({ o: options });
|
147
|
|
-
|
148
|
|
- expect(options.component.componentId).toEqual('Component1');
|
|
104
|
+ verify(mockedStore.setPropsForId('1', passProps)).called();
|
149
|
105
|
});
|
150
|
106
|
|
151
|
|
- it('passProps must be with id next to it', () => {
|
|
107
|
+ it('do not touch passProps when id for button is missing', () => {
|
152
|
108
|
const passProps = { prop: 'prop' };
|
153
|
|
- options.rightButtons = [{ passProps }];
|
154
|
|
-
|
155
|
|
- uut.processOptions({ o: options });
|
156
|
|
-
|
157
|
|
- expect(store.getPropsForId('1')).toEqual({});
|
158
|
|
- });
|
|
109
|
+ const options = { topBar: { rightButtons: [{ passProps } as any] } };
|
159
|
110
|
|
160
|
|
- it('processes Options object', () => {
|
161
|
|
- options.someKeyColor = 'rgb(255, 0, 255)';
|
162
|
|
- options.topBar = { textColor: 'red' };
|
163
|
|
- options.topBar.innerMostObj = { anotherColor: 'yellow' };
|
164
|
|
-
|
165
|
|
- uut.processOptions({ o: options });
|
166
|
|
-
|
167
|
|
- expect(options.topBar.textColor).toEqual(0xffff0000);
|
168
|
|
- });
|
169
|
|
-
|
170
|
|
- it('undefined value return undefined ', () => {
|
171
|
|
- options.someImage = undefined;
|
172
|
111
|
uut.processOptions(options);
|
173
|
112
|
|
174
|
|
- expect(options.someImage).toEqual(undefined);
|
|
113
|
+ expect(options).toEqual({ topBar: { rightButtons: [{ passProps }] } });
|
175
|
114
|
});
|
176
|
115
|
|
177
|
|
- it('omits passProps when processing options', () => {
|
178
|
|
- const passProps = {
|
|
116
|
+ it('omits passProps when processing buttons or components', () => {
|
|
117
|
+ const options = {
|
179
|
118
|
topBar: {
|
180
|
|
- rightButtons: [
|
181
|
|
- {
|
182
|
|
- passProps: {},
|
183
|
|
- id: 'btn1'
|
184
|
|
- },
|
185
|
|
- ],
|
186
|
|
- leftButtons: [
|
187
|
|
- {
|
188
|
|
- passProps: {},
|
189
|
|
- id: 'btn2'
|
190
|
|
- }
|
191
|
|
- ],
|
192
|
|
- title: {
|
193
|
|
- component: {
|
194
|
|
- passProps: {}
|
195
|
|
- }
|
196
|
|
- },
|
197
|
|
- background: {
|
198
|
|
- component: {
|
199
|
|
- passProps: {}
|
200
|
|
- }
|
201
|
|
- }
|
202
|
|
- }
|
|
119
|
+ rightButtons: [{ passProps: {}, id: 'btn1' }],
|
|
120
|
+ leftButtons: [{ passProps: {}, id: 'btn2' }],
|
|
121
|
+ title: { component: { name: 'helloThere1', passProps: {} } },
|
|
122
|
+ background: { component: { name: 'helloThere2', passProps: {} } },
|
|
123
|
+ },
|
203
|
124
|
};
|
204
|
|
- uut.processOptions(passProps);
|
205
|
|
- expect(passProps.topBar.rightButtons[0].passProps).toBeUndefined();
|
206
|
|
- expect(passProps.topBar.leftButtons[0].passProps).toBeUndefined();
|
207
|
|
- expect(passProps.topBar.title.component.passProps).toBeUndefined();
|
208
|
|
- expect(passProps.topBar.background.component.passProps).toBeUndefined();
|
|
125
|
+ uut.processOptions(options);
|
|
126
|
+ expect(options.topBar.rightButtons[0].passProps).toBeUndefined();
|
|
127
|
+ expect(options.topBar.leftButtons[0].passProps).toBeUndefined();
|
|
128
|
+ expect(options.topBar.title.component.passProps).toBeUndefined();
|
|
129
|
+ expect(options.topBar.background.component.passProps).toBeUndefined();
|
209
|
130
|
});
|
210
|
131
|
});
|