@applicaster/zapp-react-native-ui-components 15.0.0-rc.46 → 15.0.0-rc.47
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/Components/MasterCell/utils/__tests__/resolveColor.test.js +82 -3
- package/Components/MasterCell/utils/index.ts +61 -31
- package/Components/MeasurmentsPortal/MeasurementsPortal.tsx +102 -87
- package/Components/MeasurmentsPortal/__tests__/MeasurementsPortal.test.tsx +355 -0
- package/Helpers/DataSourceHelper/__tests__/itemLimitForData.test.ts +80 -0
- package/Helpers/DataSourceHelper/index.ts +10 -0
- package/package.json +6 -5
- package/Helpers/DataSourceHelper/index.js +0 -19
|
@@ -32,14 +32,16 @@ describe("resolveColor", () => {
|
|
|
32
32
|
color: "invalid_path",
|
|
33
33
|
};
|
|
34
34
|
|
|
35
|
-
expect(resolveColor(entry, style)).toEqual(
|
|
35
|
+
expect(resolveColor(entry, style)).toEqual({
|
|
36
|
+
color: undefined,
|
|
37
|
+
});
|
|
36
38
|
|
|
37
39
|
expect(loggerSpy).toHaveBeenCalledWith(
|
|
38
40
|
expect.objectContaining({
|
|
39
41
|
message: "Cannot resolve property invalid_path from the entry.",
|
|
40
42
|
data: {
|
|
43
|
+
colorFromProp: "invalid_path",
|
|
41
44
|
configurationValue: "invalid_path",
|
|
42
|
-
entry,
|
|
43
45
|
},
|
|
44
46
|
})
|
|
45
47
|
);
|
|
@@ -102,7 +104,9 @@ describe("resolveColor", () => {
|
|
|
102
104
|
color: "not.exist.path",
|
|
103
105
|
};
|
|
104
106
|
|
|
105
|
-
expect(resolveColor(entry, style)).toEqual(
|
|
107
|
+
expect(resolveColor(entry, style)).toEqual({
|
|
108
|
+
color: undefined,
|
|
109
|
+
});
|
|
106
110
|
});
|
|
107
111
|
|
|
108
112
|
it("not modify style with empty path", () => {
|
|
@@ -112,4 +116,79 @@ describe("resolveColor", () => {
|
|
|
112
116
|
|
|
113
117
|
expect(resolveColor(entry, style)).toEqual(style);
|
|
114
118
|
});
|
|
119
|
+
|
|
120
|
+
describe("memoization", () => {
|
|
121
|
+
beforeEach(() => {
|
|
122
|
+
// Clear memoization cache before each test
|
|
123
|
+
resolveColor.clear && resolveColor.clear();
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
it("hits cache with same entry and style references", () => {
|
|
127
|
+
const style = { color: "extensions.color" };
|
|
128
|
+
|
|
129
|
+
const result1 = resolveColor(entry, style);
|
|
130
|
+
const result2 = resolveColor(entry, style);
|
|
131
|
+
|
|
132
|
+
expect(result1).toBe(result2); // Same object reference
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
it("hits cache with new references but equal entry/style values", () => {
|
|
136
|
+
const entryClone = {
|
|
137
|
+
extensions: {
|
|
138
|
+
color: "red",
|
|
139
|
+
green_color: "green",
|
|
140
|
+
},
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
const style = { color: "extensions.color" };
|
|
144
|
+
const styleClone = { color: "extensions.color" };
|
|
145
|
+
|
|
146
|
+
const result1 = resolveColor(entry, style);
|
|
147
|
+
const result2 = resolveColor(entryClone, styleClone);
|
|
148
|
+
|
|
149
|
+
expect(result1).toBe(result2);
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it("misses cache when entry is new object", () => {
|
|
153
|
+
const entry2 = { extensions: { color: "blue" } }; // Same values, different object
|
|
154
|
+
const style = { color: "extensions.color" };
|
|
155
|
+
|
|
156
|
+
const result1 = resolveColor(entry, style);
|
|
157
|
+
const result2 = resolveColor(entry2, style);
|
|
158
|
+
|
|
159
|
+
expect(result1).not.toBe(result2); // Different object references
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
it("misses cache when entry property changes", () => {
|
|
163
|
+
const myEntry = {
|
|
164
|
+
extensions: {
|
|
165
|
+
color: "red",
|
|
166
|
+
green_color: "green",
|
|
167
|
+
},
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
const style = { color: "extensions.color" };
|
|
171
|
+
|
|
172
|
+
const result1 = resolveColor(myEntry, style);
|
|
173
|
+
|
|
174
|
+
myEntry.extensions.color = "blue"; // Change property
|
|
175
|
+
const result2 = resolveColor(myEntry, style);
|
|
176
|
+
|
|
177
|
+
expect(result1).toEqual({ color: "red" });
|
|
178
|
+
expect(result2).toEqual({ color: "blue" });
|
|
179
|
+
expect(result1).not.toBe(result2);
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
it("misses cache when style changes", () => {
|
|
183
|
+
const style1 = { color: "extensions.color" };
|
|
184
|
+
const style2 = { backgroundColor: "extensions.color" };
|
|
185
|
+
|
|
186
|
+
const result1 = resolveColor(entry, style1);
|
|
187
|
+
const result2 = resolveColor(entry, style2);
|
|
188
|
+
|
|
189
|
+
expect(result1).toEqual({ color: "red" });
|
|
190
|
+
expect(result2).toEqual({ backgroundColor: "red" });
|
|
191
|
+
expect(result1).not.toBe(result2);
|
|
192
|
+
});
|
|
193
|
+
});
|
|
115
194
|
});
|
|
@@ -7,9 +7,12 @@ import { useActions } from "@applicaster/zapp-react-native-utils/reactHooks/acti
|
|
|
7
7
|
import { masterCellLogger } from "../logger";
|
|
8
8
|
import { getCellState } from "../../Cell/utils";
|
|
9
9
|
import { getColorFromData } from "@applicaster/zapp-react-native-utils/cellUtils";
|
|
10
|
+
import { get } from "@applicaster/zapp-react-native-utils/utils";
|
|
10
11
|
import { isCellSelected, useBehaviorUpdate } from "./behaviorProvider";
|
|
11
12
|
import { useRoute } from "@applicaster/zapp-react-native-utils/reactHooks";
|
|
12
13
|
import { useScreenStateStore } from "@applicaster/zapp-react-native-utils/reactHooks/navigation/useScreenStateStore";
|
|
14
|
+
import memoizee from "memoizee";
|
|
15
|
+
import stringify from "fast-json-stable-stringify";
|
|
13
16
|
|
|
14
17
|
const hasElementSpecificViewType = (viewType) => (element) => {
|
|
15
18
|
if (R.isNil(element)) {
|
|
@@ -24,21 +27,21 @@ const hasElementSpecificViewType = (viewType) => (element) => {
|
|
|
24
27
|
return hasElementsSpecificViewType(viewType)(element.elements);
|
|
25
28
|
};
|
|
26
29
|
|
|
27
|
-
const logWarning =
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
30
|
+
const logWarning = (
|
|
31
|
+
colorValueFromCellStyle,
|
|
32
|
+
colorFromProp,
|
|
33
|
+
colorValueFromEntry
|
|
34
|
+
) => {
|
|
35
|
+
if (R.isNil(colorValueFromEntry)) {
|
|
36
|
+
masterCellLogger.warn({
|
|
37
|
+
message: `Cannot resolve property ${colorValueFromCellStyle} from the entry.`,
|
|
38
|
+
data: {
|
|
39
|
+
configurationValue: colorValueFromCellStyle,
|
|
40
|
+
colorFromProp,
|
|
41
|
+
},
|
|
42
|
+
});
|
|
40
43
|
}
|
|
41
|
-
|
|
44
|
+
};
|
|
42
45
|
|
|
43
46
|
export const hasElementsSpecificViewType = (viewType) => (elements) => {
|
|
44
47
|
if (R.isNil(elements) || R.isEmpty(elements)) {
|
|
@@ -48,14 +51,22 @@ export const hasElementsSpecificViewType = (viewType) => (elements) => {
|
|
|
48
51
|
return R.any(hasElementSpecificViewType(viewType))(elements);
|
|
49
52
|
};
|
|
50
53
|
|
|
51
|
-
function resolveColorForProp(entry
|
|
52
|
-
|
|
53
|
-
|
|
54
|
+
function resolveColorForProp(entry: any, colorFromProp: string | undefined) {
|
|
55
|
+
if (!colorFromProp) {
|
|
56
|
+
return undefined;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const nestedEntryValue: string | undefined = get(
|
|
60
|
+
entry,
|
|
61
|
+
colorFromProp.split(".")
|
|
62
|
+
);
|
|
54
63
|
|
|
55
64
|
const color = colorFromProp.replace(".00", "").replace(".0", ""); // https://github.com/dreamyguy/validate-color/issues/44
|
|
56
65
|
|
|
57
66
|
if (nestedEntryValue === undefined && !validateColor(color)) {
|
|
58
|
-
logWarning(colorFromProp,
|
|
67
|
+
logWarning(colorFromProp, colorFromProp, nestedEntryValue);
|
|
68
|
+
|
|
69
|
+
return undefined;
|
|
59
70
|
}
|
|
60
71
|
|
|
61
72
|
const colorValue = getColorFromData({
|
|
@@ -64,27 +75,46 @@ function resolveColorForProp(entry, style, colorProp) {
|
|
|
64
75
|
});
|
|
65
76
|
|
|
66
77
|
if (!colorValue) {
|
|
67
|
-
logWarning(
|
|
78
|
+
logWarning(colorFromProp, colorFromProp, nestedEntryValue);
|
|
68
79
|
|
|
69
|
-
return
|
|
80
|
+
return undefined;
|
|
70
81
|
}
|
|
71
82
|
|
|
72
|
-
return
|
|
83
|
+
return colorValue;
|
|
73
84
|
}
|
|
74
85
|
|
|
75
|
-
|
|
76
|
-
if (style === null || style === undefined) {
|
|
77
|
-
return style;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
// TODO can be optimized to remove 3 O(n) loops
|
|
86
|
+
const getColorKeys = memoizee((style) => {
|
|
81
87
|
const styleKeys = Object.keys(style);
|
|
82
88
|
const colorKeys = styleKeys.filter((key) => /color/i.test(key));
|
|
83
89
|
|
|
84
|
-
return colorKeys
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
90
|
+
return colorKeys;
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
export const resolveColor = memoizee(
|
|
94
|
+
(entry, style) => {
|
|
95
|
+
if (style === null || style === undefined) {
|
|
96
|
+
return style;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
return getColorKeys(style).reduce(
|
|
100
|
+
(acc, value) => {
|
|
101
|
+
if (acc[value] && typeof acc[value] === "string") {
|
|
102
|
+
const colorStyle = resolveColorForProp(entry, acc[value]);
|
|
103
|
+
|
|
104
|
+
acc[value] = colorStyle;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
return acc;
|
|
108
|
+
},
|
|
109
|
+
{ ...style }
|
|
110
|
+
);
|
|
111
|
+
},
|
|
112
|
+
{
|
|
113
|
+
normalizer: (args) => {
|
|
114
|
+
return [stringify(args[0]), stringify(args[1])].join("|");
|
|
115
|
+
},
|
|
116
|
+
}
|
|
117
|
+
);
|
|
88
118
|
|
|
89
119
|
export function isVideoPreviewEnabled({
|
|
90
120
|
enable_video_preview = false,
|
|
@@ -1,8 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import React, {
|
|
2
|
+
memo,
|
|
3
|
+
PropsWithChildren,
|
|
4
|
+
useCallback,
|
|
5
|
+
useMemo,
|
|
6
|
+
useRef,
|
|
7
|
+
useState,
|
|
8
|
+
} from "react";
|
|
4
9
|
import { View, StyleSheet } from "react-native";
|
|
5
|
-
import * as R from "ramda";
|
|
6
10
|
import { v4 } from "uuid";
|
|
7
11
|
|
|
8
12
|
type Props = {
|
|
@@ -46,93 +50,104 @@ type MeasurementPortalContextType = {
|
|
|
46
50
|
const MeasurementPortalContext =
|
|
47
51
|
React.createContext<null | MeasurementPortalContextType>(null);
|
|
48
52
|
|
|
49
|
-
const MeasurementsPortalContextProvider = (
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
height,
|
|
69
|
-
index: componentProps.current.index,
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
setMeasureComponentCallback(null);
|
|
73
|
-
setMeasuringInProgress(false);
|
|
74
|
-
onLoadFinishedIsCalled.current = false;
|
|
75
|
-
};
|
|
76
|
-
|
|
77
|
-
const setComponentProps = (props) => {
|
|
78
|
-
const handleOnLoadFinish = (...args) => {
|
|
79
|
-
const error = R.path([0, "error"], args);
|
|
80
|
-
|
|
81
|
-
if (isError(error)) {
|
|
82
|
-
finalize({ width: 0, height: 0 });
|
|
83
|
-
} else {
|
|
84
|
-
onLoadFinishedIsCalled.current = true;
|
|
85
|
-
props.onLoadFinished(...args);
|
|
86
|
-
}
|
|
87
|
-
};
|
|
88
|
-
|
|
89
|
-
componentProps.current = {
|
|
90
|
-
...props,
|
|
91
|
-
onLoadFinished: handleOnLoadFinish,
|
|
92
|
-
};
|
|
93
|
-
};
|
|
94
|
-
|
|
95
|
-
const measureComponent = React.useCallback(async (comp, props) => {
|
|
96
|
-
return new Promise((resolve) => {
|
|
97
|
-
setMeasureComponentCallback(resolve);
|
|
98
|
-
setMeasuringInProgress(true);
|
|
99
|
-
|
|
100
|
-
setComponentProps(props);
|
|
101
|
-
setComponent(comp);
|
|
102
|
-
});
|
|
103
|
-
}, []);
|
|
104
|
-
|
|
105
|
-
const onLayout = ({
|
|
106
|
-
nativeEvent: {
|
|
107
|
-
layout: { width, height },
|
|
108
|
-
},
|
|
109
|
-
}) => {
|
|
110
|
-
if (measureComponentCallback.current && onLoadFinishedIsCalled.current) {
|
|
111
|
-
finalize({
|
|
53
|
+
const MeasurementsPortalContextProvider = memo(
|
|
54
|
+
({ children }: PropsWithChildren) => {
|
|
55
|
+
const Component = useRef(View);
|
|
56
|
+
const [measuringInProgress, setMeasuringInProgress] = useState(false);
|
|
57
|
+
|
|
58
|
+
const measureComponentCallback = useRef(null);
|
|
59
|
+
const componentProps = useRef(null);
|
|
60
|
+
const onLoadFinishedIsCalled = useRef(null);
|
|
61
|
+
|
|
62
|
+
const setComponent = useCallback((comp) => {
|
|
63
|
+
Component.current = comp;
|
|
64
|
+
}, []);
|
|
65
|
+
|
|
66
|
+
const setMeasureComponentCallback = useCallback((cb) => {
|
|
67
|
+
measureComponentCallback.current = cb;
|
|
68
|
+
}, []);
|
|
69
|
+
|
|
70
|
+
const finalize = useCallback(({ width, height }) => {
|
|
71
|
+
measureComponentCallback?.current?.({
|
|
112
72
|
width,
|
|
113
73
|
height,
|
|
74
|
+
index: componentProps.current.index,
|
|
114
75
|
});
|
|
115
|
-
}
|
|
116
|
-
};
|
|
117
76
|
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
77
|
+
setMeasureComponentCallback(null);
|
|
78
|
+
setMeasuringInProgress(false);
|
|
79
|
+
onLoadFinishedIsCalled.current = false;
|
|
80
|
+
}, []);
|
|
81
|
+
|
|
82
|
+
const setComponentProps = useCallback((props) => {
|
|
83
|
+
const handleOnLoadFinish = (...args) => {
|
|
84
|
+
const error = args[0]?.error;
|
|
85
|
+
|
|
86
|
+
if (isError(error)) {
|
|
87
|
+
finalize({ width: 0, height: 0 });
|
|
88
|
+
} else {
|
|
89
|
+
onLoadFinishedIsCalled.current = true;
|
|
90
|
+
props.onLoadFinished(...args);
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
componentProps.current = {
|
|
95
|
+
...props,
|
|
96
|
+
onLoadFinished: handleOnLoadFinish,
|
|
97
|
+
};
|
|
98
|
+
}, []);
|
|
99
|
+
|
|
100
|
+
const measureComponent = useCallback(async (comp, props) => {
|
|
101
|
+
return new Promise((resolve) => {
|
|
102
|
+
setMeasureComponentCallback(resolve);
|
|
103
|
+
setMeasuringInProgress(true);
|
|
104
|
+
|
|
105
|
+
setComponentProps(props);
|
|
106
|
+
setComponent(comp);
|
|
107
|
+
});
|
|
108
|
+
}, []);
|
|
109
|
+
|
|
110
|
+
const onLayout = useCallback(
|
|
111
|
+
({
|
|
112
|
+
nativeEvent: {
|
|
113
|
+
layout: { width, height },
|
|
114
|
+
},
|
|
115
|
+
}) => {
|
|
116
|
+
if (
|
|
117
|
+
measureComponentCallback.current &&
|
|
118
|
+
onLoadFinishedIsCalled.current
|
|
119
|
+
) {
|
|
120
|
+
finalize({
|
|
121
|
+
width,
|
|
122
|
+
height,
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
},
|
|
126
|
+
[]
|
|
127
|
+
);
|
|
128
|
+
|
|
129
|
+
const contextValue = useMemo(
|
|
130
|
+
() => ({
|
|
131
|
+
measureComponent,
|
|
132
|
+
measuringInProgress,
|
|
133
|
+
}),
|
|
134
|
+
[measuringInProgress]
|
|
135
|
+
);
|
|
136
|
+
|
|
137
|
+
return (
|
|
138
|
+
<>
|
|
139
|
+
<MeasurementsPortal
|
|
140
|
+
Component={Component.current}
|
|
141
|
+
componentProps={componentProps.current}
|
|
142
|
+
onLayout={onLayout}
|
|
143
|
+
/>
|
|
144
|
+
<MeasurementPortalContext.Provider value={contextValue}>
|
|
145
|
+
{children}
|
|
146
|
+
</MeasurementPortalContext.Provider>
|
|
147
|
+
</>
|
|
148
|
+
);
|
|
149
|
+
}
|
|
150
|
+
);
|
|
136
151
|
|
|
137
152
|
export {
|
|
138
153
|
MeasurementsPortal,
|
|
@@ -0,0 +1,355 @@
|
|
|
1
|
+
import React, { useContext } from "react";
|
|
2
|
+
import { View, Text } from "react-native";
|
|
3
|
+
import { render, fireEvent, waitFor, act } from "@testing-library/react-native";
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
MeasurementsPortal,
|
|
7
|
+
MeasurementsPortalContextProvider,
|
|
8
|
+
MeasurementPortalContext,
|
|
9
|
+
} from "../MeasurementsPortal";
|
|
10
|
+
|
|
11
|
+
// Mock uuid
|
|
12
|
+
jest.mock("uuid", () => ({
|
|
13
|
+
v4: jest.fn(() => "mock-uuid-key"),
|
|
14
|
+
}));
|
|
15
|
+
|
|
16
|
+
describe("MeasurementsPortal", () => {
|
|
17
|
+
const mockOnLayout = jest.fn();
|
|
18
|
+
const MockComponent = jest.fn(() => <View testID="mock-component" />);
|
|
19
|
+
|
|
20
|
+
beforeEach(() => {
|
|
21
|
+
jest.clearAllMocks();
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it("renders with correct testID", () => {
|
|
25
|
+
const { getByTestId } = render(
|
|
26
|
+
<MeasurementsPortal Component={MockComponent} onLayout={mockOnLayout} />
|
|
27
|
+
);
|
|
28
|
+
|
|
29
|
+
expect(getByTestId("MeasurementsPortal")).toBeTruthy();
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it("renders the passed Component", () => {
|
|
33
|
+
const { getByTestId } = render(
|
|
34
|
+
<MeasurementsPortal Component={MockComponent} onLayout={mockOnLayout} />
|
|
35
|
+
);
|
|
36
|
+
|
|
37
|
+
expect(getByTestId("mock-component")).toBeTruthy();
|
|
38
|
+
expect(MockComponent).toHaveBeenCalled();
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it("passes componentProps to the Component", () => {
|
|
42
|
+
const componentProps = {
|
|
43
|
+
testProp: "test-value",
|
|
44
|
+
anotherProp: 123,
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
render(
|
|
48
|
+
<MeasurementsPortal
|
|
49
|
+
Component={MockComponent}
|
|
50
|
+
onLayout={mockOnLayout}
|
|
51
|
+
componentProps={componentProps}
|
|
52
|
+
/>
|
|
53
|
+
);
|
|
54
|
+
|
|
55
|
+
expect(MockComponent).toHaveBeenCalledWith(componentProps, {});
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it("calls onLayout when layout changes", () => {
|
|
59
|
+
const { getByTestId } = render(
|
|
60
|
+
<MeasurementsPortal Component={MockComponent} onLayout={mockOnLayout} />
|
|
61
|
+
);
|
|
62
|
+
|
|
63
|
+
const container = getByTestId("MeasurementsPortal");
|
|
64
|
+
const wrapper = container.children[0] as any;
|
|
65
|
+
|
|
66
|
+
const layoutEvent = {
|
|
67
|
+
nativeEvent: {
|
|
68
|
+
layout: { width: 100, height: 200 },
|
|
69
|
+
},
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
fireEvent(wrapper, "layout", layoutEvent);
|
|
73
|
+
|
|
74
|
+
expect(mockOnLayout).toHaveBeenCalledWith(layoutEvent);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it("applies correct container styles", () => {
|
|
78
|
+
const { getByTestId } = render(
|
|
79
|
+
<MeasurementsPortal Component={MockComponent} onLayout={mockOnLayout} />
|
|
80
|
+
);
|
|
81
|
+
|
|
82
|
+
const container = getByTestId("MeasurementsPortal");
|
|
83
|
+
|
|
84
|
+
expect(container.props.style).toEqual({
|
|
85
|
+
position: "absolute",
|
|
86
|
+
opacity: 0,
|
|
87
|
+
top: 0,
|
|
88
|
+
left: 0,
|
|
89
|
+
right: 0,
|
|
90
|
+
bottom: 0,
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
it("renders wrapper with onLayout", () => {
|
|
95
|
+
const { getByTestId } = render(
|
|
96
|
+
<MeasurementsPortal Component={MockComponent} onLayout={mockOnLayout} />
|
|
97
|
+
);
|
|
98
|
+
|
|
99
|
+
const container = getByTestId("MeasurementsPortal");
|
|
100
|
+
const wrapper = container.children[0] as any;
|
|
101
|
+
|
|
102
|
+
expect(wrapper.props.onLayout).toBe(mockOnLayout);
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it("works with different Component types", () => {
|
|
106
|
+
const CustomComponent = ({ title }: { title: string }) => (
|
|
107
|
+
<Text testID="custom-text">{title}</Text>
|
|
108
|
+
);
|
|
109
|
+
|
|
110
|
+
const props = { title: "Test Title" };
|
|
111
|
+
|
|
112
|
+
const { getByTestId, getByText } = render(
|
|
113
|
+
<MeasurementsPortal
|
|
114
|
+
Component={CustomComponent}
|
|
115
|
+
onLayout={mockOnLayout}
|
|
116
|
+
componentProps={props}
|
|
117
|
+
/>
|
|
118
|
+
);
|
|
119
|
+
|
|
120
|
+
expect(getByTestId("custom-text")).toBeTruthy();
|
|
121
|
+
expect(getByText("Test Title")).toBeTruthy();
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it("handles component without componentProps", () => {
|
|
125
|
+
render(
|
|
126
|
+
<MeasurementsPortal Component={MockComponent} onLayout={mockOnLayout} />
|
|
127
|
+
);
|
|
128
|
+
|
|
129
|
+
expect(MockComponent).toHaveBeenCalledWith({}, {});
|
|
130
|
+
});
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
describe("MeasurementsPortalContextProvider", () => {
|
|
134
|
+
const TestConsumer = () => {
|
|
135
|
+
const context = useContext(MeasurementPortalContext);
|
|
136
|
+
|
|
137
|
+
return (
|
|
138
|
+
<View testID="test-consumer">
|
|
139
|
+
<Text testID="measuring-status">
|
|
140
|
+
{context?.measuringInProgress ? "measuring" : "idle"}
|
|
141
|
+
</Text>
|
|
142
|
+
</View>
|
|
143
|
+
);
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
it("provides context to children", () => {
|
|
147
|
+
const { getByTestId } = render(
|
|
148
|
+
<MeasurementsPortalContextProvider>
|
|
149
|
+
<TestConsumer />
|
|
150
|
+
</MeasurementsPortalContextProvider>
|
|
151
|
+
);
|
|
152
|
+
|
|
153
|
+
expect(getByTestId("test-consumer")).toBeTruthy();
|
|
154
|
+
expect(getByTestId("measuring-status")).toBeTruthy();
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
it("initially sets measuringInProgress to false", () => {
|
|
158
|
+
const { getByText } = render(
|
|
159
|
+
<MeasurementsPortalContextProvider>
|
|
160
|
+
<TestConsumer />
|
|
161
|
+
</MeasurementsPortalContextProvider>
|
|
162
|
+
);
|
|
163
|
+
|
|
164
|
+
expect(getByText("idle")).toBeTruthy();
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
it("renders MeasurementsPortal with default View component", () => {
|
|
168
|
+
const { getByTestId } = render(
|
|
169
|
+
<MeasurementsPortalContextProvider>
|
|
170
|
+
<View />
|
|
171
|
+
</MeasurementsPortalContextProvider>
|
|
172
|
+
);
|
|
173
|
+
|
|
174
|
+
expect(getByTestId("MeasurementsPortal")).toBeTruthy();
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
describe("measureComponent function", () => {
|
|
178
|
+
const TestMeasureComponent = () => {
|
|
179
|
+
const context = useContext(MeasurementPortalContext);
|
|
180
|
+
const [result, setResult] = React.useState<any>(null);
|
|
181
|
+
|
|
182
|
+
const handleMeasure = async () => {
|
|
183
|
+
if (context?.measureComponent) {
|
|
184
|
+
const measurement = await context.measureComponent(View, {
|
|
185
|
+
index: 0,
|
|
186
|
+
onLoadFinished: () => {},
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
setResult(measurement);
|
|
190
|
+
}
|
|
191
|
+
};
|
|
192
|
+
|
|
193
|
+
return (
|
|
194
|
+
<View testID="measure-component">
|
|
195
|
+
<Text testID="measuring-status">
|
|
196
|
+
{context?.measuringInProgress ? "measuring" : "idle"}
|
|
197
|
+
</Text>
|
|
198
|
+
<Text testID="measure-button" onPress={handleMeasure}>
|
|
199
|
+
Measure
|
|
200
|
+
</Text>
|
|
201
|
+
{result ? (
|
|
202
|
+
<Text testID="result">
|
|
203
|
+
{result.width}x{result.height}
|
|
204
|
+
</Text>
|
|
205
|
+
) : null}
|
|
206
|
+
</View>
|
|
207
|
+
);
|
|
208
|
+
};
|
|
209
|
+
|
|
210
|
+
it("sets measuringInProgress to true when measureComponent is called", async () => {
|
|
211
|
+
const { getByTestId, getByText } = render(
|
|
212
|
+
<MeasurementsPortalContextProvider>
|
|
213
|
+
<TestMeasureComponent />
|
|
214
|
+
</MeasurementsPortalContextProvider>
|
|
215
|
+
);
|
|
216
|
+
|
|
217
|
+
expect(getByText("idle")).toBeTruthy();
|
|
218
|
+
|
|
219
|
+
await act(async () => {
|
|
220
|
+
fireEvent.press(getByTestId("measure-button"));
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
expect(getByText("measuring")).toBeTruthy();
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
it("can measure a component through the context", async () => {
|
|
227
|
+
const TestMeasureComponent = () => {
|
|
228
|
+
const context = useContext(MeasurementPortalContext);
|
|
229
|
+
const [measured, setMeasured] = React.useState(false);
|
|
230
|
+
|
|
231
|
+
React.useEffect(() => {
|
|
232
|
+
if (context?.measureComponent) {
|
|
233
|
+
context
|
|
234
|
+
.measureComponent(View, {
|
|
235
|
+
index: 5,
|
|
236
|
+
onLoadFinished: () => {},
|
|
237
|
+
})
|
|
238
|
+
.then(() => {
|
|
239
|
+
setMeasured(true);
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
}, [context]);
|
|
243
|
+
|
|
244
|
+
return (
|
|
245
|
+
<View testID="measure-component">
|
|
246
|
+
<Text testID="measure-status">
|
|
247
|
+
{measured ? "measured" : "not-measured"}
|
|
248
|
+
</Text>
|
|
249
|
+
</View>
|
|
250
|
+
);
|
|
251
|
+
};
|
|
252
|
+
|
|
253
|
+
const { getByText } = render(
|
|
254
|
+
<MeasurementsPortalContextProvider>
|
|
255
|
+
<TestMeasureComponent />
|
|
256
|
+
</MeasurementsPortalContextProvider>
|
|
257
|
+
);
|
|
258
|
+
|
|
259
|
+
// Initially should be not measured
|
|
260
|
+
expect(getByText("not-measured")).toBeTruthy();
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
it("handles error in onLoadFinished", async () => {
|
|
264
|
+
const ErrorComponent = ({ onLoadFinished }: any) => {
|
|
265
|
+
React.useEffect(() => {
|
|
266
|
+
// Simulate error
|
|
267
|
+
setTimeout(
|
|
268
|
+
() => onLoadFinished({ error: new Error("Test error") }),
|
|
269
|
+
10
|
|
270
|
+
);
|
|
271
|
+
}, [onLoadFinished]);
|
|
272
|
+
|
|
273
|
+
return <View testID="error-component" />;
|
|
274
|
+
};
|
|
275
|
+
|
|
276
|
+
const TestErrorFlow = () => {
|
|
277
|
+
const context = useContext(MeasurementPortalContext);
|
|
278
|
+
const [result, setResult] = React.useState<any>(null);
|
|
279
|
+
|
|
280
|
+
const handleMeasure = React.useCallback(async () => {
|
|
281
|
+
if (context?.measureComponent) {
|
|
282
|
+
const measurement = await context.measureComponent(ErrorComponent, {
|
|
283
|
+
index: 1,
|
|
284
|
+
onLoadFinished: () => {},
|
|
285
|
+
});
|
|
286
|
+
|
|
287
|
+
setResult(measurement);
|
|
288
|
+
}
|
|
289
|
+
}, [context]);
|
|
290
|
+
|
|
291
|
+
React.useEffect(() => {
|
|
292
|
+
handleMeasure();
|
|
293
|
+
}, [handleMeasure]);
|
|
294
|
+
|
|
295
|
+
return (
|
|
296
|
+
<View testID="error-flow">
|
|
297
|
+
{result ? (
|
|
298
|
+
<Text testID="error-result">
|
|
299
|
+
{result.width}x{result.height}
|
|
300
|
+
</Text>
|
|
301
|
+
) : null}
|
|
302
|
+
</View>
|
|
303
|
+
);
|
|
304
|
+
};
|
|
305
|
+
|
|
306
|
+
const { queryByText } = render(
|
|
307
|
+
<MeasurementsPortalContextProvider>
|
|
308
|
+
<TestErrorFlow />
|
|
309
|
+
</MeasurementsPortalContextProvider>
|
|
310
|
+
);
|
|
311
|
+
|
|
312
|
+
// Wait for error handling
|
|
313
|
+
await waitFor(() => {
|
|
314
|
+
expect(queryByText("0x0")).toBeTruthy();
|
|
315
|
+
});
|
|
316
|
+
});
|
|
317
|
+
});
|
|
318
|
+
});
|
|
319
|
+
|
|
320
|
+
describe("MeasurementPortalContext", () => {
|
|
321
|
+
it("returns null when used outside of provider", () => {
|
|
322
|
+
const TestComponent = () => {
|
|
323
|
+
const context = useContext(MeasurementPortalContext);
|
|
324
|
+
|
|
325
|
+
return (
|
|
326
|
+
<Text testID="context-value">
|
|
327
|
+
{context ? "has-context" : "no-context"}
|
|
328
|
+
</Text>
|
|
329
|
+
);
|
|
330
|
+
};
|
|
331
|
+
|
|
332
|
+
const { getByText } = render(<TestComponent />);
|
|
333
|
+
expect(getByText("no-context")).toBeTruthy();
|
|
334
|
+
});
|
|
335
|
+
|
|
336
|
+
it("returns context value when used inside provider", () => {
|
|
337
|
+
const TestComponent = () => {
|
|
338
|
+
const context = useContext(MeasurementPortalContext);
|
|
339
|
+
|
|
340
|
+
return (
|
|
341
|
+
<Text testID="context-value">
|
|
342
|
+
{context ? "has-context" : "no-context"}
|
|
343
|
+
</Text>
|
|
344
|
+
);
|
|
345
|
+
};
|
|
346
|
+
|
|
347
|
+
const { getByText } = render(
|
|
348
|
+
<MeasurementsPortalContextProvider>
|
|
349
|
+
<TestComponent />
|
|
350
|
+
</MeasurementsPortalContextProvider>
|
|
351
|
+
);
|
|
352
|
+
|
|
353
|
+
expect(getByText("has-context")).toBeTruthy();
|
|
354
|
+
});
|
|
355
|
+
});
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { itemLimitForData } from "..";
|
|
2
|
+
|
|
3
|
+
describe("itemLimitForData (no mocks)", () => {
|
|
4
|
+
test("returns full array when item_limit is undefined (default Infinity)", () => {
|
|
5
|
+
// @ts-ignore
|
|
6
|
+
const result = itemLimitForData([1, 2, 3], {
|
|
7
|
+
rules: { item_limit: undefined },
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
expect(result).toEqual([1, 2, 3]);
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
test("enforces positive numeric item_limit", () => {
|
|
14
|
+
// @ts-ignore
|
|
15
|
+
const result = itemLimitForData([1, 2, 3, 4], {
|
|
16
|
+
rules: { item_limit: 2 },
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
expect(result).toEqual([1, 2]);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
test("returns empty array when entry is empty", () => {
|
|
23
|
+
const result = itemLimitForData([], {
|
|
24
|
+
rules: { item_limit: 3 },
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
expect(result).toEqual([]);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
test("defaults entry to empty array when it is undefined", () => {
|
|
31
|
+
const result = itemLimitForData(undefined, {
|
|
32
|
+
rules: { item_limit: 5 },
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
expect(result).toEqual([]);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
test("handles missing component", () => {
|
|
39
|
+
// @ts-ignore
|
|
40
|
+
const result = itemLimitForData([10, 20, 30], undefined);
|
|
41
|
+
// missing component → item_limit = undefined → fallback to Infinity
|
|
42
|
+
expect(result).toEqual([10, 20, 30]);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
test("handles missing rules object", () => {
|
|
46
|
+
// @ts-ignore
|
|
47
|
+
const result = itemLimitForData([10, 20, 30], {});
|
|
48
|
+
|
|
49
|
+
expect(result).toEqual([10, 20, 30]);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
test("non-positive item_limit should fall back to Infinity", () => {
|
|
53
|
+
// @ts-ignore
|
|
54
|
+
const result = itemLimitForData([1, 2, 3], {
|
|
55
|
+
rules: { item_limit: -10 },
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
// -10 → invalid → fallback to Infinity → no limit
|
|
59
|
+
expect(result).toEqual([1, 2, 3]);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
test("zero item_limit should fall back to Infinity", () => {
|
|
63
|
+
// @ts-ignore
|
|
64
|
+
const result = itemLimitForData([1, 2, 3], {
|
|
65
|
+
rules: { item_limit: 0 },
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
// 0 → invalid → fallback to Infinity
|
|
69
|
+
expect(result).toEqual([1, 2, 3]);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
test("NaN item_limit should fall back to Infinity", () => {
|
|
73
|
+
// @ts-ignore
|
|
74
|
+
const result = itemLimitForData([1, 2, 3], {
|
|
75
|
+
rules: { item_limit: NaN },
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
expect(result).toEqual([1, 2, 3]);
|
|
79
|
+
});
|
|
80
|
+
});
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { toPositiveNumberWithDefault } from "@applicaster/zapp-react-native-utils/numberUtils";
|
|
2
|
+
|
|
3
|
+
export function itemLimitForData(entry, component) {
|
|
4
|
+
const itemLimit = toPositiveNumberWithDefault(
|
|
5
|
+
Infinity,
|
|
6
|
+
component?.rules?.item_limit
|
|
7
|
+
);
|
|
8
|
+
|
|
9
|
+
return (entry || []).slice(0, itemLimit);
|
|
10
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@applicaster/zapp-react-native-ui-components",
|
|
3
|
-
"version": "15.0.0-rc.
|
|
3
|
+
"version": "15.0.0-rc.47",
|
|
4
4
|
"description": "Applicaster Zapp React Native ui components for the Quick Brick App",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"types": "index.d.ts",
|
|
@@ -28,10 +28,11 @@
|
|
|
28
28
|
},
|
|
29
29
|
"homepage": "https://github.com/applicaster/quickbrick#readme",
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@applicaster/applicaster-types": "15.0.0-rc.
|
|
32
|
-
"@applicaster/zapp-react-native-bridge": "15.0.0-rc.
|
|
33
|
-
"@applicaster/zapp-react-native-redux": "15.0.0-rc.
|
|
34
|
-
"@applicaster/zapp-react-native-utils": "15.0.0-rc.
|
|
31
|
+
"@applicaster/applicaster-types": "15.0.0-rc.47",
|
|
32
|
+
"@applicaster/zapp-react-native-bridge": "15.0.0-rc.47",
|
|
33
|
+
"@applicaster/zapp-react-native-redux": "15.0.0-rc.47",
|
|
34
|
+
"@applicaster/zapp-react-native-utils": "15.0.0-rc.47",
|
|
35
|
+
"fast-json-stable-stringify": "^2.1.0",
|
|
35
36
|
"promise": "^8.3.0",
|
|
36
37
|
"url": "^0.11.0",
|
|
37
38
|
"uuid": "^3.3.2"
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import * as R from "ramda";
|
|
2
|
-
|
|
3
|
-
export function itemLimitForData(entry = [], component) {
|
|
4
|
-
const itemLimitValue = Number(R.path(["rules", "item_limit"], component));
|
|
5
|
-
|
|
6
|
-
const itemLimit =
|
|
7
|
-
itemLimitValue && itemLimitValue > 0
|
|
8
|
-
? itemLimitValue
|
|
9
|
-
: Number.MAX_SAFE_INTEGER;
|
|
10
|
-
|
|
11
|
-
const isInRange = (min, max) => R.both(R.gte(R.__, min), R.lt(R.__, max));
|
|
12
|
-
|
|
13
|
-
const entryShouldBeSliced = (entry) =>
|
|
14
|
-
isInRange(0, R.length(entry))(itemLimit);
|
|
15
|
-
|
|
16
|
-
const sliceEntriesUpToItemLimit = R.slice(0, itemLimit);
|
|
17
|
-
|
|
18
|
-
return R.when(entryShouldBeSliced, sliceEntriesUpToItemLimit)(entry);
|
|
19
|
-
}
|