@applicaster/zapp-react-native-utils 15.1.15-alpha.4236143002 → 15.1.15-rc.0
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/actionsExecutor/actions/__tests__/showToast.test.ts +2 -2
- package/actionsExecutor/actions/showToast.ts +2 -2
- package/manifestUtils/defaultManifestConfigurations/generalContent.js +6 -0
- package/modalState/__tests__/useModalStoreState.test.ts +142 -0
- package/modalState/store.ts +9 -2
- package/modalState/useBottomSheetContent.ts +17 -12
- package/package.json +2 -2
- package/reactHooks/navigation/__tests__/index.test.tsx +124 -1
- package/reactHooks/navigation/useRoute.ts +8 -4
|
@@ -31,7 +31,7 @@ describe("showToastAction", () => {
|
|
|
31
31
|
message: "Added to queue",
|
|
32
32
|
extraMessage: "Extra info",
|
|
33
33
|
style: undefined,
|
|
34
|
-
timeout:
|
|
34
|
+
timeout: 5000,
|
|
35
35
|
source: "confirmation",
|
|
36
36
|
},
|
|
37
37
|
]);
|
|
@@ -78,7 +78,7 @@ describe("showToastAction", () => {
|
|
|
78
78
|
message: undefined,
|
|
79
79
|
extraMessage: undefined,
|
|
80
80
|
style: undefined,
|
|
81
|
-
timeout:
|
|
81
|
+
timeout: 5000,
|
|
82
82
|
source: "confirmation",
|
|
83
83
|
},
|
|
84
84
|
]);
|
|
@@ -5,7 +5,7 @@ import { postEvent } from "../../reactHooks/useSubscriberFor";
|
|
|
5
5
|
|
|
6
6
|
export const CONFIRMATION_TOAST_SOURCE = "confirmation";
|
|
7
7
|
|
|
8
|
-
export const CONFIRMATION_TOAST_DEFAULT_TIMEOUT =
|
|
8
|
+
export const CONFIRMATION_TOAST_DEFAULT_TIMEOUT = 5000;
|
|
9
9
|
|
|
10
10
|
/**
|
|
11
11
|
* Shows a mobile confirmation toast with Theme defaults from
|
|
@@ -13,7 +13,7 @@ export const CONFIRMATION_TOAST_DEFAULT_TIMEOUT = 1000;
|
|
|
13
13
|
*
|
|
14
14
|
* This action is confirmation-only by design: it always stamps
|
|
15
15
|
* `source: "confirmation"` (so Theme confirmation styles apply) and defaults
|
|
16
|
-
* `timeout` to
|
|
16
|
+
* `timeout` to 5000 ms. Non-confirmation toasts (e.g. Offline Experience)
|
|
17
17
|
* must post on the shared `"showToast"` event bus directly and omit `source`.
|
|
18
18
|
* Callers may override individual style fields; Theme supplies the rest.
|
|
19
19
|
*
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import { act, renderHook } from "@testing-library/react-native";
|
|
2
|
+
|
|
3
|
+
import { modalStore, useModalStoreState } from "../";
|
|
4
|
+
|
|
5
|
+
const item = { id: "river-general", name: "Test Modal" };
|
|
6
|
+
const props = { entry: { id: "modal-entry-1" } };
|
|
7
|
+
|
|
8
|
+
const openModal = () =>
|
|
9
|
+
act(() => {
|
|
10
|
+
modalStore.getState().openModal({ item, props } as any);
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
// Counts every render of the hook so the tests can assert on subscription
|
|
14
|
+
// behaviour, not just on the returned value.
|
|
15
|
+
const renderModalStoreState = (skipSubscription?: boolean) => {
|
|
16
|
+
const renders = { count: 0 };
|
|
17
|
+
|
|
18
|
+
const view = renderHook(() => {
|
|
19
|
+
renders.count += 1;
|
|
20
|
+
|
|
21
|
+
return skipSubscription === undefined
|
|
22
|
+
? useModalStoreState()
|
|
23
|
+
: useModalStoreState(skipSubscription);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
return { ...view, renders };
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
describe("useModalStoreState", () => {
|
|
30
|
+
afterEach(() => {
|
|
31
|
+
act(() => {
|
|
32
|
+
modalStore.getState().dismissModal();
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
describe("when subscribed (default)", () => {
|
|
37
|
+
it("returns the live modal state when called without arguments", () => {
|
|
38
|
+
const { result } = renderModalStoreState();
|
|
39
|
+
|
|
40
|
+
expect(result.current.visible).toBe(false);
|
|
41
|
+
|
|
42
|
+
openModal();
|
|
43
|
+
|
|
44
|
+
expect(result.current.visible).toBe(true);
|
|
45
|
+
expect(result.current.screen).toBe(item);
|
|
46
|
+
expect(result.current.props).toBe(props);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it("re-renders the consumer when the modal state changes", () => {
|
|
50
|
+
const { renders } = renderModalStoreState(false);
|
|
51
|
+
|
|
52
|
+
const rendersBeforeOpen = renders.count;
|
|
53
|
+
|
|
54
|
+
openModal();
|
|
55
|
+
|
|
56
|
+
expect(renders.count).toBeGreaterThan(rendersBeforeOpen);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
// Counterpart to "returns a stable reference across store updates": an open
|
|
60
|
+
// modal is the one point where a live subscription and a skipped one hold
|
|
61
|
+
// different references, which is what makes that assertion meaningful.
|
|
62
|
+
it("returns a new reference while the modal is open", () => {
|
|
63
|
+
const { result } = renderModalStoreState(false);
|
|
64
|
+
|
|
65
|
+
const firstValue = result.current;
|
|
66
|
+
|
|
67
|
+
openModal();
|
|
68
|
+
|
|
69
|
+
expect(result.current).not.toBe(firstValue);
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
describe("when subscription is skipped", () => {
|
|
74
|
+
it("reports an empty modal state even while a modal is open", () => {
|
|
75
|
+
openModal();
|
|
76
|
+
|
|
77
|
+
expect(modalStore.getState().modalState.visible).toBe(true);
|
|
78
|
+
|
|
79
|
+
const { result } = renderModalStoreState(true);
|
|
80
|
+
|
|
81
|
+
expect(result.current).toMatchObject({
|
|
82
|
+
visible: false,
|
|
83
|
+
screen: null,
|
|
84
|
+
options: {},
|
|
85
|
+
props: {},
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it("does not re-render the consumer when the modal state changes", () => {
|
|
90
|
+
const { renders } = renderModalStoreState(true);
|
|
91
|
+
|
|
92
|
+
const rendersBeforeOpen = renders.count;
|
|
93
|
+
|
|
94
|
+
openModal();
|
|
95
|
+
|
|
96
|
+
expect(renders.count).toBe(rendersBeforeOpen);
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
// The skipped branch has to hand back a module-level constant. Returning a
|
|
100
|
+
// fresh object would fail zustand's Object.is check and re-render on every
|
|
101
|
+
// store notification - the opposite of what skipping is for.
|
|
102
|
+
it("returns a stable reference across store updates", () => {
|
|
103
|
+
const { result } = renderModalStoreState(true);
|
|
104
|
+
|
|
105
|
+
const firstValue = result.current;
|
|
106
|
+
|
|
107
|
+
openModal();
|
|
108
|
+
|
|
109
|
+
// Asserted while the modal is open: dismissing resets the store to the
|
|
110
|
+
// very same initialModalState object, so an end-to-end round trip would
|
|
111
|
+
// hold this identity even for a live subscription.
|
|
112
|
+
expect(result.current).toBe(firstValue);
|
|
113
|
+
|
|
114
|
+
act(() => {
|
|
115
|
+
modalStore.getState().dismissModal();
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
expect(result.current).toBe(firstValue);
|
|
119
|
+
});
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it("picks up the current modal state when it starts subscribing", () => {
|
|
123
|
+
let skipSubscription = true;
|
|
124
|
+
|
|
125
|
+
const { result, rerender } = renderHook(() =>
|
|
126
|
+
useModalStoreState(skipSubscription)
|
|
127
|
+
);
|
|
128
|
+
|
|
129
|
+
openModal();
|
|
130
|
+
|
|
131
|
+
expect(result.current.visible).toBe(false);
|
|
132
|
+
|
|
133
|
+
skipSubscription = false;
|
|
134
|
+
|
|
135
|
+
act(() => {
|
|
136
|
+
rerender(undefined);
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
expect(result.current.visible).toBe(true);
|
|
140
|
+
expect(result.current.screen).toBe(item);
|
|
141
|
+
});
|
|
142
|
+
});
|
package/modalState/store.ts
CHANGED
|
@@ -98,5 +98,12 @@ export const openBottomSheetModal = ({
|
|
|
98
98
|
});
|
|
99
99
|
};
|
|
100
100
|
|
|
101
|
-
|
|
102
|
-
|
|
101
|
+
/**
|
|
102
|
+
* Pass `skipSubscription` from callers that render outside of a modal: the
|
|
103
|
+
* selector then returns the stable `initialModalState` reference, so the
|
|
104
|
+
* component is never re-rendered by modal state it does not read.
|
|
105
|
+
*/
|
|
106
|
+
export const useModalStoreState = (skipSubscription = false): ModalState =>
|
|
107
|
+
useModalStore<ModalState>((state) =>
|
|
108
|
+
skipSubscription ? initialModalState : state.modalState
|
|
109
|
+
);
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import React, { useEffect, useState } from "react";
|
|
1
|
+
import React, { useCallback, useEffect, useState } from "react";
|
|
2
2
|
import { toBooleanWithDefaultFalse } from "@applicaster/zapp-react-native-utils/booleanUtils";
|
|
3
3
|
|
|
4
4
|
import { MenuItem, modalOrchestrator } from "./ModalOrchestrator";
|
|
@@ -45,7 +45,9 @@ export const useBottomSheetContent = (props: Props): Return => {
|
|
|
45
45
|
// Subscribe to the viewModel state changes
|
|
46
46
|
useEffect(() => {
|
|
47
47
|
return viewModel?.subscribe((nextState) => {
|
|
48
|
-
setState(
|
|
48
|
+
setState((state) => {
|
|
49
|
+
return state === nextState ? state : nextState;
|
|
50
|
+
});
|
|
49
51
|
});
|
|
50
52
|
}, [viewModel]);
|
|
51
53
|
|
|
@@ -65,16 +67,19 @@ export const useBottomSheetContent = (props: Props): Return => {
|
|
|
65
67
|
* - If the item was a leaf action (not a submenu), triggers viewModel.refetch()
|
|
66
68
|
* so that checkmarks (isSelected) reflect the updated server state.
|
|
67
69
|
*/
|
|
68
|
-
const handleItemPress =
|
|
69
|
-
|
|
70
|
-
|
|
70
|
+
const handleItemPress = useCallback(
|
|
71
|
+
async (item: MenuItem) => {
|
|
72
|
+
const actions = getMenuItemActions(item);
|
|
73
|
+
const isSubMenu = actions.some(isPresentSubMenuAction);
|
|
71
74
|
|
|
72
|
-
|
|
75
|
+
await onPress(item);
|
|
73
76
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
77
|
+
if (!isSubMenu) {
|
|
78
|
+
await viewModel?.refetch();
|
|
79
|
+
}
|
|
80
|
+
},
|
|
81
|
+
[onPress, viewModel]
|
|
82
|
+
);
|
|
78
83
|
|
|
79
84
|
const goBackOrClose = React.useCallback(() => {
|
|
80
85
|
if (modalOrchestrator.canGoBack) {
|
|
@@ -82,7 +87,7 @@ export const useBottomSheetContent = (props: Props): Return => {
|
|
|
82
87
|
} else {
|
|
83
88
|
modalOrchestrator.close();
|
|
84
89
|
}
|
|
85
|
-
}, [
|
|
90
|
+
}, []);
|
|
86
91
|
|
|
87
92
|
return {
|
|
88
93
|
items: viewModel?.editableCollection ? editableItems : state?.items,
|
|
@@ -94,7 +99,7 @@ export const useBottomSheetContent = (props: Props): Return => {
|
|
|
94
99
|
canGoBack: modalOrchestrator.canGoBack,
|
|
95
100
|
handleItemPress,
|
|
96
101
|
goBackOrClose,
|
|
97
|
-
close: () => modalOrchestrator.close(),
|
|
102
|
+
close: useCallback(() => modalOrchestrator.close(), []),
|
|
98
103
|
role: state?.role,
|
|
99
104
|
behavior: state?.behavior,
|
|
100
105
|
dynamicCollection: state?.dynamicCollection,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@applicaster/zapp-react-native-utils",
|
|
3
|
-
"version": "15.1.15-
|
|
3
|
+
"version": "15.1.15-rc.0",
|
|
4
4
|
"description": "Applicaster Zapp React Native utilities package",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"types": "index.d.ts",
|
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
},
|
|
28
28
|
"homepage": "https://github.com/applicaster/quickbrick#readme",
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@applicaster/applicaster-types": "15.1.15-
|
|
30
|
+
"@applicaster/applicaster-types": "15.1.15-rc.0",
|
|
31
31
|
"buffer": "^5.2.1",
|
|
32
32
|
"camelize": "^1.0.0",
|
|
33
33
|
"dayjs": "^1.11.10",
|
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import React from "react";
|
|
2
2
|
import { PathnameContext } from "@applicaster/zapp-react-native-ui-components/Contexts/PathnameContext";
|
|
3
|
-
import { renderHook } from "@testing-library/react-
|
|
3
|
+
import { act, render, renderHook } from "@testing-library/react-native";
|
|
4
4
|
import { isNavBarVisible, useRoute } from "../";
|
|
5
5
|
import { Provider } from "react-redux";
|
|
6
6
|
import configureMockStore from "redux-mock-store";
|
|
7
7
|
import { NavigationContext } from "@applicaster/zapp-react-native-ui-components/Contexts/NavigationContext";
|
|
8
8
|
import { ROUTE_TYPES } from "@applicaster/zapp-react-native-utils/navigationUtils/routeTypes";
|
|
9
9
|
import { ScreenDataContext } from "@applicaster/zapp-react-native-ui-components/Contexts/ScreenDataContext";
|
|
10
|
+
import { modalStore } from "@applicaster/zapp-react-native-utils/modalState";
|
|
10
11
|
|
|
11
12
|
const plugins = [];
|
|
12
13
|
|
|
@@ -45,6 +46,9 @@ const videoItem = {
|
|
|
45
46
|
type: { value: "video" },
|
|
46
47
|
};
|
|
47
48
|
|
|
49
|
+
const modalEntry = { id: "modal-entry-1" };
|
|
50
|
+
const modalPathname = `${ROUTE_TYPES.MODAL}/${rivers["river-general"].id}`;
|
|
51
|
+
|
|
48
52
|
const mockGetStackForPathname = jest.fn().mockReturnValue(homeStack);
|
|
49
53
|
|
|
50
54
|
const mainStackNavigator = {
|
|
@@ -168,6 +172,45 @@ const hookModalWrapper = ({ children }) => (
|
|
|
168
172
|
</Provider>
|
|
169
173
|
);
|
|
170
174
|
|
|
175
|
+
// Reports every render of useRoute so the tests below can assert on route
|
|
176
|
+
// transitions and on subscription behaviour, not just on a one-shot result.
|
|
177
|
+
function RouteProbe({ onRender }: { onRender: (route: any) => void }) {
|
|
178
|
+
onRender(useRoute());
|
|
179
|
+
|
|
180
|
+
return null;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
const openRiverModal = () =>
|
|
184
|
+
act(() => {
|
|
185
|
+
modalStore.getState().openModal({
|
|
186
|
+
item: rivers["river-general"] as any,
|
|
187
|
+
props: { entry: modalEntry },
|
|
188
|
+
});
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
const renderRouteProbe = (pathname: string, onRender: (route: any) => void) => {
|
|
192
|
+
const tree = (currentPathname: string) => (
|
|
193
|
+
<Provider store={store}>
|
|
194
|
+
<ScreenDataContext.Provider value={homeStack.state}>
|
|
195
|
+
<NavigationContext.Provider value={videoModalNavigator}>
|
|
196
|
+
<PathnameContext.Provider value={currentPathname}>
|
|
197
|
+
<RouteProbe onRender={onRender} />
|
|
198
|
+
</PathnameContext.Provider>
|
|
199
|
+
</NavigationContext.Provider>
|
|
200
|
+
</ScreenDataContext.Provider>
|
|
201
|
+
</Provider>
|
|
202
|
+
);
|
|
203
|
+
|
|
204
|
+
const { rerender } = render(tree(pathname));
|
|
205
|
+
|
|
206
|
+
return {
|
|
207
|
+
navigateTo: (nextPathname: string) =>
|
|
208
|
+
act(() => {
|
|
209
|
+
rerender(tree(nextPathname));
|
|
210
|
+
}),
|
|
211
|
+
};
|
|
212
|
+
};
|
|
213
|
+
|
|
171
214
|
describe("navigation", () => {
|
|
172
215
|
describe("useRoute", () => {
|
|
173
216
|
describe("Main stack components", () => {
|
|
@@ -233,6 +276,86 @@ describe("navigation", () => {
|
|
|
233
276
|
);
|
|
234
277
|
});
|
|
235
278
|
});
|
|
279
|
+
|
|
280
|
+
describe("store subscriptions", () => {
|
|
281
|
+
afterEach(() => {
|
|
282
|
+
act(() => {
|
|
283
|
+
modalStore.getState().dismissModal();
|
|
284
|
+
});
|
|
285
|
+
});
|
|
286
|
+
|
|
287
|
+
// Every hook in useRoute must run on every render. When the modal and
|
|
288
|
+
// hook-modal subscriptions lived inside the route branches, walking a
|
|
289
|
+
// single mounted component through the route types changed the hook
|
|
290
|
+
// count and React threw "Rendered more hooks than during the previous
|
|
291
|
+
// render".
|
|
292
|
+
it("keeps a stable hook order when the route type changes", () => {
|
|
293
|
+
let route: any;
|
|
294
|
+
|
|
295
|
+
const { navigateTo } = renderRouteProbe(homeStack.route, (current) => {
|
|
296
|
+
route = current;
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
expect(route.screenData.id).toEqual(entry.id);
|
|
300
|
+
expect(route.screenData.targetScreen.id).toEqual(screen.id);
|
|
301
|
+
|
|
302
|
+
openRiverModal();
|
|
303
|
+
navigateTo(modalPathname);
|
|
304
|
+
|
|
305
|
+
expect(route.pathname).toEqual(modalPathname);
|
|
306
|
+
expect(route.screenData.id).toEqual(rivers["river-general"].id);
|
|
307
|
+
|
|
308
|
+
navigateTo(hooksModalPathname);
|
|
309
|
+
|
|
310
|
+
expect(route.pathname).toEqual(hooksModalPathname);
|
|
311
|
+
|
|
312
|
+
expect(route.screenData.id).toEqual(
|
|
313
|
+
hookModalContextState.state.screenData.payload.id
|
|
314
|
+
);
|
|
315
|
+
|
|
316
|
+
navigateTo(videoModalPathname);
|
|
317
|
+
|
|
318
|
+
expect(route.pathname).toEqual(videoModalPathname);
|
|
319
|
+
expect(route.screenData.id).toEqual(videoItem.id);
|
|
320
|
+
|
|
321
|
+
expect(route.screenData.targetScreen.id).toEqual(
|
|
322
|
+
rivers["river-player"].id
|
|
323
|
+
);
|
|
324
|
+
|
|
325
|
+
navigateTo(homeStack.route);
|
|
326
|
+
|
|
327
|
+
expect(route.pathname).toEqual(homeStack.route);
|
|
328
|
+
expect(route.screenData.id).toEqual(entry.id);
|
|
329
|
+
});
|
|
330
|
+
|
|
331
|
+
it("does not re-render a non-modal route when modal state changes", () => {
|
|
332
|
+
let renders = 0;
|
|
333
|
+
|
|
334
|
+
renderRouteProbe(homeStack.route, () => {
|
|
335
|
+
renders += 1;
|
|
336
|
+
});
|
|
337
|
+
|
|
338
|
+
const rendersBeforeOpen = renders;
|
|
339
|
+
|
|
340
|
+
openRiverModal();
|
|
341
|
+
|
|
342
|
+
expect(renders).toBe(rendersBeforeOpen);
|
|
343
|
+
});
|
|
344
|
+
|
|
345
|
+
it("re-renders a modal route when modal state changes", () => {
|
|
346
|
+
let route: any;
|
|
347
|
+
|
|
348
|
+
renderRouteProbe(modalPathname, (current) => {
|
|
349
|
+
route = current;
|
|
350
|
+
});
|
|
351
|
+
|
|
352
|
+
expect(route.screenData).toEqual({});
|
|
353
|
+
|
|
354
|
+
openRiverModal();
|
|
355
|
+
|
|
356
|
+
expect(route.screenData.id).toEqual(rivers["river-general"].id);
|
|
357
|
+
});
|
|
358
|
+
});
|
|
236
359
|
});
|
|
237
360
|
|
|
238
361
|
describe("isNavBarVisible", () => {
|
|
@@ -48,14 +48,18 @@ export const useRoute = (
|
|
|
48
48
|
"contentTypes",
|
|
49
49
|
]);
|
|
50
50
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
51
|
+
// useRoute runs in every cell, so both modal stores are subscribed to only on
|
|
52
|
+
// the routes that actually read them. Off those routes the selectors return a
|
|
53
|
+
// stable reference, which keeps an unrelated modal opening or closing from
|
|
54
|
+
// re-rendering the whole tree.
|
|
55
55
|
const hookModalScreenData = useHookModalScreenData(
|
|
56
56
|
!isHookModalPathname(pathname)
|
|
57
57
|
);
|
|
58
58
|
|
|
59
|
+
const modalState = useModalStoreState(!isModalPathname(pathname));
|
|
60
|
+
|
|
61
|
+
const modalScreenData = modalState.screen;
|
|
62
|
+
|
|
59
63
|
const videoModalScreenData =
|
|
60
64
|
navigator?.videoModalState?.item &&
|
|
61
65
|
legacyScreenData(
|