@applicaster/zapp-react-native-utils 13.0.0-rc.44 → 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.
@@ -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
+ });
@@ -1,6 +1,8 @@
1
1
  /* eslint no-console: off */
2
2
 
3
- import * as R from "ramda";
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: R.always(true), keys: [] },
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 isEqualByValue = R.isEmpty(byValue)
33
- ? true
34
- : R.all((key) => R.equals(prev[key], next[key]), byValue);
34
+ for (const key of byReference) {
35
+ if (prev[key] !== next[key]) {
36
+ if (debug) console.log({ isEqualByReference: false });
35
37
 
36
- const isEqualByReference = R.isEmpty(byReference)
37
- ? true
38
- : R.all((key) => prev[key] === next[key], byReference);
38
+ return false;
39
+ }
40
+ }
39
41
 
40
- const isEqualByComparator = R.isEmpty(byComparator.keys)
41
- ? true
42
- : R.all(
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
- if (debug) {
48
- console.log({ isEqualByValue, isEqualByReference, isEqualByComparator });
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 undeclaredKeys = Array.from(allKeys).filter(
61
- (key) => !declaredKeys.has(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
- const isRestKeysEqualAllBySameValue = undeclaredKeys.every((key) =>
65
- Object.is(prev[key], next[key])
66
- );
71
+ return false;
72
+ }
73
+ }
74
+ }
67
75
 
68
- return (
69
- isEqualByValue &&
70
- isEqualByReference &&
71
- isEqualByComparator &&
72
- isRestKeysEqualAllBySameValue
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 isEqualByValue && isEqualByReference && isEqualByComparator;
88
+ return true;
77
89
  };
@@ -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
- setIsFocusable(
111
- componentCellsFocusable && (!hasNoData || !hideIfDataEmpty)
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
- }, [setIsFocusable, componentCellsFocusable, hasNoData]);
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.44",
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.44",
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",
@@ -3,5 +3,8 @@ export const useNavigation = jest.fn(() => ({
3
3
  screenData: {
4
4
  name: "someName",
5
5
  },
6
+ videoModalState: {
7
+ visible: false,
8
+ },
6
9
  currentRoute: "someRoute",
7
10
  }));
@@ -7,7 +7,7 @@ import {
7
7
  componentDidMount,
8
8
  } from "./fixtures";
9
9
 
10
- import { attachLifeCycleMethods, createReactRef, mergeChildrenProps } from "..";
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");
@@ -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