@applicaster/zapp-react-native-utils 13.0.0-rc.43 → 13.0.0-rc.45
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +0 -10
- package/equalUtils/__tests__/isEqual/combination.test.ts +263 -0
- package/equalUtils/index.ts +41 -29
- package/focusManager/index.ts +41 -4
- package/package.json +2 -3
- package/reactHooks/navigation/__mocks__/useNavigation.ts +3 -0
- package/reactUtils/__tests__/__snapshots__/reactUtilsHelpers.test.js.snap +16 -4
- package/reactUtils/__tests__/reactUtils.test.js +1 -19
- package/reactUtils/__tests__/reactUtilsHelpers.test.js +72 -71
- package/reactUtils/createContext/__tests__/__snapshots__/helpers.test.js.snap +6 -72
- package/reactUtils/createContext/__tests__/__snapshots__/provider.test.js.snap +3 -28
- package/reactUtils/createContext/__tests__/__snapshots__/withConsumer.test.js.snap +3 -4
- package/reactUtils/createContext/__tests__/__snapshots__/withProvider.test.js.snap +7 -3
- package/reactUtils/createContext/__tests__/helpers.test.js +107 -121
- package/reactUtils/createContext/__tests__/provider.test.js +4 -6
- package/reactUtils/createContext/__tests__/withConsumer.test.js +12 -12
- package/reactUtils/createContext/__tests__/withProvider.test.js +4 -4
- package/reactUtils/index.ts +0 -11
- package/testUtils/index.tsx +0 -10
package/README.md
CHANGED
|
@@ -376,16 +376,6 @@ useEffect(() => {
|
|
|
376
376
|
}, []);
|
|
377
377
|
```
|
|
378
378
|
|
|
379
|
-
### testUtils
|
|
380
|
-
|
|
381
|
-
`@applicaster/zapp-react-native-utils/testUtils`
|
|
382
|
-
|
|
383
|
-
- `testIDEquals: (testId: any) => boolean | { width: number, height: number }` - Function to be used in tandem with enzyme findWhere
|
|
384
|
-
|
|
385
|
-
```javascript
|
|
386
|
-
wrapper.findWhere(testIDEquals("text"));
|
|
387
|
-
```
|
|
388
|
-
|
|
389
379
|
### appUtils - contextKeysManager
|
|
390
380
|
|
|
391
381
|
`@applicaster/zapp-react-native-utils/appUtils/contextKeyManager`
|
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
import { isEqual } from "../..";
|
|
2
|
+
|
|
3
|
+
const array = [1, 2, 3];
|
|
4
|
+
const sameProp = "sameProp";
|
|
5
|
+
|
|
6
|
+
describe("isEqual - comprehensive tests", () => {
|
|
7
|
+
describe("all conditions together", () => {
|
|
8
|
+
it("all conditions checked and not equal", () => {
|
|
9
|
+
const prev = {
|
|
10
|
+
byValueProp: { a: 1, b: 2 },
|
|
11
|
+
byRefProp: array,
|
|
12
|
+
byComparatorProp: "test",
|
|
13
|
+
extraProp1: 123,
|
|
14
|
+
extraProp2: sameProp,
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
const next = {
|
|
18
|
+
byValueProp: { a: 1, b: 2 },
|
|
19
|
+
byRefProp: array,
|
|
20
|
+
byComparatorProp: "different",
|
|
21
|
+
extraProp1: 456,
|
|
22
|
+
extraProp2: sameProp,
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
const customComparator = jest.fn().mockReturnValue(true);
|
|
26
|
+
|
|
27
|
+
const result = isEqual({
|
|
28
|
+
byValue: ["byValueProp"],
|
|
29
|
+
byReference: ["byRefProp"],
|
|
30
|
+
byComparator: {
|
|
31
|
+
comparator: customComparator,
|
|
32
|
+
keys: ["byComparatorProp"],
|
|
33
|
+
},
|
|
34
|
+
compareRemainingKeysBySameValue: true,
|
|
35
|
+
})(prev, next);
|
|
36
|
+
|
|
37
|
+
expect(result).toBe(false);
|
|
38
|
+
|
|
39
|
+
expect(customComparator).toHaveBeenCalledWith(
|
|
40
|
+
prev.byComparatorProp,
|
|
41
|
+
next.byComparatorProp
|
|
42
|
+
);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it("all conditions pass with exact remaining keys check", () => {
|
|
46
|
+
const prev = {
|
|
47
|
+
byValueProp: { a: 1, b: 2 },
|
|
48
|
+
byRefProp: array,
|
|
49
|
+
byComparatorProp: "test",
|
|
50
|
+
extraProp: sameProp,
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
const next = {
|
|
54
|
+
byValueProp: { a: 1, b: 2 },
|
|
55
|
+
byRefProp: array,
|
|
56
|
+
byComparatorProp: "test",
|
|
57
|
+
extraProp: sameProp,
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
const customComparator = jest.fn().mockReturnValue(true);
|
|
61
|
+
|
|
62
|
+
const result = isEqual({
|
|
63
|
+
byValue: ["byValueProp"],
|
|
64
|
+
byReference: ["byRefProp"],
|
|
65
|
+
byComparator: {
|
|
66
|
+
comparator: customComparator,
|
|
67
|
+
keys: ["byComparatorProp"],
|
|
68
|
+
},
|
|
69
|
+
compareRemainingKeysBySameValue: true,
|
|
70
|
+
})(prev, next);
|
|
71
|
+
|
|
72
|
+
expect(result).toBe(true);
|
|
73
|
+
|
|
74
|
+
expect(customComparator).toHaveBeenCalledWith(
|
|
75
|
+
prev.byComparatorProp,
|
|
76
|
+
next.byComparatorProp
|
|
77
|
+
);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it("byReference check fails first (early return)", () => {
|
|
81
|
+
const prev = {
|
|
82
|
+
byValueProp: { a: 1, b: 2 },
|
|
83
|
+
byRefProp: array,
|
|
84
|
+
byComparatorProp: sameProp,
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
const next = {
|
|
88
|
+
byValueProp: { a: 1, b: 2 },
|
|
89
|
+
byRefProp: [1, 2, 3],
|
|
90
|
+
byComparatorProp: sameProp,
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
const customComparator = jest.fn().mockReturnValue(true);
|
|
94
|
+
|
|
95
|
+
const result = isEqual({
|
|
96
|
+
byValue: ["byValueProp"],
|
|
97
|
+
byReference: ["byRefProp"],
|
|
98
|
+
byComparator: {
|
|
99
|
+
comparator: customComparator,
|
|
100
|
+
keys: ["byComparatorProp"],
|
|
101
|
+
},
|
|
102
|
+
})(prev, next);
|
|
103
|
+
|
|
104
|
+
expect(result).toBe(false);
|
|
105
|
+
expect(customComparator).not.toHaveBeenCalled();
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it("byValue check fails after byReference passes", () => {
|
|
109
|
+
const prev = {
|
|
110
|
+
byValueProp: { a: 1, b: 2 },
|
|
111
|
+
byRefProp: array,
|
|
112
|
+
byComparatorProp: sameProp,
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
const next = {
|
|
116
|
+
byValueProp: { a: 1, b: 3 },
|
|
117
|
+
byRefProp: array,
|
|
118
|
+
byComparatorProp: sameProp,
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
const customComparator = jest.fn().mockReturnValue(true);
|
|
122
|
+
|
|
123
|
+
const result = isEqual({
|
|
124
|
+
byValue: ["byValueProp"],
|
|
125
|
+
byReference: ["byRefProp"],
|
|
126
|
+
byComparator: {
|
|
127
|
+
comparator: customComparator,
|
|
128
|
+
keys: ["byComparatorProp"],
|
|
129
|
+
},
|
|
130
|
+
})(prev, next);
|
|
131
|
+
|
|
132
|
+
expect(result).toBe(false);
|
|
133
|
+
expect(customComparator).not.toHaveBeenCalled();
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
it("byComparator check fails after other checks pass", () => {
|
|
137
|
+
const prev = {
|
|
138
|
+
byValueProp: { a: 1, b: 2 },
|
|
139
|
+
byRefProp: array,
|
|
140
|
+
byComparatorProp: "test",
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
const next = {
|
|
144
|
+
byValueProp: { a: 1, b: 2 },
|
|
145
|
+
byRefProp: array,
|
|
146
|
+
byComparatorProp: "different",
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
const customComparator = jest.fn().mockReturnValue(false);
|
|
150
|
+
|
|
151
|
+
const result = isEqual({
|
|
152
|
+
byValue: ["byValueProp"],
|
|
153
|
+
byReference: ["byRefProp"],
|
|
154
|
+
byComparator: {
|
|
155
|
+
comparator: customComparator,
|
|
156
|
+
keys: ["byComparatorProp"],
|
|
157
|
+
},
|
|
158
|
+
})(prev, next);
|
|
159
|
+
|
|
160
|
+
expect(result).toBe(false);
|
|
161
|
+
|
|
162
|
+
expect(customComparator).toHaveBeenCalledWith(
|
|
163
|
+
prev.byComparatorProp,
|
|
164
|
+
next.byComparatorProp
|
|
165
|
+
);
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
it("remaining keys comparison fails", () => {
|
|
169
|
+
const sharedArray = [1, 2, 3];
|
|
170
|
+
|
|
171
|
+
const prev = {
|
|
172
|
+
byValueProp: { a: 1, b: 2 },
|
|
173
|
+
byRefProp: sharedArray,
|
|
174
|
+
byComparatorProp: "test",
|
|
175
|
+
extraProp: "original",
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
const next = {
|
|
179
|
+
byValueProp: { a: 1, b: 2 },
|
|
180
|
+
byRefProp: sharedArray,
|
|
181
|
+
byComparatorProp: "different",
|
|
182
|
+
extraProp: "changed",
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
const customComparator = jest.fn().mockReturnValue(true);
|
|
186
|
+
|
|
187
|
+
const result = isEqual({
|
|
188
|
+
byValue: ["byValueProp"],
|
|
189
|
+
byReference: ["byRefProp"],
|
|
190
|
+
byComparator: {
|
|
191
|
+
comparator: customComparator,
|
|
192
|
+
keys: ["byComparatorProp"],
|
|
193
|
+
},
|
|
194
|
+
compareRemainingKeysBySameValue: true,
|
|
195
|
+
})(prev, next);
|
|
196
|
+
|
|
197
|
+
expect(result).toBe(false);
|
|
198
|
+
expect(customComparator).toHaveBeenCalled();
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
it("with debug flag", () => {
|
|
202
|
+
const consoleSpy = jest.spyOn(console, "log").mockImplementation();
|
|
203
|
+
|
|
204
|
+
const prev = {
|
|
205
|
+
byValueProp: { a: 1, b: 2 },
|
|
206
|
+
byRefProp: array,
|
|
207
|
+
byComparatorProp: "test",
|
|
208
|
+
extraProp: sameProp,
|
|
209
|
+
};
|
|
210
|
+
|
|
211
|
+
const next = {
|
|
212
|
+
byValueProp: { a: 1, b: 2 },
|
|
213
|
+
byRefProp: array,
|
|
214
|
+
byComparatorProp: "different",
|
|
215
|
+
extraProp: sameProp,
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
const customComparator = jest.fn().mockReturnValue(true);
|
|
219
|
+
|
|
220
|
+
const result = isEqual({
|
|
221
|
+
byValue: ["byValueProp"],
|
|
222
|
+
byReference: ["byRefProp"],
|
|
223
|
+
byComparator: {
|
|
224
|
+
comparator: customComparator,
|
|
225
|
+
keys: ["byComparatorProp"],
|
|
226
|
+
},
|
|
227
|
+
compareRemainingKeysBySameValue: true,
|
|
228
|
+
debug: true,
|
|
229
|
+
})(prev, next);
|
|
230
|
+
|
|
231
|
+
expect(result).toBe(true);
|
|
232
|
+
expect(consoleSpy).toHaveBeenCalled();
|
|
233
|
+
|
|
234
|
+
consoleSpy.mockRestore();
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
it("handles missing properties", () => {
|
|
238
|
+
const prev = {
|
|
239
|
+
byValueProp: { a: 1, b: 2 },
|
|
240
|
+
byRefProp: [1, 2, 3],
|
|
241
|
+
};
|
|
242
|
+
|
|
243
|
+
const next = {
|
|
244
|
+
byValueProp: { a: 1, b: 2 },
|
|
245
|
+
byComparatorProp: "test",
|
|
246
|
+
};
|
|
247
|
+
|
|
248
|
+
const customComparator = jest.fn().mockReturnValue(true);
|
|
249
|
+
|
|
250
|
+
const result = isEqual({
|
|
251
|
+
byValue: ["byValueProp"],
|
|
252
|
+
byReference: ["byRefProp"],
|
|
253
|
+
byComparator: {
|
|
254
|
+
comparator: customComparator,
|
|
255
|
+
keys: ["byComparatorProp"],
|
|
256
|
+
},
|
|
257
|
+
})(prev, next);
|
|
258
|
+
|
|
259
|
+
expect(result).toBe(false);
|
|
260
|
+
expect(customComparator).not.toHaveBeenCalled();
|
|
261
|
+
});
|
|
262
|
+
});
|
|
263
|
+
});
|
package/equalUtils/index.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
/* eslint no-console: off */
|
|
2
2
|
|
|
3
|
-
import
|
|
3
|
+
import { equals } from "ramda";
|
|
4
|
+
|
|
5
|
+
const alwaysTrue = () => true;
|
|
4
6
|
|
|
5
7
|
export const isParentFocusEqual = (
|
|
6
8
|
prevParentFocus: ParentFocus,
|
|
@@ -18,7 +20,7 @@ export const isEqual =
|
|
|
18
20
|
({
|
|
19
21
|
byValue = [],
|
|
20
22
|
byReference = [],
|
|
21
|
-
byComparator = { comparator:
|
|
23
|
+
byComparator = { comparator: alwaysTrue, keys: [] },
|
|
22
24
|
debug = false,
|
|
23
25
|
compareRemainingKeysBySameValue = false,
|
|
24
26
|
}: {
|
|
@@ -29,23 +31,28 @@ export const isEqual =
|
|
|
29
31
|
compareRemainingKeysBySameValue?: boolean;
|
|
30
32
|
}) =>
|
|
31
33
|
(prev, next) => {
|
|
32
|
-
const
|
|
33
|
-
|
|
34
|
-
|
|
34
|
+
for (const key of byReference) {
|
|
35
|
+
if (prev[key] !== next[key]) {
|
|
36
|
+
if (debug) console.log({ isEqualByReference: false });
|
|
35
37
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
39
41
|
|
|
40
|
-
const
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
(key) => byComparator.comparator(prev[key], next[key]),
|
|
44
|
-
byComparator.keys
|
|
45
|
-
);
|
|
42
|
+
for (const key of byValue) {
|
|
43
|
+
if (!equals(prev[key], next[key])) {
|
|
44
|
+
if (debug) console.log({ isEqualByValue: false });
|
|
46
45
|
|
|
47
|
-
|
|
48
|
-
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
for (const key of byComparator.keys) {
|
|
51
|
+
if (!byComparator.comparator(prev[key], next[key])) {
|
|
52
|
+
if (debug) console.log({ isEqualByComparator: false });
|
|
53
|
+
|
|
54
|
+
return false;
|
|
55
|
+
}
|
|
49
56
|
}
|
|
50
57
|
|
|
51
58
|
if (compareRemainingKeysBySameValue) {
|
|
@@ -57,21 +64,26 @@ export const isEqual =
|
|
|
57
64
|
...byComparator.keys,
|
|
58
65
|
]);
|
|
59
66
|
|
|
60
|
-
const
|
|
61
|
-
(key)
|
|
62
|
-
|
|
67
|
+
for (const key of Array.from(allKeys)) {
|
|
68
|
+
if (!declaredKeys.has(key) && !Object.is(prev[key], next[key])) {
|
|
69
|
+
if (debug) console.log({ isRestKeysEqualAllBySameValue: false });
|
|
63
70
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
71
|
+
return false;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
67
75
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
76
|
+
// if we made it here, everything matched
|
|
77
|
+
if (debug) {
|
|
78
|
+
console.log({
|
|
79
|
+
isEqualByValue: true,
|
|
80
|
+
isEqualByReference: true,
|
|
81
|
+
isEqualByComparator: true,
|
|
82
|
+
isRestKeysEqualAllBySameValue: compareRemainingKeysBySameValue
|
|
83
|
+
? true
|
|
84
|
+
: undefined,
|
|
85
|
+
});
|
|
74
86
|
}
|
|
75
87
|
|
|
76
|
-
return
|
|
88
|
+
return true;
|
|
77
89
|
};
|
package/focusManager/index.ts
CHANGED
|
@@ -8,6 +8,8 @@ import { isNilOrEmpty } from "../reactUtils/helpers";
|
|
|
8
8
|
import { isAndroidTVPlatform } from "../reactUtils";
|
|
9
9
|
import { noop } from "../functionUtils";
|
|
10
10
|
|
|
11
|
+
const EMPTY_GROUP_COMPONENT = "empty_group_component";
|
|
12
|
+
|
|
11
13
|
export function useFocusManager() {
|
|
12
14
|
return focusManager;
|
|
13
15
|
}
|
|
@@ -74,6 +76,14 @@ export function useFocusEffect(onFocus, focusWatcher) {
|
|
|
74
76
|
}, focusWatcher);
|
|
75
77
|
}
|
|
76
78
|
|
|
79
|
+
export function shouldShowEmptyGroup(component) {
|
|
80
|
+
return (
|
|
81
|
+
component?.ui_components?.filter(
|
|
82
|
+
({ component_type }) => component_type === EMPTY_GROUP_COMPONENT
|
|
83
|
+
)?.length > 0
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
|
|
77
87
|
export const isFocusable = (component: ZappEntry | ZappUIComponent | any) =>
|
|
78
88
|
R.tryCatch(
|
|
79
89
|
R.compose(
|
|
@@ -101,15 +111,42 @@ export const useIsFocusable = (props: Props): void => {
|
|
|
101
111
|
component.rules.hide_component_if_data_is_empty
|
|
102
112
|
);
|
|
103
113
|
|
|
114
|
+
const isLoadingData = !!zappPipesData?.loading;
|
|
115
|
+
|
|
104
116
|
const hasNoData =
|
|
105
117
|
isNilOrEmpty(zappPipesData?.data) ||
|
|
106
118
|
isNilOrEmpty(zappPipesData?.data?.entry);
|
|
107
119
|
|
|
120
|
+
const hasDataError = !!zappPipesData?.error;
|
|
121
|
+
|
|
122
|
+
const hasEmptyGroup = shouldShowEmptyGroup(component);
|
|
123
|
+
|
|
124
|
+
const isGallery = component?.component_type === "gallery-qb";
|
|
125
|
+
|
|
126
|
+
const isHiddenDueToEmptyData =
|
|
127
|
+
!isLoadingData && hasNoData && hideIfDataEmpty && !hasEmptyGroup;
|
|
128
|
+
|
|
129
|
+
const defaultFocusableLogic =
|
|
130
|
+
componentCellsFocusable && (!hasNoData || !hideIfDataEmpty);
|
|
131
|
+
|
|
108
132
|
React.useEffect(() => {
|
|
109
133
|
if (isAndroidTVPlatform()) {
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
)
|
|
134
|
+
let isFocusableNew = false;
|
|
135
|
+
|
|
136
|
+
if (isGallery) {
|
|
137
|
+
isFocusableNew = true;
|
|
138
|
+
} else if (isFocusable(component)) {
|
|
139
|
+
isFocusableNew = defaultFocusableLogic;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
setIsFocusable(isFocusableNew);
|
|
113
143
|
}
|
|
114
|
-
}, [
|
|
144
|
+
}, [
|
|
145
|
+
defaultFocusableLogic,
|
|
146
|
+
isHiddenDueToEmptyData,
|
|
147
|
+
isGallery,
|
|
148
|
+
setIsFocusable,
|
|
149
|
+
hasDataError,
|
|
150
|
+
hasEmptyGroup,
|
|
151
|
+
]);
|
|
115
152
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@applicaster/zapp-react-native-utils",
|
|
3
|
-
"version": "13.0.0-rc.
|
|
3
|
+
"version": "13.0.0-rc.45",
|
|
4
4
|
"description": "Applicaster Zapp React Native utilities package",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"types": "index.d.ts",
|
|
@@ -28,11 +28,10 @@
|
|
|
28
28
|
},
|
|
29
29
|
"homepage": "https://github.com/applicaster/quickbrick#readme",
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@applicaster/applicaster-types": "13.0.0-rc.
|
|
31
|
+
"@applicaster/applicaster-types": "13.0.0-rc.45",
|
|
32
32
|
"buffer": "^5.2.1",
|
|
33
33
|
"camelize": "^1.0.0",
|
|
34
34
|
"create-react-context": "^0.2.3",
|
|
35
|
-
"create-react-ref": "^0.1.0",
|
|
36
35
|
"dayjs": "^1.11.10",
|
|
37
36
|
"memoizee": "0.4.15",
|
|
38
37
|
"prop-types": "^15.0.0",
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
|
2
2
|
|
|
3
|
-
exports[`addLifeCycleMethods attaches the lifecycle method to the component 1`] = `
|
|
3
|
+
exports[`reactUtils helpers addLifeCycleMethods attaches the lifecycle method to the component 1`] = `
|
|
4
4
|
<View>
|
|
5
5
|
<Text>
|
|
6
6
|
I'm a class component
|
|
@@ -8,7 +8,7 @@ exports[`addLifeCycleMethods attaches the lifecycle method to the component 1`]
|
|
|
8
8
|
</View>
|
|
9
9
|
`;
|
|
10
10
|
|
|
11
|
-
exports[`attachMethodToPrototype attaches a lifecycle method to the component's prototype 1`] = `
|
|
11
|
+
exports[`reactUtils helpers attachMethodToPrototype attaches a lifecycle method to the component's prototype 1`] = `
|
|
12
12
|
<View>
|
|
13
13
|
<Text>
|
|
14
14
|
I'm a class component
|
|
@@ -16,6 +16,18 @@ exports[`attachMethodToPrototype attaches a lifecycle method to the component's
|
|
|
16
16
|
</View>
|
|
17
17
|
`;
|
|
18
18
|
|
|
19
|
-
exports[`attachMethodToPrototype preserves any existing lifecycle method 1`] =
|
|
19
|
+
exports[`reactUtils helpers attachMethodToPrototype preserves any existing lifecycle method 1`] = `
|
|
20
|
+
<View>
|
|
21
|
+
<Text>
|
|
22
|
+
I'm a stateless component
|
|
23
|
+
</Text>
|
|
24
|
+
</View>
|
|
25
|
+
`;
|
|
20
26
|
|
|
21
|
-
exports[`wrapInClass wraps the stateless component in a class 1`] =
|
|
27
|
+
exports[`reactUtils helpers wrapInClass wraps the stateless component in a class 1`] = `
|
|
28
|
+
<View>
|
|
29
|
+
<Text>
|
|
30
|
+
I'm a stateless component
|
|
31
|
+
</Text>
|
|
32
|
+
</View>
|
|
33
|
+
`;
|
|
@@ -7,7 +7,7 @@ import {
|
|
|
7
7
|
componentDidMount,
|
|
8
8
|
} from "./fixtures";
|
|
9
9
|
|
|
10
|
-
import { attachLifeCycleMethods,
|
|
10
|
+
import { attachLifeCycleMethods, mergeChildrenProps } from "..";
|
|
11
11
|
|
|
12
12
|
describe("attachLifeCycleMethods", () => {
|
|
13
13
|
let addLifeCycleMethodsSpy;
|
|
@@ -41,24 +41,6 @@ describe("attachLifeCycleMethods", () => {
|
|
|
41
41
|
});
|
|
42
42
|
});
|
|
43
43
|
|
|
44
|
-
describe("createReactRef", () => {
|
|
45
|
-
it("calls the React.createRef function if it exists", () => {
|
|
46
|
-
jest.clearAllMocks();
|
|
47
|
-
|
|
48
|
-
const ref = createReactRef();
|
|
49
|
-
expect(ref).toHaveProperty("current");
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
it("calls the polyfill if React.createRef doesn't exist", () => {
|
|
53
|
-
jest.clearAllMocks();
|
|
54
|
-
const originalCreateRef = React.createRef;
|
|
55
|
-
React.createRef = undefined;
|
|
56
|
-
const ref = createReactRef();
|
|
57
|
-
expect(ref).toHaveProperty("current");
|
|
58
|
-
React.createRef = originalCreateRef;
|
|
59
|
-
});
|
|
60
|
-
});
|
|
61
|
-
|
|
62
44
|
describe("mergeChildrenProps", () => {
|
|
63
45
|
it("returns the cloned children with the merged props", () => {
|
|
64
46
|
const Egg = () => React.createElement("Egg");
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import * as React from "react";
|
|
2
|
-
import {
|
|
3
|
-
import { shallowToJson } from "enzyme-to-json";
|
|
2
|
+
import { render } from "@testing-library/react-native";
|
|
4
3
|
|
|
5
4
|
import {
|
|
6
5
|
isClassComponent,
|
|
@@ -20,98 +19,100 @@ import {
|
|
|
20
19
|
} from "./fixtures";
|
|
21
20
|
|
|
22
21
|
describe("reactUtils helpers", () => {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
22
|
+
describe("isNilOrEmpty", () => {
|
|
23
|
+
it("should return correct output", () => {
|
|
24
|
+
expect(isNilOrEmpty).toBeDefined();
|
|
25
|
+
expect(isNilOrEmpty(null)).toBeTruthy();
|
|
26
|
+
expect(isNilOrEmpty(undefined)).toBeTruthy();
|
|
27
|
+
expect(isNilOrEmpty("")).toBeTruthy();
|
|
28
|
+
expect(isNilOrEmpty([])).toBeTruthy();
|
|
29
|
+
});
|
|
29
30
|
});
|
|
30
|
-
});
|
|
31
31
|
|
|
32
|
-
describe("isClassComponent", () => {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
32
|
+
describe("isClassComponent", () => {
|
|
33
|
+
it("returns true if the component is a class component", () => {
|
|
34
|
+
expect(isClassComponent(ClassComponent)).toBe(true);
|
|
35
|
+
});
|
|
36
36
|
|
|
37
|
-
|
|
38
|
-
|
|
37
|
+
it("returns false otherwise", () => {
|
|
38
|
+
expect(isClassComponent(StatelessComponent)).toBe(false);
|
|
39
|
+
});
|
|
39
40
|
});
|
|
40
|
-
});
|
|
41
41
|
|
|
42
|
-
describe("attachMethodToPrototype", () => {
|
|
43
|
-
|
|
42
|
+
describe("attachMethodToPrototype", () => {
|
|
43
|
+
const initialWrapper = render(<ClassComponent />);
|
|
44
44
|
|
|
45
|
-
|
|
46
|
-
COMPONENT_DID_MOUNT_METHOD
|
|
47
|
-
);
|
|
48
|
-
|
|
49
|
-
afterEach(() => {
|
|
50
|
-
componentDidMount.mockClear();
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
it("attaches a lifecycle method to the component's prototype", () => {
|
|
54
|
-
const NewComponent = attachMethodToPrototype(ClassComponent)(
|
|
55
|
-
componentDidMount,
|
|
45
|
+
expect(initialWrapper.toJSON()).not.toHaveProperty(
|
|
56
46
|
COMPONENT_DID_MOUNT_METHOD
|
|
57
47
|
);
|
|
58
48
|
|
|
59
|
-
|
|
49
|
+
afterEach(() => {
|
|
50
|
+
componentDidMount.mockClear();
|
|
51
|
+
});
|
|
60
52
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
53
|
+
it("attaches a lifecycle method to the component's prototype", () => {
|
|
54
|
+
const NewComponent = attachMethodToPrototype(ClassComponent)(
|
|
55
|
+
componentDidMount,
|
|
56
|
+
COMPONENT_DID_MOUNT_METHOD
|
|
57
|
+
);
|
|
65
58
|
|
|
66
|
-
|
|
67
|
-
const NewComponent = attachMethodToPrototype(ComponentWithLifeCycle)(
|
|
68
|
-
componentDidMount,
|
|
69
|
-
COMPONENT_DID_MOUNT_METHOD
|
|
70
|
-
);
|
|
59
|
+
const wrapper = render(<NewComponent />);
|
|
71
60
|
|
|
72
|
-
|
|
61
|
+
expect(wrapper.toJSON(wrapper)).toMatchSnapshot();
|
|
62
|
+
expect(new NewComponent()).toHaveProperty(COMPONENT_DID_MOUNT_METHOD);
|
|
63
|
+
expect(componentDidMount).toHaveBeenCalled();
|
|
64
|
+
});
|
|
73
65
|
|
|
74
|
-
|
|
66
|
+
it("preserves any existing lifecycle method", () => {
|
|
67
|
+
const NewComponent = attachMethodToPrototype(ComponentWithLifeCycle)(
|
|
68
|
+
componentDidMount,
|
|
69
|
+
COMPONENT_DID_MOUNT_METHOD
|
|
70
|
+
);
|
|
75
71
|
|
|
76
|
-
|
|
77
|
-
expect(componentDidMount).toHaveBeenCalled();
|
|
78
|
-
expect(existingMethod).toHaveBeenCalled();
|
|
79
|
-
});
|
|
80
|
-
});
|
|
72
|
+
expect(componentDidMount).not.toHaveBeenCalled();
|
|
81
73
|
|
|
82
|
-
|
|
83
|
-
let NewComponent;
|
|
84
|
-
let wrapper;
|
|
74
|
+
const wrapper = render(<NewComponent />);
|
|
85
75
|
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
76
|
+
expect(wrapper.toJSON()).toMatchSnapshot();
|
|
77
|
+
expect(componentDidMount).toHaveBeenCalled();
|
|
78
|
+
expect(existingMethod).toHaveBeenCalled();
|
|
79
|
+
});
|
|
89
80
|
});
|
|
90
81
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
});
|
|
95
|
-
});
|
|
82
|
+
describe("wrapInClass", () => {
|
|
83
|
+
let NewComponent;
|
|
84
|
+
let wrapper;
|
|
96
85
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
86
|
+
beforeEach(() => {
|
|
87
|
+
NewComponent = wrapInClass(StatelessComponent);
|
|
88
|
+
wrapper = render(<NewComponent />);
|
|
89
|
+
});
|
|
100
90
|
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
91
|
+
it("wraps the stateless component in a class", () => {
|
|
92
|
+
expect(isClassComponent(NewComponent)).toBe(true);
|
|
93
|
+
expect(wrapper.toJSON()).toMatchSnapshot();
|
|
94
|
+
});
|
|
104
95
|
});
|
|
105
96
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
97
|
+
describe("addLifeCycleMethods", () => {
|
|
98
|
+
let NewComponent;
|
|
99
|
+
let wrapper;
|
|
109
100
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
101
|
+
beforeEach(() => {
|
|
102
|
+
NewComponent = addLifeCycleMethods({ componentDidMount })(ClassComponent);
|
|
103
|
+
wrapper = render(<NewComponent />);
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
afterEach(() => {
|
|
107
|
+
componentDidMount.mockClear();
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it("attaches the lifecycle method to the component", () => {
|
|
111
|
+
expect(wrapper.toJSON()).toMatchSnapshot();
|
|
112
|
+
});
|
|
113
113
|
|
|
114
|
-
|
|
115
|
-
|
|
114
|
+
it("adds the lifecycle method to the component", () => {
|
|
115
|
+
expect(componentDidMount).toHaveBeenCalled();
|
|
116
|
+
});
|
|
116
117
|
});
|
|
117
118
|
});
|
|
@@ -1,59 +1,27 @@
|
|
|
1
1
|
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
|
2
2
|
|
|
3
|
-
exports[`createContextSetters when no state validator is provided calls setState when the setters are called, no matter what the value is 1`] = `
|
|
3
|
+
exports[`helpers createContextSetters when no state validator is provided calls setState when the setters are called, no matter what the value is 1`] = `
|
|
4
4
|
{
|
|
5
5
|
"bar": "bar",
|
|
6
6
|
"foo": "foo",
|
|
7
7
|
}
|
|
8
8
|
`;
|
|
9
9
|
|
|
10
|
-
exports[`createContextSetters when no state validator is provided calls setState when the setters are called, no matter what the value is 2`] = `
|
|
11
|
-
{
|
|
12
|
-
"bar": "bar",
|
|
13
|
-
"foo": "invalid foo",
|
|
14
|
-
}
|
|
15
|
-
`;
|
|
16
|
-
|
|
17
|
-
exports[`createContextSetters when no state validator is provided calls setState when the setters are called, no matter what the value is 3`] = `
|
|
18
|
-
{
|
|
19
|
-
"bar": "invalid bar",
|
|
20
|
-
"foo": "invalid foo",
|
|
21
|
-
}
|
|
22
|
-
`;
|
|
23
|
-
|
|
24
|
-
exports[`createContextSetters when no state validator is provided calls setState when the setters are called, no matter what the value is 4`] = `
|
|
25
|
-
[
|
|
26
|
-
[
|
|
27
|
-
[Function],
|
|
28
|
-
],
|
|
29
|
-
[
|
|
30
|
-
[Function],
|
|
31
|
-
],
|
|
32
|
-
]
|
|
33
|
-
`;
|
|
34
|
-
|
|
35
|
-
exports[`createContextSetters when state validator is provided calls setState when the setters are called & the validator passes 1`] = `
|
|
10
|
+
exports[`helpers createContextSetters when no state validator is provided calls setState when the setters are called, no matter what the value is 2`] = `
|
|
36
11
|
{
|
|
37
12
|
"bar": "bar",
|
|
38
13
|
"foo": "foo",
|
|
39
14
|
}
|
|
40
15
|
`;
|
|
41
16
|
|
|
42
|
-
exports[`createContextSetters when state validator is provided calls setState when the setters are called
|
|
17
|
+
exports[`helpers createContextSetters when no state validator is provided calls setState when the setters are called, no matter what the value is 3`] = `
|
|
43
18
|
{
|
|
44
19
|
"bar": "bar",
|
|
45
|
-
"foo": "
|
|
46
|
-
}
|
|
47
|
-
`;
|
|
48
|
-
|
|
49
|
-
exports[`createContextSetters when state validator is provided calls setState when the setters are called & the validator passes 3`] = `
|
|
50
|
-
{
|
|
51
|
-
"bar": "new bar",
|
|
52
|
-
"foo": "new foo",
|
|
20
|
+
"foo": "foo",
|
|
53
21
|
}
|
|
54
22
|
`;
|
|
55
23
|
|
|
56
|
-
exports[`createContextSetters when state validator is provided calls setState when the setters are called
|
|
24
|
+
exports[`helpers createContextSetters when no state validator is provided calls setState when the setters are called, no matter what the value is 4`] = `
|
|
57
25
|
[
|
|
58
26
|
[
|
|
59
27
|
[Function],
|
|
@@ -64,43 +32,9 @@ exports[`createContextSetters when state validator is provided calls setState wh
|
|
|
64
32
|
]
|
|
65
33
|
`;
|
|
66
34
|
|
|
67
|
-
exports[`createContextSetters when state validator is provided skips the update if the new value is the same as the previous one 1`] = `
|
|
35
|
+
exports[`helpers createContextSetters when state validator is provided skips the update if the new value is the same as the previous one 1`] = `
|
|
68
36
|
{
|
|
69
37
|
"bar": "bar",
|
|
70
38
|
"foo": "new foo",
|
|
71
39
|
}
|
|
72
40
|
`;
|
|
73
|
-
|
|
74
|
-
exports[`createContextSetters when state validator is provided skips the update if the state validator doesn't pass 1`] = `
|
|
75
|
-
{
|
|
76
|
-
"bar": "bar",
|
|
77
|
-
"foo": "foo",
|
|
78
|
-
}
|
|
79
|
-
`;
|
|
80
|
-
|
|
81
|
-
exports[`createContextSetters when state validator is provided skips the update if the state validator doesn't pass 2`] = `
|
|
82
|
-
{
|
|
83
|
-
"bar": "bar",
|
|
84
|
-
"foo": "foo",
|
|
85
|
-
}
|
|
86
|
-
`;
|
|
87
|
-
|
|
88
|
-
exports[`createContextSetters when state validator is provided skips the update if the state validator doesn't pass 3`] = `
|
|
89
|
-
{
|
|
90
|
-
"bar": "bar",
|
|
91
|
-
"foo": "foo",
|
|
92
|
-
}
|
|
93
|
-
`;
|
|
94
|
-
|
|
95
|
-
exports[`createContextSetters when state validator is provided skips the update if the state validator doesn't pass 4`] = `
|
|
96
|
-
[
|
|
97
|
-
[
|
|
98
|
-
"trying to invoke setFoo with invalid foo - but it was rejected by the state validator
|
|
99
|
-
skipping context update",
|
|
100
|
-
],
|
|
101
|
-
[
|
|
102
|
-
"trying to invoke setBar with invalid bar - but it was rejected by the state validator
|
|
103
|
-
skipping context update",
|
|
104
|
-
],
|
|
105
|
-
]
|
|
106
|
-
`;
|
|
@@ -1,32 +1,7 @@
|
|
|
1
1
|
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
|
2
2
|
|
|
3
3
|
exports[`createProvider creates a context Provider component 1`] = `
|
|
4
|
-
<
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
"context": "initialValue",
|
|
8
|
-
"otherContext": "otherValue",
|
|
9
|
-
"setContext": [Function],
|
|
10
|
-
"setOtherContext": [Function],
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
>
|
|
14
|
-
<MockChild
|
|
15
|
-
context="context"
|
|
16
|
-
/>
|
|
17
|
-
</mockConstructor>
|
|
18
|
-
`;
|
|
19
|
-
|
|
20
|
-
exports[`createProvider creates a context Provider component 2`] = `
|
|
21
|
-
{
|
|
22
|
-
"children": <MockChild
|
|
23
|
-
context="context"
|
|
24
|
-
/>,
|
|
25
|
-
"value": {
|
|
26
|
-
"context": "initialValue",
|
|
27
|
-
"otherContext": "otherValue",
|
|
28
|
-
"setContext": [Function],
|
|
29
|
-
"setOtherContext": [Function],
|
|
30
|
-
},
|
|
31
|
-
}
|
|
4
|
+
<View>
|
|
5
|
+
context
|
|
6
|
+
</View>
|
|
32
7
|
`;
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import * as React from "react";
|
|
2
|
-
import { shallow } from "enzyme";
|
|
3
2
|
import { View, Text } from "react-native";
|
|
4
3
|
|
|
4
|
+
import { NOOP, getSetterName, createContextSetters } from "../helpers";
|
|
5
|
+
|
|
5
6
|
function stateValidator({ property, value }) {
|
|
6
7
|
return (
|
|
7
8
|
(property === "foo" && value === "new foo") ||
|
|
@@ -9,150 +10,135 @@ function stateValidator({ property, value }) {
|
|
|
9
10
|
);
|
|
10
11
|
}
|
|
11
12
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
reactUtilsLogger: {
|
|
18
|
-
addSubsystem: jest.fn(() => mockLogger),
|
|
19
|
-
},
|
|
20
|
-
}));
|
|
21
|
-
|
|
22
|
-
const { NOOP, getSetterName, createContextSetters } = require("../helpers");
|
|
23
|
-
|
|
24
|
-
describe("NOOP", () => {
|
|
25
|
-
it("does nothing", () => {
|
|
26
|
-
expect(NOOP()).toBeUndefined();
|
|
13
|
+
describe("helpers", () => {
|
|
14
|
+
describe("NOOP", () => {
|
|
15
|
+
it("does nothing", () => {
|
|
16
|
+
expect(NOOP()).toBeUndefined();
|
|
17
|
+
});
|
|
27
18
|
});
|
|
28
|
-
});
|
|
29
19
|
|
|
30
|
-
describe("getSetterName", () => {
|
|
31
|
-
|
|
32
|
-
|
|
20
|
+
describe("getSetterName", () => {
|
|
21
|
+
it("returns a setter name for the given property name", () => {
|
|
22
|
+
expect(getSetterName("fooBar")).toBe("setFooBar");
|
|
23
|
+
});
|
|
33
24
|
});
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
describe("createContextSetters", () => {
|
|
37
|
-
describe("when state validator is provided", () => {
|
|
38
|
-
class Component extends React.Component {
|
|
39
|
-
constructor(props) {
|
|
40
|
-
super(props);
|
|
41
|
-
this.state = { foo: "foo", bar: "bar" };
|
|
42
|
-
createContextSetters(["foo", "bar"], this, { stateValidator });
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
resetInitialState() {
|
|
46
|
-
this.setState({ foo: "foo", bar: "bar" });
|
|
47
|
-
}
|
|
48
25
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
26
|
+
describe("createContextSetters", () => {
|
|
27
|
+
describe("when state validator is provided", () => {
|
|
28
|
+
class ClassComponent {
|
|
29
|
+
constructor() {
|
|
30
|
+
this._state = { foo: "foo", bar: "bar" };
|
|
31
|
+
createContextSetters(["foo", "bar"], this, { stateValidator });
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
setState(newState) {
|
|
35
|
+
if (typeof newState === "function") {
|
|
36
|
+
this.state = newState(this.state);
|
|
37
|
+
} else {
|
|
38
|
+
this.state = { ...this.state, ...newState };
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
set state(val) {
|
|
43
|
+
this._state = val;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
get state() {
|
|
47
|
+
return this._state;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
getState() {
|
|
51
|
+
return this._state;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
resetInitialState() {
|
|
55
|
+
this.setState({ foo: "foo", bar: "bar" });
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
render() {
|
|
59
|
+
return <View testID="test-view">{JSON.stringify(this.state)}</View>;
|
|
60
|
+
}
|
|
55
61
|
}
|
|
56
|
-
}
|
|
57
62
|
|
|
58
|
-
|
|
59
|
-
const instance = wrapper.instance();
|
|
60
|
-
const setStateSpy = jest.spyOn(instance, "setState");
|
|
63
|
+
let instance;
|
|
61
64
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
mockLogger.warning.mockClear();
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
it("adds all properties and setters to the component", () => {
|
|
69
|
-
expect(instance).toHaveProperty("setFoo");
|
|
70
|
-
expect(instance.setFoo).toBeFunction();
|
|
71
|
-
expect(instance).toHaveProperty("setBar");
|
|
72
|
-
expect(instance.setBar).toBeFunction();
|
|
73
|
-
});
|
|
65
|
+
beforeEach(() => {
|
|
66
|
+
instance = new ClassComponent();
|
|
67
|
+
});
|
|
74
68
|
|
|
75
|
-
|
|
76
|
-
|
|
69
|
+
it("calls setState when the setters are called & the validator passes", () => {
|
|
70
|
+
expect(instance.state.foo).toEqual("foo");
|
|
71
|
+
expect(instance.state.bar).toEqual("bar");
|
|
77
72
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
instance.setBar("new bar");
|
|
82
|
-
expect(instance.state).toMatchSnapshot();
|
|
83
|
-
|
|
84
|
-
expect(setStateSpy).toHaveBeenCalledTimes(2);
|
|
85
|
-
expect(setStateSpy.mock.calls).toMatchSnapshot();
|
|
86
|
-
expect(mockLogger.warning).not.toHaveBeenCalled();
|
|
87
|
-
});
|
|
73
|
+
instance.setFoo("new foo");
|
|
74
|
+
expect(instance.state.foo).toEqual("new foo");
|
|
88
75
|
|
|
89
|
-
|
|
90
|
-
|
|
76
|
+
instance.setBar("new bar");
|
|
77
|
+
expect(instance.state.bar).toEqual("new bar");
|
|
78
|
+
});
|
|
91
79
|
|
|
92
|
-
|
|
93
|
-
|
|
80
|
+
it("skips the update if the state validator doesn't pass", () => {
|
|
81
|
+
expect(instance.state.foo).toEqual("foo");
|
|
82
|
+
expect(instance.state.bar).toEqual("bar");
|
|
94
83
|
|
|
95
|
-
|
|
96
|
-
|
|
84
|
+
instance.setFoo("invalid foo");
|
|
85
|
+
expect(instance.state.foo).toEqual("foo");
|
|
97
86
|
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
});
|
|
87
|
+
instance.setBar("invalid bar");
|
|
88
|
+
expect(instance.state.bar).toEqual("bar");
|
|
89
|
+
});
|
|
102
90
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
91
|
+
it("skips the update if the new value is the same as the previous one", () => {
|
|
92
|
+
instance.setFoo("new foo");
|
|
93
|
+
instance.setFoo("new foo");
|
|
94
|
+
expect(instance.state).toMatchSnapshot();
|
|
95
|
+
});
|
|
108
96
|
});
|
|
109
|
-
});
|
|
110
|
-
|
|
111
|
-
describe("when no state validator is provided", () => {
|
|
112
|
-
class Component extends React.Component {
|
|
113
|
-
constructor(props) {
|
|
114
|
-
super(props);
|
|
115
|
-
this.state = { foo: "foo", bar: "bar" };
|
|
116
|
-
createContextSetters(["foo", "bar"], this);
|
|
117
|
-
}
|
|
118
97
|
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
98
|
+
describe("when no state validator is provided", () => {
|
|
99
|
+
class Component extends React.Component {
|
|
100
|
+
constructor(props) {
|
|
101
|
+
super(props);
|
|
102
|
+
this.state = { foo: "foo", bar: "bar" };
|
|
103
|
+
createContextSetters(["foo", "bar"], this);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
render() {
|
|
107
|
+
return (
|
|
108
|
+
<View>
|
|
109
|
+
<Text>I am a component </Text>
|
|
110
|
+
</View>
|
|
111
|
+
);
|
|
112
|
+
}
|
|
125
113
|
}
|
|
126
|
-
}
|
|
127
114
|
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
115
|
+
// const wrapper = new Component();
|
|
116
|
+
const instance = new Component();
|
|
117
|
+
const setStateSpy = jest.spyOn(instance, "setState");
|
|
131
118
|
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
});
|
|
119
|
+
afterEach(() => {
|
|
120
|
+
setStateSpy.mockClear();
|
|
121
|
+
});
|
|
136
122
|
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
123
|
+
it("adds all properties and setters to the component", () => {
|
|
124
|
+
expect(instance).toHaveProperty("setFoo");
|
|
125
|
+
expect(instance.setFoo).toBeFunction();
|
|
126
|
+
expect(instance).toHaveProperty("setBar");
|
|
127
|
+
expect(instance.setBar).toBeFunction();
|
|
128
|
+
});
|
|
143
129
|
|
|
144
|
-
|
|
145
|
-
|
|
130
|
+
it("calls setState when the setters are called, no matter what the value is", () => {
|
|
131
|
+
expect(instance.state).toMatchSnapshot();
|
|
146
132
|
|
|
147
|
-
|
|
148
|
-
|
|
133
|
+
instance.setFoo("invalid foo");
|
|
134
|
+
expect(instance.state).toMatchSnapshot();
|
|
149
135
|
|
|
150
|
-
|
|
151
|
-
|
|
136
|
+
instance.setBar("invalid bar");
|
|
137
|
+
expect(instance.state).toMatchSnapshot();
|
|
152
138
|
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
139
|
+
expect(setStateSpy).toHaveBeenCalledTimes(2);
|
|
140
|
+
expect(setStateSpy.mock.calls).toMatchSnapshot();
|
|
141
|
+
});
|
|
156
142
|
});
|
|
157
143
|
});
|
|
158
144
|
});
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/* eslint-disable react/prop-types */
|
|
2
2
|
import * as React from "react";
|
|
3
|
-
import {
|
|
4
|
-
|
|
3
|
+
import { render } from "@testing-library/react-native";
|
|
4
|
+
|
|
5
5
|
import { View } from "react-native";
|
|
6
6
|
|
|
7
7
|
import { createProvider } from "../provider";
|
|
@@ -29,14 +29,12 @@ describe("createProvider", () => {
|
|
|
29
29
|
it("creates a context Provider component", () => {
|
|
30
30
|
const MockChild = (props) => <View>{props.context}</View>;
|
|
31
31
|
|
|
32
|
-
const wrapper =
|
|
32
|
+
const wrapper = render(
|
|
33
33
|
<Provider>
|
|
34
34
|
<MockChild context={"context"} />
|
|
35
35
|
</Provider>
|
|
36
36
|
);
|
|
37
37
|
|
|
38
|
-
expect(
|
|
39
|
-
|
|
40
|
-
expect(wrapper.find(ReactContext.Provider).props()).toMatchSnapshot();
|
|
38
|
+
expect(wrapper.toJSON()).toMatchSnapshot();
|
|
41
39
|
});
|
|
42
40
|
});
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/* eslint-disable react/prop-types */
|
|
2
2
|
import * as React from "react";
|
|
3
|
-
import {
|
|
4
|
-
|
|
3
|
+
import { render } from "@testing-library/react-native";
|
|
4
|
+
|
|
5
5
|
import { View } from "react-native";
|
|
6
6
|
|
|
7
7
|
import { createWithConsumerDecorator } from "../withConsumer";
|
|
@@ -11,21 +11,21 @@ describe("createConsumer", () => {
|
|
|
11
11
|
const { Consumer } = ReactContext;
|
|
12
12
|
const withConsumer = createWithConsumerDecorator(Consumer);
|
|
13
13
|
|
|
14
|
-
it("returns a function", () => {
|
|
15
|
-
expect(withConsumer).toBeFunction();
|
|
16
|
-
});
|
|
17
|
-
|
|
18
14
|
it("creates a context consumer function", () => {
|
|
19
|
-
|
|
20
|
-
const DecoratedChild = withConsumer(Child);
|
|
15
|
+
expect(withConsumer).toBeFunction();
|
|
21
16
|
const newContextValue = "newValue";
|
|
22
17
|
|
|
23
|
-
const
|
|
18
|
+
const Child = (props) => {
|
|
19
|
+
props.setContext(newContextValue);
|
|
20
|
+
|
|
21
|
+
return <View>{props.context}</View>;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
const DecoratedChild = withConsumer(Child);
|
|
24
25
|
|
|
25
|
-
const
|
|
26
|
+
const wrapper = render(<DecoratedChild />);
|
|
26
27
|
|
|
27
|
-
expect(
|
|
28
|
-
childWrapper.props().setContext(newContextValue);
|
|
28
|
+
expect(wrapper.toJSON()).toMatchSnapshot();
|
|
29
29
|
|
|
30
30
|
expect(mockContextData.setContext).toHaveBeenCalledWith(newContextValue);
|
|
31
31
|
});
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/* eslint-disable react/prop-types */
|
|
2
2
|
import * as React from "react";
|
|
3
|
-
import {
|
|
4
|
-
|
|
3
|
+
import { render } from "@testing-library/react-native";
|
|
4
|
+
|
|
5
5
|
import { View, Text } from "react-native";
|
|
6
6
|
|
|
7
7
|
import { createWithProviderDecorator } from "../withProvider";
|
|
@@ -19,8 +19,8 @@ describe("withProvider", () => {
|
|
|
19
19
|
|
|
20
20
|
const DecoratedComponent = withProvider(Component);
|
|
21
21
|
|
|
22
|
-
const wrapper =
|
|
22
|
+
const wrapper = render(<DecoratedComponent />);
|
|
23
23
|
|
|
24
|
-
expect(
|
|
24
|
+
expect(wrapper.toJSON()).toMatchSnapshot();
|
|
25
25
|
});
|
|
26
26
|
});
|
package/reactUtils/index.ts
CHANGED
|
@@ -2,7 +2,6 @@
|
|
|
2
2
|
import * as R from "ramda";
|
|
3
3
|
import * as React from "react";
|
|
4
4
|
import { Platform } from "react-native";
|
|
5
|
-
import createRef from "create-react-ref/lib/createRef";
|
|
6
5
|
|
|
7
6
|
import { addLifeCycleMethods, wrapInClass, isClassComponent } from "./helpers";
|
|
8
7
|
|
|
@@ -19,16 +18,6 @@ export function attachLifeCycleMethods(lifecycleMethods) {
|
|
|
19
18
|
);
|
|
20
19
|
}
|
|
21
20
|
|
|
22
|
-
/**
|
|
23
|
-
* creates a React Ref. if the version of React doesn't
|
|
24
|
-
* have this API, will use a polyfill instead
|
|
25
|
-
*/
|
|
26
|
-
export function createReactRef() {
|
|
27
|
-
return React.createRef && typeof React.createRef === "function"
|
|
28
|
-
? React.createRef()
|
|
29
|
-
: createRef();
|
|
30
|
-
}
|
|
31
|
-
|
|
32
21
|
/**
|
|
33
22
|
* Clone the children and merge each of the cloned elements with otherProps.
|
|
34
23
|
* @param {React.Children} children to clone
|
package/testUtils/index.tsx
CHANGED
|
@@ -15,16 +15,6 @@ import { ThemeContext } from "../theme";
|
|
|
15
15
|
|
|
16
16
|
export { getByTestId } from "./getByTestId";
|
|
17
17
|
|
|
18
|
-
/**
|
|
19
|
-
* Function to be used in tandem with enzyme findWhere(node => node.prop('testID') === 'yourID')
|
|
20
|
-
* Example:
|
|
21
|
-
* wrapper.findWhere(testIDEquals('yourID'))
|
|
22
|
-
* @param {*} testId
|
|
23
|
-
* @return boolean
|
|
24
|
-
*/
|
|
25
|
-
export const testIDEquals = (testId: any): boolean =>
|
|
26
|
-
R.compose(R.equals(testId), R.invoker(1, "prop")("testID"));
|
|
27
|
-
|
|
28
18
|
/** Helper function for react-test-renderer, accepts type and renderer
|
|
29
19
|
* * @param {*} type React Component type instance or name of component
|
|
30
20
|
* * @param {*} renderer renderer instance
|