@applicaster/zapp-react-native-utils 15.1.15-alpha.4236143002 → 15.1.15
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/ActionExecutor.ts +10 -1
- package/actionsExecutor/ActionExecutorContext.tsx +3 -0
- package/actionsExecutor/__tests__/ActionExecutor.test.ts +71 -0
- package/actionsExecutor/actions/__tests__/showAlert.test.ts +49 -0
- package/actionsExecutor/actions/__tests__/showToast.test.ts +2 -2
- package/actionsExecutor/actions/index.ts +2 -0
- package/actionsExecutor/actions/showAlert.ts +51 -0
- package/actionsExecutor/actions/showToast.ts +2 -2
- package/actionsExecutor/consts.ts +1 -0
- 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
|
@@ -56,7 +56,6 @@ export class ActionExecutor {
|
|
|
56
56
|
context?: ActionExecutionContext
|
|
57
57
|
): Promise<ActionResult> => {
|
|
58
58
|
for (const action of actionTypes) {
|
|
59
|
-
// TODO: Handle action result error
|
|
60
59
|
const result = await this.handleAction(action, context);
|
|
61
60
|
|
|
62
61
|
if (result === ActionResult.Cancel) {
|
|
@@ -64,6 +63,16 @@ export class ActionExecutor {
|
|
|
64
63
|
|
|
65
64
|
return result;
|
|
66
65
|
}
|
|
66
|
+
|
|
67
|
+
if (result === ActionResult.Error) {
|
|
68
|
+
// Everything after a failed step was written for a state that never
|
|
69
|
+
// happened — the event was refused, the screen never opened — so
|
|
70
|
+
// running it is how a chain ends up telling the user their mail was
|
|
71
|
+
// sent when it was not.
|
|
72
|
+
log_error(`handleActions: action: ${action.type} failed`, { action });
|
|
73
|
+
|
|
74
|
+
return result;
|
|
75
|
+
}
|
|
67
76
|
}
|
|
68
77
|
|
|
69
78
|
return ActionResult.Success;
|
|
@@ -18,6 +18,7 @@ import { createLogger } from "../logger";
|
|
|
18
18
|
import {
|
|
19
19
|
appRestartAction,
|
|
20
20
|
confirmDialogAction,
|
|
21
|
+
showAlertAction,
|
|
21
22
|
createGoBackAction,
|
|
22
23
|
createGoHomeAction,
|
|
23
24
|
createNavigateToScreenAction,
|
|
@@ -109,6 +110,8 @@ const prepareDefaultActions = (actionExecutor) => {
|
|
|
109
110
|
confirmDialogAction
|
|
110
111
|
);
|
|
111
112
|
|
|
113
|
+
actionExecutor.registerAction(ACTION_TYPES.SHOW_ALERT, showAlertAction);
|
|
114
|
+
|
|
112
115
|
actionExecutor.registerAction(
|
|
113
116
|
ACTION_TYPES.SEND_CLOUD_EVENT,
|
|
114
117
|
sendCloudEventAction
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { ActionExecutor, ActionResult } from "../ActionExecutor";
|
|
2
|
+
|
|
3
|
+
jest.mock("../../logger", () => ({
|
|
4
|
+
createLogger: () => ({
|
|
5
|
+
log_error: jest.fn(),
|
|
6
|
+
log_debug: jest.fn(),
|
|
7
|
+
log_info: jest.fn(),
|
|
8
|
+
}),
|
|
9
|
+
}));
|
|
10
|
+
|
|
11
|
+
describe("handleActions", () => {
|
|
12
|
+
const executor = () => {
|
|
13
|
+
const ran: string[] = [];
|
|
14
|
+
const actionExecutor = new ActionExecutor();
|
|
15
|
+
|
|
16
|
+
const register = (type: string, result?: ActionResult) =>
|
|
17
|
+
actionExecutor.registerAction(type, async () => {
|
|
18
|
+
ran.push(type);
|
|
19
|
+
|
|
20
|
+
return result as ActionResult;
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
register("ok", ActionResult.Success);
|
|
24
|
+
register("fails", ActionResult.Error);
|
|
25
|
+
register("cancels", ActionResult.Cancel);
|
|
26
|
+
register("recover", ActionResult.Success);
|
|
27
|
+
register("after", ActionResult.Success);
|
|
28
|
+
|
|
29
|
+
return { actionExecutor, ran };
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
// Everything after a failed step was written for a state that never
|
|
33
|
+
// happened: the event was refused, the screen never opened.
|
|
34
|
+
it("stops when an action fails", async () => {
|
|
35
|
+
const { actionExecutor, ran } = executor();
|
|
36
|
+
|
|
37
|
+
const result = await actionExecutor.handleActions([
|
|
38
|
+
{ type: "fails" },
|
|
39
|
+
{ type: "after" },
|
|
40
|
+
]);
|
|
41
|
+
|
|
42
|
+
expect(ran).toEqual(["fails"]);
|
|
43
|
+
expect(result).toBe(ActionResult.Error);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it("still stops on a cancel", async () => {
|
|
47
|
+
const { actionExecutor, ran } = executor();
|
|
48
|
+
|
|
49
|
+
const result = await actionExecutor.handleActions([
|
|
50
|
+
{ type: "cancels" },
|
|
51
|
+
{ type: "after" },
|
|
52
|
+
]);
|
|
53
|
+
|
|
54
|
+
expect(ran).toEqual(["cancels"]);
|
|
55
|
+
expect(result).toBe(ActionResult.Cancel);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
// An action nobody registered never ran, which is a misconfigured feed
|
|
59
|
+
// rather than a step that failed. It is stepped over, as it always was.
|
|
60
|
+
it("steps over an action with no handler", async () => {
|
|
61
|
+
const { actionExecutor, ran } = executor();
|
|
62
|
+
|
|
63
|
+
const result = await actionExecutor.handleActions([
|
|
64
|
+
{ type: "nobody-registered-this" },
|
|
65
|
+
{ type: "after" },
|
|
66
|
+
]);
|
|
67
|
+
|
|
68
|
+
expect(ran).toEqual(["after"]);
|
|
69
|
+
expect(result).toBe(ActionResult.Success);
|
|
70
|
+
});
|
|
71
|
+
});
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { showAlertAction } from "../showAlert";
|
|
2
|
+
import { ActionResult } from "../../ActionExecutor";
|
|
3
|
+
import { showAlertDialog } from "../../../alertUtils";
|
|
4
|
+
|
|
5
|
+
jest.mock("../../../alertUtils", () => ({
|
|
6
|
+
showAlertDialog: jest.fn(),
|
|
7
|
+
}));
|
|
8
|
+
|
|
9
|
+
const dialog = showAlertDialog as jest.Mock;
|
|
10
|
+
|
|
11
|
+
const dismiss = () => dialog.mock.calls[0][0].completion();
|
|
12
|
+
|
|
13
|
+
describe("showAlert", () => {
|
|
14
|
+
beforeEach(() => dialog.mockReset());
|
|
15
|
+
|
|
16
|
+
it("shows the notice and resolves once it is dismissed", async () => {
|
|
17
|
+
const result = showAlertAction({
|
|
18
|
+
type: "showAlert",
|
|
19
|
+
options: { title: "Locked", message: "Ask the account owner." },
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
expect(dialog).toHaveBeenCalledWith(
|
|
23
|
+
expect.objectContaining({
|
|
24
|
+
title: "Locked",
|
|
25
|
+
message: "Ask the account owner.",
|
|
26
|
+
okButtonText: "OK",
|
|
27
|
+
})
|
|
28
|
+
);
|
|
29
|
+
|
|
30
|
+
dismiss();
|
|
31
|
+
|
|
32
|
+
// Dismissing a notice is not a cancellation: the chain carries on.
|
|
33
|
+
await expect(result).resolves.toBe(ActionResult.Success);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it("takes the dismiss label from the action", async () => {
|
|
37
|
+
const result = showAlertAction({
|
|
38
|
+
type: "showAlert",
|
|
39
|
+
options: { title: "Locked", okButtonText: "Got it" },
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
expect(dialog).toHaveBeenCalledWith(
|
|
43
|
+
expect.objectContaining({ okButtonText: "Got it" })
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
dismiss();
|
|
47
|
+
await result;
|
|
48
|
+
});
|
|
49
|
+
});
|
|
@@ -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
|
]);
|
|
@@ -15,6 +15,8 @@ export { appRestartAction } from "./appRestart";
|
|
|
15
15
|
|
|
16
16
|
export { confirmDialogAction } from "./confirmDialog";
|
|
17
17
|
|
|
18
|
+
export { showAlertAction } from "./showAlert";
|
|
19
|
+
|
|
18
20
|
export { sendCloudEventAction } from "./sendCloudEvent";
|
|
19
21
|
|
|
20
22
|
export { sessionStorageToggleFlagAction } from "./sessionStorageToggleFlag";
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/// <reference types="@applicaster/applicaster-types" />
|
|
2
|
+
import { showAlertDialog } from "../../alertUtils";
|
|
3
|
+
import { ActionResult } from "../ActionExecutor";
|
|
4
|
+
import { ActionHandler } from "../types";
|
|
5
|
+
import { createLogger } from "../../logger";
|
|
6
|
+
|
|
7
|
+
const { log_info } = createLogger({
|
|
8
|
+
subsystem: "ActionExecutorContext",
|
|
9
|
+
category: "General",
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Options for the showAlert action.
|
|
14
|
+
*/
|
|
15
|
+
export interface ShowAlertOptions {
|
|
16
|
+
/** Dialog title (required) */
|
|
17
|
+
title: string;
|
|
18
|
+
|
|
19
|
+
/** Dialog message */
|
|
20
|
+
message?: string;
|
|
21
|
+
|
|
22
|
+
/** Dismiss button text. Defaults to "OK" */
|
|
23
|
+
okButtonText?: string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Tells the user something and waits for them to acknowledge it.
|
|
28
|
+
*
|
|
29
|
+
* One button, because there is nothing to decide. Use `confirmDialog` when the
|
|
30
|
+
* user is choosing between going on and backing out; a `confirmDialog` without
|
|
31
|
+
* a cancel label still renders two buttons, the second one blank.
|
|
32
|
+
*
|
|
33
|
+
* Always resolves to Success: dismissing a notice is not a cancellation, so the
|
|
34
|
+
* rest of the chain runs.
|
|
35
|
+
*/
|
|
36
|
+
export const showAlertAction: ActionHandler<ShowAlertOptions> = async (
|
|
37
|
+
action
|
|
38
|
+
): Promise<ActionResult> => {
|
|
39
|
+
log_info("handleAction: showAlert event");
|
|
40
|
+
|
|
41
|
+
const options = action.options as ShowAlertOptions;
|
|
42
|
+
|
|
43
|
+
return new Promise<ActionResult>((resolve) => {
|
|
44
|
+
showAlertDialog({
|
|
45
|
+
title: options?.title,
|
|
46
|
+
message: options?.message,
|
|
47
|
+
okButtonText: options?.okButtonText || "OK",
|
|
48
|
+
completion: () => resolve(ActionResult.Success),
|
|
49
|
+
});
|
|
50
|
+
});
|
|
51
|
+
};
|
|
@@ -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",
|
|
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",
|
|
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(
|