@applicaster/zapp-react-native-utils 16.0.0-rc.86 → 16.0.0-rc.87

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.
@@ -31,7 +31,7 @@ describe("showToastAction", () => {
31
31
  message: "Added to queue",
32
32
  extraMessage: "Extra info",
33
33
  style: undefined,
34
- timeout: 1000,
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: 1000,
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 = 1000;
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 1000 ms. Non-confirmation toasts (e.g. Offline Experience)
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
  *
@@ -12,6 +12,12 @@ const generalContent = () => ({
12
12
  label: "Navigation Bar",
13
13
  placeholder: "Choose Nav Bar",
14
14
  },
15
+ {
16
+ key: "hide_menu",
17
+ type: "switch",
18
+ label: "Hide Menu",
19
+ initial_value: false,
20
+ },
15
21
  ],
16
22
  },
17
23
  styles: {
@@ -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
+ });
@@ -98,5 +98,12 @@ export const openBottomSheetModal = ({
98
98
  });
99
99
  };
100
100
 
101
- export const useModalStoreState = (): ModalState =>
102
- useModalStore<ModalState>((state) => state.modalState);
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(nextState);
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 = async (item: MenuItem) => {
69
- const actions = getMenuItemActions(item);
70
- const isSubMenu = actions.some(isPresentSubMenuAction);
70
+ const handleItemPress = useCallback(
71
+ async (item: MenuItem) => {
72
+ const actions = getMenuItemActions(item);
73
+ const isSubMenu = actions.some(isPresentSubMenuAction);
71
74
 
72
- await onPress(item);
75
+ await onPress(item);
73
76
 
74
- if (!isSubMenu) {
75
- await viewModel?.refetch();
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
- }, [modalOrchestrator.canGoBack]);
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": "16.0.0-rc.86",
3
+ "version": "16.0.0-rc.87",
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": "16.0.0-rc.86",
30
+ "@applicaster/applicaster-types": "16.0.0-rc.87",
31
31
  "buffer": "^5.2.1",
32
32
  "camelize": "^1.0.0",
33
33
  "dayjs": "^1.11.10",
@@ -1,6 +1,6 @@
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-native";
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";
@@ -181,6 +181,45 @@ const hookModalWrapper = ({ children }) => (
181
181
  </Provider>
182
182
  );
183
183
 
184
+ // Reports every render of useRoute so the tests below can assert on route
185
+ // transitions and on subscription behaviour, not just on a one-shot result.
186
+ function RouteProbe({ onRender }: { onRender: (route: any) => void }) {
187
+ onRender(useRoute());
188
+
189
+ return null;
190
+ }
191
+
192
+ const openRiverModal = () =>
193
+ act(() => {
194
+ modalStore.getState().openModal({
195
+ item: rivers["river-general"] as any,
196
+ props: { entry: modalEntry },
197
+ });
198
+ });
199
+
200
+ const renderRouteProbe = (pathname: string, onRender: (route: any) => void) => {
201
+ const tree = (currentPathname: string) => (
202
+ <Provider store={store}>
203
+ <ScreenDataContext.Provider value={homeStack.state}>
204
+ <NavigationContext.Provider value={videoModalNavigator}>
205
+ <PathnameContext.Provider value={currentPathname}>
206
+ <RouteProbe onRender={onRender} />
207
+ </PathnameContext.Provider>
208
+ </NavigationContext.Provider>
209
+ </ScreenDataContext.Provider>
210
+ </Provider>
211
+ );
212
+
213
+ const { rerender } = render(tree(pathname));
214
+
215
+ return {
216
+ navigateTo: (nextPathname: string) =>
217
+ act(() => {
218
+ rerender(tree(nextPathname));
219
+ }),
220
+ };
221
+ };
222
+
184
223
  describe("navigation", () => {
185
224
  describe("useRoute", () => {
186
225
  describe("Main stack components", () => {
@@ -279,6 +318,94 @@ describe("navigation", () => {
279
318
  );
280
319
  });
281
320
  });
321
+
322
+ describe("store subscriptions", () => {
323
+ afterEach(() => {
324
+ act(() => {
325
+ modalStore.getState().dismissModal();
326
+ });
327
+ });
328
+
329
+ // Every hook in useRoute must run on every render. When the modal and
330
+ // hook-modal subscriptions lived inside the route branches, walking a
331
+ // single mounted component through the route types changed the hook
332
+ // count and React threw "Rendered more hooks than during the previous
333
+ // render".
334
+ it("keeps a stable hook order when the route type changes", () => {
335
+ let route: any;
336
+
337
+ const { navigateTo } = renderRouteProbe(homeStack.route, (current) => {
338
+ route = current;
339
+ });
340
+
341
+ expect(route.screenData.id).toEqual(entry.id);
342
+ expect(route.screenData.targetScreen.id).toEqual(screen.id);
343
+
344
+ openRiverModal();
345
+ navigateTo(modalPathname);
346
+
347
+ expect(route.pathname).toEqual(modalPathname);
348
+ expect(route.screenData.id).toEqual(modalEntry.id);
349
+
350
+ expect(route.screenData.targetScreen.id).toEqual(
351
+ rivers["river-general"].id
352
+ );
353
+
354
+ navigateTo(hooksModalPathname);
355
+
356
+ expect(route.pathname).toEqual(hooksModalPathname);
357
+
358
+ expect(route.screenData.id).toEqual(
359
+ hookModalContextState.state.screenData.payload.id
360
+ );
361
+
362
+ navigateTo(videoModalPathname);
363
+
364
+ expect(route.pathname).toEqual(videoModalPathname);
365
+ expect(route.screenData.id).toEqual(videoItem.id);
366
+
367
+ expect(route.screenData.targetScreen.id).toEqual(
368
+ rivers["river-player"].id
369
+ );
370
+
371
+ navigateTo(homeStack.route);
372
+
373
+ expect(route.pathname).toEqual(homeStack.route);
374
+ expect(route.screenData.id).toEqual(entry.id);
375
+ });
376
+
377
+ it("does not re-render a non-modal route when modal state changes", () => {
378
+ let renders = 0;
379
+
380
+ renderRouteProbe(homeStack.route, () => {
381
+ renders += 1;
382
+ });
383
+
384
+ const rendersBeforeOpen = renders;
385
+
386
+ openRiverModal();
387
+
388
+ expect(renders).toBe(rendersBeforeOpen);
389
+ });
390
+
391
+ it("re-renders a modal route when modal state changes", () => {
392
+ let route: any;
393
+
394
+ renderRouteProbe(modalPathname, (current) => {
395
+ route = current;
396
+ });
397
+
398
+ expect(route.screenData).toEqual({});
399
+
400
+ openRiverModal();
401
+
402
+ expect(route.screenData.id).toEqual(modalEntry.id);
403
+
404
+ expect(route.screenData.targetScreen.id).toEqual(
405
+ rivers["river-general"].id
406
+ );
407
+ });
408
+ });
282
409
  });
283
410
 
284
411
  describe("isNavBarVisible", () => {
@@ -50,14 +50,18 @@ export const useRoute = (
50
50
  const rivers = useRivers();
51
51
  const contentTypes = useContentTypes();
52
52
 
53
- const modalState = useModalStoreState();
54
-
55
- const modalScreenData = modalState.screen;
56
-
53
+ // useRoute runs in every cell, so both modal stores are subscribed to only on
54
+ // the routes that actually read them. Off those routes the selectors return a
55
+ // stable reference, which keeps an unrelated modal opening or closing from
56
+ // re-rendering the whole tree.
57
57
  const hookModalScreenData = useHookModalScreenData(
58
58
  !isHookModalPathname(pathname)
59
59
  );
60
60
 
61
+ const modalState = useModalStoreState(!isModalPathname(pathname));
62
+
63
+ const modalScreenData = modalState.screen;
64
+
61
65
  const videoModalScreenData =
62
66
  navigator?.videoModalState?.item &&
63
67
  legacyScreenData(