@applicaster/zapp-react-native-utils 16.0.0-alpha.4004661200 → 16.0.0-alpha.4065452341
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/ActionExecutorContext.tsx +10 -1
- package/actionsExecutor/actions/__tests__/dismissBottomSheet.test.ts +3 -0
- package/actionsExecutor/actions/__tests__/openBottomSheet.inlineItems.test.ts +113 -1
- package/actionsExecutor/actions/__tests__/presentScreen.test.ts +127 -0
- package/actionsExecutor/actions/__tests__/showToast.test.ts +2 -2
- package/actionsExecutor/actions/dismissBottomSheet.ts +2 -1
- package/actionsExecutor/actions/index.ts +2 -0
- package/actionsExecutor/actions/openBottomSheet.ts +20 -11
- package/actionsExecutor/actions/presentScreen.ts +78 -0
- package/actionsExecutor/actions/showToast.ts +2 -2
- package/actionsExecutor/consts.ts +1 -0
- package/configurationUtils/__tests__/manifestKeyParser.test.ts +21 -0
- package/configurationUtils/__tests__/modalBlocksParser.test.ts +153 -0
- package/configurationUtils/manifestKeyParser.ts +12 -1
- package/configurationUtils/modalBlocksParser.ts +118 -0
- package/entryActions/__tests__/buildEntryActions.test.ts +39 -0
- package/entryActions/buildEntryActions.ts +1 -0
- package/manifestUtils/defaultManifestConfigurations/generalContent.js +6 -0
- package/modalState/ModalOrchestrator.ts +21 -9
- package/modalState/RemoteEditableCollection.ts +9 -1
- package/modalState/__tests__/RemoteEditableCollection.test.ts +90 -1
- package/modalState/__tests__/index.test.ts +30 -1
- package/modalState/__tests__/useModalStoreState.test.ts +142 -0
- package/modalState/index.ts +6 -1
- package/modalState/store.ts +9 -2
- package/modalState/types.ts +29 -2
- package/modalState/useBottomSheetContent.ts +24 -17
- package/package.json +2 -2
- package/reactHooks/cell-click/__tests__/index.test.js +64 -0
- package/reactHooks/cell-click/index.ts +21 -14
- package/reactHooks/navigation/__tests__/index.test.tsx +176 -1
- package/reactHooks/navigation/__tests__/usePresentScreen.test.ts +154 -0
- package/reactHooks/navigation/index.ts +2 -0
- package/reactHooks/navigation/usePresentScreen.ts +157 -0
- package/reactHooks/navigation/useRoute.ts +22 -5
|
@@ -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/index.ts
CHANGED
|
@@ -25,6 +25,11 @@ export type {
|
|
|
25
25
|
SelectionBehavior,
|
|
26
26
|
} from "./types";
|
|
27
27
|
|
|
28
|
-
export {
|
|
28
|
+
export {
|
|
29
|
+
isPresentSubMenuAction,
|
|
30
|
+
getMenuItemActions,
|
|
31
|
+
OPERATIONS,
|
|
32
|
+
hasOperation,
|
|
33
|
+
} from "./types";
|
|
29
34
|
|
|
30
35
|
export { useBottomSheetContent } from "./useBottomSheetContent";
|
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
|
+
);
|
package/modalState/types.ts
CHANGED
|
@@ -37,8 +37,8 @@ export interface MenuItem {
|
|
|
37
37
|
summary?: string;
|
|
38
38
|
icon?: string;
|
|
39
39
|
isSelected?: boolean;
|
|
40
|
-
|
|
41
|
-
|
|
40
|
+
actions?: (Action | PresentSubMenuAction)[];
|
|
41
|
+
/** @deprecated Use `actions` array instead. Maintained for backwards compatibility. */
|
|
42
42
|
action?: Action | PresentSubMenuAction;
|
|
43
43
|
secondaryAction?: SecondaryAction;
|
|
44
44
|
trailingButton?: any;
|
|
@@ -118,6 +118,7 @@ export interface Content {
|
|
|
118
118
|
dynamicCollection?: DynamicCollectionOptions;
|
|
119
119
|
themePluginId?: string;
|
|
120
120
|
styleOverrides?: Record<string, any>;
|
|
121
|
+
context?: Record<string, any>;
|
|
121
122
|
}
|
|
122
123
|
|
|
123
124
|
export interface Menu {
|
|
@@ -131,3 +132,29 @@ export function isPresentSubMenuAction(
|
|
|
131
132
|
): action is PresentSubMenuAction {
|
|
132
133
|
return !!action && typeof action === "object" && "menu" in action;
|
|
133
134
|
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Normalizes an item's action/actions into an array of Action items.
|
|
138
|
+
* Supports canonical `actions: Action[]` and backwards-compatible `action: Action | Action[]`.
|
|
139
|
+
*/
|
|
140
|
+
export function getMenuItemActions(
|
|
141
|
+
item?: Partial<MenuItem> | null
|
|
142
|
+
): (Action | PresentSubMenuAction)[] {
|
|
143
|
+
if (!item) {
|
|
144
|
+
return [];
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
if (Array.isArray(item.actions) && item.actions.length > 0) {
|
|
148
|
+
return item.actions;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
if (Array.isArray((item as any).action)) {
|
|
152
|
+
return (item as any).action;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
if (item.action) {
|
|
156
|
+
return [item.action];
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
return [];
|
|
160
|
+
}
|
|
@@ -1,14 +1,15 @@
|
|
|
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
|
-
import {
|
|
5
|
-
isPresentSubMenuAction,
|
|
6
|
-
MenuItem,
|
|
7
|
-
modalOrchestrator,
|
|
8
|
-
} from "./ModalOrchestrator";
|
|
4
|
+
import { MenuItem, modalOrchestrator } from "./ModalOrchestrator";
|
|
9
5
|
|
|
10
6
|
import { ContentViewModel } from "./ContentViewModel";
|
|
11
|
-
import
|
|
7
|
+
import {
|
|
8
|
+
DynamicCollectionOptions,
|
|
9
|
+
getMenuItemActions,
|
|
10
|
+
isPresentSubMenuAction,
|
|
11
|
+
SelectionBehavior,
|
|
12
|
+
} from "./types";
|
|
12
13
|
import type { EditableCollection } from "./EditableCollection";
|
|
13
14
|
|
|
14
15
|
type Props = {
|
|
@@ -44,7 +45,9 @@ export const useBottomSheetContent = (props: Props): Return => {
|
|
|
44
45
|
// Subscribe to the viewModel state changes
|
|
45
46
|
useEffect(() => {
|
|
46
47
|
return viewModel?.subscribe((nextState) => {
|
|
47
|
-
setState(
|
|
48
|
+
setState((state) => {
|
|
49
|
+
return state === nextState ? state : nextState;
|
|
50
|
+
});
|
|
48
51
|
});
|
|
49
52
|
}, [viewModel]);
|
|
50
53
|
|
|
@@ -64,15 +67,19 @@ export const useBottomSheetContent = (props: Props): Return => {
|
|
|
64
67
|
* - If the item was a leaf action (not a submenu), triggers viewModel.refetch()
|
|
65
68
|
* so that checkmarks (isSelected) reflect the updated server state.
|
|
66
69
|
*/
|
|
67
|
-
const handleItemPress =
|
|
68
|
-
|
|
70
|
+
const handleItemPress = useCallback(
|
|
71
|
+
async (item: MenuItem) => {
|
|
72
|
+
const actions = getMenuItemActions(item);
|
|
73
|
+
const isSubMenu = actions.some(isPresentSubMenuAction);
|
|
69
74
|
|
|
70
|
-
|
|
75
|
+
await onPress(item);
|
|
71
76
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
77
|
+
if (!isSubMenu) {
|
|
78
|
+
await viewModel?.refetch();
|
|
79
|
+
}
|
|
80
|
+
},
|
|
81
|
+
[onPress, viewModel]
|
|
82
|
+
);
|
|
76
83
|
|
|
77
84
|
const goBackOrClose = React.useCallback(() => {
|
|
78
85
|
if (modalOrchestrator.canGoBack) {
|
|
@@ -80,7 +87,7 @@ export const useBottomSheetContent = (props: Props): Return => {
|
|
|
80
87
|
} else {
|
|
81
88
|
modalOrchestrator.close();
|
|
82
89
|
}
|
|
83
|
-
}, [
|
|
90
|
+
}, []);
|
|
84
91
|
|
|
85
92
|
return {
|
|
86
93
|
items: viewModel?.editableCollection ? editableItems : state?.items,
|
|
@@ -92,7 +99,7 @@ export const useBottomSheetContent = (props: Props): Return => {
|
|
|
92
99
|
canGoBack: modalOrchestrator.canGoBack,
|
|
93
100
|
handleItemPress,
|
|
94
101
|
goBackOrClose,
|
|
95
|
-
close: () => modalOrchestrator.close(),
|
|
102
|
+
close: useCallback(() => modalOrchestrator.close(), []),
|
|
96
103
|
role: state?.role,
|
|
97
104
|
behavior: state?.behavior,
|
|
98
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-alpha.
|
|
3
|
+
"version": "16.0.0-alpha.4065452341",
|
|
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-alpha.
|
|
30
|
+
"@applicaster/applicaster-types": "16.0.0-alpha.4065452341",
|
|
31
31
|
"buffer": "^5.2.1",
|
|
32
32
|
"camelize": "^1.0.0",
|
|
33
33
|
"dayjs": "^1.11.10",
|
|
@@ -5,6 +5,7 @@ import { ActionExecutorContext } from "@applicaster/zapp-react-native-utils/acti
|
|
|
5
5
|
|
|
6
6
|
const mockNavigator = {
|
|
7
7
|
getPathname: () => "pathnameMock",
|
|
8
|
+
currentRoute: "pathnameMock",
|
|
8
9
|
push: jest.fn(),
|
|
9
10
|
data: {},
|
|
10
11
|
};
|
|
@@ -29,6 +30,7 @@ jest.mock("@applicaster/zapp-react-native-utils/reactHooks/screen", () => ({
|
|
|
29
30
|
),
|
|
30
31
|
useTargetScreenData: jest.fn(() => ({})),
|
|
31
32
|
useCurrentScreenData: jest.fn(() => ({})),
|
|
33
|
+
useScreenContext: jest.fn(() => ({})),
|
|
32
34
|
}));
|
|
33
35
|
|
|
34
36
|
jest.mock("@applicaster/zapp-react-native-ui-components/Contexts", () => ({
|
|
@@ -66,6 +68,12 @@ const wrapper = ({ children }) => (
|
|
|
66
68
|
</CellTapContext.Provider>
|
|
67
69
|
);
|
|
68
70
|
|
|
71
|
+
const actionExecutorWrapper = ({ children }) => (
|
|
72
|
+
<ActionExecutorContext.Provider value={actionExecutor}>
|
|
73
|
+
{children}
|
|
74
|
+
</ActionExecutorContext.Provider>
|
|
75
|
+
);
|
|
76
|
+
|
|
69
77
|
describe("useCellClick", () => {
|
|
70
78
|
it("returns a function", () => {
|
|
71
79
|
const { result } = renderHook(() => useCellClick(mockProps));
|
|
@@ -95,4 +103,60 @@ describe("useCellClick", () => {
|
|
|
95
103
|
|
|
96
104
|
cleanup();
|
|
97
105
|
});
|
|
106
|
+
|
|
107
|
+
it("does not push when the cell finishes a hook", async () => {
|
|
108
|
+
mockNavigator.push.mockClear();
|
|
109
|
+
|
|
110
|
+
const item = {
|
|
111
|
+
id: "profile-1",
|
|
112
|
+
type: { value: "feed" },
|
|
113
|
+
extensions: {
|
|
114
|
+
tap_actions: {
|
|
115
|
+
actions: [
|
|
116
|
+
{ type: "sessionStorageSet" },
|
|
117
|
+
{ type: "finishHook", options: { success: true } },
|
|
118
|
+
],
|
|
119
|
+
},
|
|
120
|
+
},
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
const { result } = renderHook(() => useCellClick({ item }), {
|
|
124
|
+
wrapper: actionExecutorWrapper,
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
await result.current();
|
|
128
|
+
|
|
129
|
+
await waitFor(() => {
|
|
130
|
+
expect(actionExecutor.handleEntryActions).toHaveBeenCalled();
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
expect(mockNavigator.push).not.toHaveBeenCalled();
|
|
134
|
+
cleanup();
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
it("pushes after tap actions that do not finish a hook", async () => {
|
|
138
|
+
mockNavigator.push.mockClear();
|
|
139
|
+
|
|
140
|
+
const item = {
|
|
141
|
+
id: "episode-1",
|
|
142
|
+
type: { value: "feed" },
|
|
143
|
+
extensions: {
|
|
144
|
+
tap_actions: {
|
|
145
|
+
actions: [{ type: "sessionStorageSet" }],
|
|
146
|
+
},
|
|
147
|
+
},
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
const { result } = renderHook(() => useCellClick({ item }), {
|
|
151
|
+
wrapper: actionExecutorWrapper,
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
await result.current();
|
|
155
|
+
|
|
156
|
+
await waitFor(() => {
|
|
157
|
+
expect(mockNavigator.push).toHaveBeenCalled();
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
cleanup();
|
|
161
|
+
});
|
|
98
162
|
});
|
|
@@ -83,6 +83,12 @@ export const useCellClick = ({
|
|
|
83
83
|
|
|
84
84
|
logOnPress(selectedItem, pathname, component, onCellTap);
|
|
85
85
|
|
|
86
|
+
const tapActions = selectedItem?.extensions?.tap_actions?.actions;
|
|
87
|
+
|
|
88
|
+
const finishesHook =
|
|
89
|
+
Array.isArray(tapActions) &&
|
|
90
|
+
tapActions.some((action) => action?.type === "finishHook");
|
|
91
|
+
|
|
86
92
|
if (selectedItem) {
|
|
87
93
|
await actionExecutor?.handleEntryActions(selectedItem, {
|
|
88
94
|
component,
|
|
@@ -101,20 +107,21 @@ export const useCellClick = ({
|
|
|
101
107
|
url: selectedItem.link.href,
|
|
102
108
|
});
|
|
103
109
|
}
|
|
104
|
-
} else {
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
110
|
+
} else if (isFunction(onCellTap)) {
|
|
111
|
+
onCellTap?.(selectedItem, _index);
|
|
112
|
+
} else if (!finishesHook) {
|
|
113
|
+
// finishHook already completes the hook and navigates to the original
|
|
114
|
+
// target. Pushing the cell as well stacks a second copy of that screen
|
|
115
|
+
// (e.g. home/home after a profile selector hook).
|
|
116
|
+
if (currentRoute === pathname || pathname.includes("video-modal")) {
|
|
117
|
+
const targetScreen = component?.data?.target;
|
|
118
|
+
|
|
119
|
+
push(
|
|
120
|
+
R.when(
|
|
121
|
+
() => targetScreen,
|
|
122
|
+
R.mergeLeft({ screen_type: targetScreen })
|
|
123
|
+
)(selectedItem)
|
|
124
|
+
);
|
|
118
125
|
}
|
|
119
126
|
}
|
|
120
127
|
},
|
|
@@ -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-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";
|
|
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
|
|
|
@@ -156,6 +157,20 @@ const videoModalWrapper = ({ children }) => (
|
|
|
156
157
|
</Provider>
|
|
157
158
|
);
|
|
158
159
|
|
|
160
|
+
const modalEntry = { id: "modal-entry-1", type: { value: "video" } };
|
|
161
|
+
|
|
162
|
+
const modalPathname = `${ROUTE_TYPES.MODAL}/${rivers["river-general"].id}`;
|
|
163
|
+
|
|
164
|
+
const modalWrapper = ({ children }) => (
|
|
165
|
+
<Provider store={store}>
|
|
166
|
+
<NavigationContext.Provider value={mainStackNavigator}>
|
|
167
|
+
<PathnameContext.Provider value={modalPathname}>
|
|
168
|
+
{children}
|
|
169
|
+
</PathnameContext.Provider>
|
|
170
|
+
</NavigationContext.Provider>
|
|
171
|
+
</Provider>
|
|
172
|
+
);
|
|
173
|
+
|
|
159
174
|
const hookModalWrapper = ({ children }) => (
|
|
160
175
|
<Provider store={store}>
|
|
161
176
|
<NavigationContext.Provider value={hookModalNavigator}>
|
|
@@ -166,6 +181,45 @@ const hookModalWrapper = ({ children }) => (
|
|
|
166
181
|
</Provider>
|
|
167
182
|
);
|
|
168
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
|
+
|
|
169
223
|
describe("navigation", () => {
|
|
170
224
|
describe("useRoute", () => {
|
|
171
225
|
describe("Main stack components", () => {
|
|
@@ -188,6 +242,39 @@ describe("navigation", () => {
|
|
|
188
242
|
});
|
|
189
243
|
});
|
|
190
244
|
|
|
245
|
+
describe("Modal components", () => {
|
|
246
|
+
beforeEach(() => {
|
|
247
|
+
modalStore.getState().openModal({
|
|
248
|
+
item: rivers["river-general"] as any,
|
|
249
|
+
props: { entry: modalEntry },
|
|
250
|
+
});
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
afterEach(() => {
|
|
254
|
+
modalStore.getState().dismissModal();
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
it("should return a correct pathname", () => {
|
|
258
|
+
const { result } = renderHook(() => useRoute(), {
|
|
259
|
+
wrapper: modalWrapper,
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
expect(result.current.pathname).toEqual(modalPathname);
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
it("should return correct screenData information", () => {
|
|
266
|
+
const { result } = renderHook(() => useRoute(), {
|
|
267
|
+
wrapper: modalWrapper,
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
expect(result.current.screenData.id).toEqual(modalEntry.id);
|
|
271
|
+
|
|
272
|
+
expect(result.current.screenData.targetScreen.id).toEqual(
|
|
273
|
+
rivers["river-general"].id
|
|
274
|
+
);
|
|
275
|
+
});
|
|
276
|
+
});
|
|
277
|
+
|
|
191
278
|
describe("Hook modal components", () => {
|
|
192
279
|
it("should return a correct pathname", () => {
|
|
193
280
|
const { result } = renderHook(() => useRoute(), {
|
|
@@ -231,6 +318,94 @@ describe("navigation", () => {
|
|
|
231
318
|
);
|
|
232
319
|
});
|
|
233
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
|
+
});
|
|
234
409
|
});
|
|
235
410
|
|
|
236
411
|
describe("isNavBarVisible", () => {
|