@applicaster/zapp-react-native-utils 16.0.0-alpha.1151561937 → 16.0.0-alpha.1180700101
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 +26 -1
- package/actionsExecutor/actions/__tests__/dismissBottomSheet.test.ts +3 -0
- package/actionsExecutor/actions/__tests__/goBack.test.ts +62 -0
- package/actionsExecutor/actions/__tests__/goHome.test.ts +19 -0
- package/actionsExecutor/actions/__tests__/openBottomSheet.inlineItems.test.ts +113 -1
- package/actionsExecutor/actions/__tests__/presentScreen.test.ts +127 -0
- package/actionsExecutor/actions/dismissBottomSheet.ts +2 -1
- package/actionsExecutor/actions/goBack.ts +60 -0
- package/actionsExecutor/actions/goHome.ts +31 -0
- package/actionsExecutor/actions/index.ts +6 -0
- package/actionsExecutor/actions/openBottomSheet.ts +15 -10
- package/actionsExecutor/actions/presentScreen.ts +78 -0
- package/actionsExecutor/consts.ts +3 -0
- package/appUtils/playerManager/__tests__/playerNative.test.ts +38 -0
- package/appUtils/playerManager/playerNative.ts +5 -3
- 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 +156 -0
- package/entryActions/aliasPresentation.ts +124 -0
- package/entryActions/buildEntryActions.ts +166 -0
- package/entryActions/index.ts +17 -0
- package/entryActions/types.ts +39 -0
- package/manifestUtils/_internals/index.js +8 -3
- package/manifestUtils/defaultManifestConfigurations/player.js +22 -1
- package/modalState/ModalOrchestrator.ts +21 -9
- package/modalState/__tests__/index.test.ts +30 -1
- package/modalState/index.ts +6 -1
- package/modalState/types.ts +28 -2
- package/modalState/useBottomSheetContent.ts +9 -7
- package/package.json +2 -2
- package/playerUtils/__tests__/resolvePlayerActions.test.ts +138 -0
- package/playerUtils/index.ts +17 -1
- package/playerUtils/resolvePlayerActions.ts +71 -0
- package/reactHooks/actions/index.ts +67 -2
- package/reactHooks/navigation/__tests__/index.test.tsx +48 -0
- 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 +14 -1
- package/uiActionsRegistrator/__tests__/resolveActionIdentifiers.test.ts +93 -0
- package/uiActionsRegistrator/__tests__/subscribe.test.ts +107 -0
- package/uiActionsRegistrator/__tests__/usableActionItems.test.ts +52 -0
- package/uiActionsRegistrator/index.ts +25 -203
- package/uiActionsRegistrator/registry.ts +335 -0
- package/uiActionsRegistrator/resolveActionIdentifiers.ts +100 -0
- package/uiActionsRegistrator/usableActionItems.ts +66 -0
- package/playerUtils/__tests__/getPlayerActionButtons.test.ts +0 -54
- package/playerUtils/getPlayerActionButtons.ts +0 -17
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { resolvePlayerActions } from "../resolvePlayerActions";
|
|
2
|
+
import { selectActionButtons } from "../../conf/player/selectors";
|
|
3
|
+
import { uiActionsRegistry } from "../../uiActionsRegistrator";
|
|
4
|
+
|
|
5
|
+
jest.mock("../../conf/player/selectors", () => ({
|
|
6
|
+
selectActionButtons: jest.fn(),
|
|
7
|
+
}));
|
|
8
|
+
|
|
9
|
+
const action = (identifier: string) =>
|
|
10
|
+
({ identifier, action: { invokeAction: jest.fn() } }) as any;
|
|
11
|
+
|
|
12
|
+
describe("resolvePlayerActions", () => {
|
|
13
|
+
const entry = {
|
|
14
|
+
id: "e1",
|
|
15
|
+
extensions: {
|
|
16
|
+
entry_action: [
|
|
17
|
+
{ button: { alias: "add_to_playlist" }, actions: [{ type: "test" }] },
|
|
18
|
+
{ button: { alias: "add_to_queue" }, actions: [{ type: "test" }] },
|
|
19
|
+
],
|
|
20
|
+
},
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
// The entry's own actions never reach the registry; the caller expands them.
|
|
24
|
+
const expandEntryActions = jest.fn(() => [
|
|
25
|
+
action("entry_action_0"),
|
|
26
|
+
action("entry_action_1"),
|
|
27
|
+
]);
|
|
28
|
+
|
|
29
|
+
beforeEach(() => {
|
|
30
|
+
expandEntryActions.mockClear();
|
|
31
|
+
|
|
32
|
+
jest
|
|
33
|
+
.spyOn(uiActionsRegistry, "getActions")
|
|
34
|
+
.mockImplementation((name) => [action(name)]);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
afterEach(() => {
|
|
38
|
+
jest.restoreAllMocks();
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it("expands entry_actions through the caller and keeps config order", () => {
|
|
42
|
+
(selectActionButtons as jest.Mock).mockReturnValue(
|
|
43
|
+
"entry_actions,local_storage_favourites_action,offline-content-button"
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
const result = resolvePlayerActions({
|
|
47
|
+
configuration: {},
|
|
48
|
+
entry,
|
|
49
|
+
expandEntryActions,
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
expect(expandEntryActions).toHaveBeenCalledWith(entry);
|
|
53
|
+
|
|
54
|
+
expect(uiActionsRegistry.getActions).not.toHaveBeenCalledWith(
|
|
55
|
+
"entry_actions",
|
|
56
|
+
entry
|
|
57
|
+
);
|
|
58
|
+
|
|
59
|
+
expect(result.map((item) => item.identifier)).toEqual([
|
|
60
|
+
"entry_action_0",
|
|
61
|
+
"entry_action_1",
|
|
62
|
+
"local_storage_favourites_action",
|
|
63
|
+
"offline-content-button",
|
|
64
|
+
]);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it.each([undefined, null, ""])(
|
|
68
|
+
"resolves nothing when the config is %p",
|
|
69
|
+
(configured) => {
|
|
70
|
+
(selectActionButtons as jest.Mock).mockReturnValue(configured);
|
|
71
|
+
|
|
72
|
+
expect(
|
|
73
|
+
resolvePlayerActions({ configuration: {}, entry, expandEntryActions })
|
|
74
|
+
).toEqual([]);
|
|
75
|
+
}
|
|
76
|
+
);
|
|
77
|
+
|
|
78
|
+
it("does not cap the resolved list at two items", () => {
|
|
79
|
+
(selectActionButtons as jest.Mock).mockReturnValue("a,b,c");
|
|
80
|
+
|
|
81
|
+
expect(
|
|
82
|
+
resolvePlayerActions({
|
|
83
|
+
configuration: {},
|
|
84
|
+
entry,
|
|
85
|
+
expandEntryActions,
|
|
86
|
+
}).map((item) => item.identifier)
|
|
87
|
+
).toEqual(["a", "b", "c"]);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it("has no special case for any plugin identifier", () => {
|
|
91
|
+
(selectActionButtons as jest.Mock).mockReturnValue(
|
|
92
|
+
"open-modal-bottom-sheet-cell-action,local_storage_favourites_action"
|
|
93
|
+
);
|
|
94
|
+
|
|
95
|
+
expect(
|
|
96
|
+
resolvePlayerActions({
|
|
97
|
+
configuration: {},
|
|
98
|
+
entry,
|
|
99
|
+
expandEntryActions,
|
|
100
|
+
}).map((item) => item.identifier)
|
|
101
|
+
).toEqual([
|
|
102
|
+
"open-modal-bottom-sheet-cell-action",
|
|
103
|
+
"local_storage_favourites_action",
|
|
104
|
+
]);
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it("collapses a repeated identifier in the config string", () => {
|
|
108
|
+
(selectActionButtons as jest.Mock).mockReturnValue(
|
|
109
|
+
"favorites,favorites,share"
|
|
110
|
+
);
|
|
111
|
+
|
|
112
|
+
expect(
|
|
113
|
+
resolvePlayerActions({
|
|
114
|
+
configuration: {},
|
|
115
|
+
entry,
|
|
116
|
+
expandEntryActions,
|
|
117
|
+
}).map((item) => item.identifier)
|
|
118
|
+
).toEqual(["favorites", "share"]);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it("drops resolved items that cannot be invoked", () => {
|
|
122
|
+
(selectActionButtons as jest.Mock).mockReturnValue("broken,share");
|
|
123
|
+
|
|
124
|
+
(uiActionsRegistry.getActions as jest.Mock).mockImplementation((name) =>
|
|
125
|
+
name === "broken"
|
|
126
|
+
? [{ identifier: "broken" } as any]
|
|
127
|
+
: [action(name as string)]
|
|
128
|
+
);
|
|
129
|
+
|
|
130
|
+
expect(
|
|
131
|
+
resolvePlayerActions({
|
|
132
|
+
configuration: {},
|
|
133
|
+
entry,
|
|
134
|
+
expandEntryActions,
|
|
135
|
+
}).map((item) => item.identifier)
|
|
136
|
+
).toEqual(["share"]);
|
|
137
|
+
});
|
|
138
|
+
});
|
package/playerUtils/index.ts
CHANGED
|
@@ -7,7 +7,23 @@ import { isTV } from "@applicaster/zapp-react-native-utils/reactUtils";
|
|
|
7
7
|
import { getBoolFromConfigValue } from "../configurationUtils";
|
|
8
8
|
import { Dimensions } from "react-native";
|
|
9
9
|
|
|
10
|
-
export {
|
|
10
|
+
export { resolvePlayerActions } from "./resolvePlayerActions";
|
|
11
|
+
|
|
12
|
+
export { isEntryAction } from "../entryActions";
|
|
13
|
+
|
|
14
|
+
export {
|
|
15
|
+
parseActionIdentifiers,
|
|
16
|
+
resolveActionIdentifiers,
|
|
17
|
+
} from "../uiActionsRegistrator";
|
|
18
|
+
|
|
19
|
+
export type { ResolveActionIdentifiersParams } from "../uiActionsRegistrator";
|
|
20
|
+
|
|
21
|
+
export type {
|
|
22
|
+
ActionButtonDescriptor,
|
|
23
|
+
OverflowDescriptor,
|
|
24
|
+
PlayerControlDescriptor,
|
|
25
|
+
ResolvePlayerActionsParams,
|
|
26
|
+
} from "./resolvePlayerActions";
|
|
11
27
|
|
|
12
28
|
export {
|
|
13
29
|
resolvePlayerContentText,
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { selectActionButtons } from "../conf/player/selectors";
|
|
2
|
+
import {
|
|
3
|
+
usableActionItems,
|
|
4
|
+
RegisteredAction,
|
|
5
|
+
resolveActionIdentifiers,
|
|
6
|
+
} from "../uiActionsRegistrator";
|
|
7
|
+
|
|
8
|
+
export type ResolvePlayerActionsParams = {
|
|
9
|
+
configuration: any;
|
|
10
|
+
entry: ZappEntry | ZappFeed;
|
|
11
|
+
/** Expands the `entry_actions` token; see `resolveActionIdentifiers`. */
|
|
12
|
+
expandEntryActions?: (entry: ZappEntry | ZappFeed) => RegisteredAction[];
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* The player's action buttons, resolved against the entry.
|
|
17
|
+
*
|
|
18
|
+
* Every `player_action_buttons` token goes through `uiActionsRegistry`, except
|
|
19
|
+
* `entry_actions`, which the caller expands from the entry itself. The list is
|
|
20
|
+
* not capped; the overlay decides how many fit and sends the rest to the sheet.
|
|
21
|
+
*/
|
|
22
|
+
export function resolvePlayerActions({
|
|
23
|
+
configuration,
|
|
24
|
+
entry,
|
|
25
|
+
expandEntryActions,
|
|
26
|
+
}: ResolvePlayerActionsParams): RegisteredAction[] {
|
|
27
|
+
const buttonsString = selectActionButtons(configuration);
|
|
28
|
+
|
|
29
|
+
if (!buttonsString) {
|
|
30
|
+
return [];
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// Deduped at the source: `player_action_buttons` is a hand-typed string, so
|
|
34
|
+
// the same identifier can legitimately appear twice. Left alone, a repeat
|
|
35
|
+
// would be counted twice against the inline limit, rendered twice on the
|
|
36
|
+
// overlay, and collide as a React key. Items with no `invokeAction` are
|
|
37
|
+
// dropped here too - they can never do anything.
|
|
38
|
+
return usableActionItems(
|
|
39
|
+
resolveActionIdentifiers({
|
|
40
|
+
identifiers: buttonsString,
|
|
41
|
+
entry,
|
|
42
|
+
expandEntryActions,
|
|
43
|
+
})
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** One control on the player overlay, before overflow is applied. */
|
|
48
|
+
export type ActionButtonDescriptor =
|
|
49
|
+
| { kind: "subtitle" }
|
|
50
|
+
| { kind: "close" }
|
|
51
|
+
| { kind: "airplay" }
|
|
52
|
+
| { kind: "action"; action: RegisteredAction };
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* The Overflow control, produced by `splitOverflow` when more content actions
|
|
56
|
+
* resolve than fit on the overlay.
|
|
57
|
+
*/
|
|
58
|
+
export type OverflowDescriptor = {
|
|
59
|
+
kind: "overflow";
|
|
60
|
+
/**
|
|
61
|
+
* Every content action, the overlay companion included. An overlay button
|
|
62
|
+
* renders nothing when its action has no icon, so the companion needs the
|
|
63
|
+
* sheet as a second home or it could be reachable from nowhere.
|
|
64
|
+
*/
|
|
65
|
+
additionalActions: RegisteredAction[];
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
/** What the overlay actually renders, once overflow has been applied. */
|
|
69
|
+
export type PlayerControlDescriptor =
|
|
70
|
+
| ActionButtonDescriptor
|
|
71
|
+
| OverflowDescriptor;
|
|
@@ -6,6 +6,7 @@ import { ActionsContext } from "@applicaster/zapp-react-native-ui-components/Con
|
|
|
6
6
|
import {
|
|
7
7
|
observeEntryState,
|
|
8
8
|
RegisteredActionValue,
|
|
9
|
+
uiActionsRegistry,
|
|
9
10
|
} from "../../uiActionsRegistrator";
|
|
10
11
|
import { reactHooksLogger } from "../logger";
|
|
11
12
|
|
|
@@ -34,7 +35,13 @@ export function useActions<T = unknown>(plugId: string): any {
|
|
|
34
35
|
if (!logged) {
|
|
35
36
|
logger.warning({
|
|
36
37
|
message: `useActions: Couldn't find an action for ${plugId} plugin`,
|
|
37
|
-
|
|
38
|
+
// Identifiers only. The action map holds every plugin's module and
|
|
39
|
+
// live React context, which is far too large for a log line and can
|
|
40
|
+
// carry whatever state those plugins hold.
|
|
41
|
+
data: {
|
|
42
|
+
plugId,
|
|
43
|
+
availableActions: Object.keys(context?.actions ?? {}),
|
|
44
|
+
},
|
|
38
45
|
});
|
|
39
46
|
|
|
40
47
|
setLogged(true);
|
|
@@ -80,7 +87,31 @@ export function useEntryActionState(
|
|
|
80
87
|
useEffect(() => {
|
|
81
88
|
if (!action) return undefined;
|
|
82
89
|
|
|
83
|
-
|
|
90
|
+
// An action's stream is third-party code. Subscribing with only a `next`
|
|
91
|
+
// handler lets RxJS rethrow an error notification asynchronously, where no
|
|
92
|
+
// error boundary catches it - one plugin faulting would take the app down
|
|
93
|
+
// rather than this one button. The button keeps its last known state.
|
|
94
|
+
let subscription;
|
|
95
|
+
|
|
96
|
+
try {
|
|
97
|
+
subscription = observeEntryState(action, entry).subscribe({
|
|
98
|
+
next: setState,
|
|
99
|
+
error: (error) =>
|
|
100
|
+
logger.warning({
|
|
101
|
+
message:
|
|
102
|
+
"useEntryActionState: the action's state stream failed - the button keeps its last state",
|
|
103
|
+
data: { entryId, error },
|
|
104
|
+
}),
|
|
105
|
+
});
|
|
106
|
+
} catch (error) {
|
|
107
|
+
logger.warning({
|
|
108
|
+
message:
|
|
109
|
+
"useEntryActionState: could not observe the action - the button keeps its initial state",
|
|
110
|
+
data: { entryId, error },
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
return undefined;
|
|
114
|
+
}
|
|
84
115
|
|
|
85
116
|
return () => subscription.unsubscribe();
|
|
86
117
|
// eslint-disable-next-line @wogns3623/better-exhaustive-deps/exhaustive-deps
|
|
@@ -88,6 +119,18 @@ export function useEntryActionState(
|
|
|
88
119
|
|
|
89
120
|
const invokeAction = useCallback(
|
|
90
121
|
(options: InvokeArgsOptions = {}) => {
|
|
122
|
+
if (typeof action?.invokeAction !== "function") {
|
|
123
|
+
// A button was rendered and pressed, and the press goes nowhere. Only
|
|
124
|
+
// reachable on a user interaction, so it costs nothing on render. The
|
|
125
|
+
// action value carries no identifier, so the label is what locates the
|
|
126
|
+
// button that misbehaved.
|
|
127
|
+
logger.warning({
|
|
128
|
+
message:
|
|
129
|
+
"useEntryActionState: the pressed action has no invokeAction - the press did nothing",
|
|
130
|
+
data: { entryId, label: state?.label },
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
|
|
91
134
|
action?.invokeAction?.(entry, { updateState: setState, ...options });
|
|
92
135
|
},
|
|
93
136
|
// eslint-disable-next-line @wogns3623/better-exhaustive-deps/exhaustive-deps
|
|
@@ -96,3 +139,25 @@ export function useEntryActionState(
|
|
|
96
139
|
|
|
97
140
|
return { state, invokeAction };
|
|
98
141
|
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* A value that changes whenever a provider is added to or removed from
|
|
145
|
+
* `uiActionsRegistry`.
|
|
146
|
+
*
|
|
147
|
+
* Providers can register after the UI that wants them has rendered - the audio
|
|
148
|
+
* player registers playback speed only once its controller has state, and
|
|
149
|
+
* plugin-backed actions are bridged in from an effect. Put this in the
|
|
150
|
+
* dependency list of anything that resolves actions, so the list is re-read
|
|
151
|
+
* instead of being frozen at first paint.
|
|
152
|
+
*/
|
|
153
|
+
export function useRegistryRevision(): number {
|
|
154
|
+
const [revision, setRevision] = useState(0);
|
|
155
|
+
|
|
156
|
+
useEffect(
|
|
157
|
+
() =>
|
|
158
|
+
uiActionsRegistry.subscribe(() => setRevision((current) => current + 1)),
|
|
159
|
+
[]
|
|
160
|
+
);
|
|
161
|
+
|
|
162
|
+
return revision;
|
|
163
|
+
}
|
|
@@ -7,6 +7,7 @@ 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}>
|
|
@@ -188,6 +203,39 @@ describe("navigation", () => {
|
|
|
188
203
|
});
|
|
189
204
|
});
|
|
190
205
|
|
|
206
|
+
describe("Modal components", () => {
|
|
207
|
+
beforeEach(() => {
|
|
208
|
+
modalStore.getState().openModal({
|
|
209
|
+
item: rivers["river-general"] as any,
|
|
210
|
+
props: { entry: modalEntry },
|
|
211
|
+
});
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
afterEach(() => {
|
|
215
|
+
modalStore.getState().dismissModal();
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
it("should return a correct pathname", () => {
|
|
219
|
+
const { result } = renderHook(() => useRoute(), {
|
|
220
|
+
wrapper: modalWrapper,
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
expect(result.current.pathname).toEqual(modalPathname);
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
it("should return correct screenData information", () => {
|
|
227
|
+
const { result } = renderHook(() => useRoute(), {
|
|
228
|
+
wrapper: modalWrapper,
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
expect(result.current.screenData.id).toEqual(modalEntry.id);
|
|
232
|
+
|
|
233
|
+
expect(result.current.screenData.targetScreen.id).toEqual(
|
|
234
|
+
rivers["river-general"].id
|
|
235
|
+
);
|
|
236
|
+
});
|
|
237
|
+
});
|
|
238
|
+
|
|
191
239
|
describe("Hook modal components", () => {
|
|
192
240
|
it("should return a correct pathname", () => {
|
|
193
241
|
const { result } = renderHook(() => useRoute(), {
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import { modalStore } from "../../../modalState";
|
|
2
|
+
import { isWeb } from "../../../reactUtils";
|
|
3
|
+
import { presentScreen } from "../usePresentScreen";
|
|
4
|
+
|
|
5
|
+
jest.mock("../../../reactUtils", () => ({
|
|
6
|
+
...jest.requireActual("../../../reactUtils"),
|
|
7
|
+
isWeb: jest.fn(() => false),
|
|
8
|
+
}));
|
|
9
|
+
|
|
10
|
+
const river = { id: "river-parent-lock", type: "parent_lock" } as any;
|
|
11
|
+
|
|
12
|
+
const rivers = { "river-parent-lock": river };
|
|
13
|
+
|
|
14
|
+
const contentTypes = { parent_lock: { screen_id: "river-parent-lock" } };
|
|
15
|
+
|
|
16
|
+
const present = (args = {}) =>
|
|
17
|
+
presentScreen({ typeMapping: "parent_lock", rivers, contentTypes, ...args });
|
|
18
|
+
|
|
19
|
+
const modalState = () => modalStore.getState().modalState;
|
|
20
|
+
|
|
21
|
+
const screenCallback = () => modalState().props?.resultCallback as hookCallback;
|
|
22
|
+
|
|
23
|
+
describe("presentScreen", () => {
|
|
24
|
+
beforeEach(() => {
|
|
25
|
+
jest.clearAllMocks();
|
|
26
|
+
(isWeb as jest.Mock).mockReturnValue(false);
|
|
27
|
+
modalStore.getState().dismissModal();
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it("opens a modal with the river resolved from typeMapping", () => {
|
|
31
|
+
void present();
|
|
32
|
+
|
|
33
|
+
expect(modalState().visible).toBe(true);
|
|
34
|
+
expect(modalState().screen).toBe(river);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it("resolves with success when the presented screen reports success", async () => {
|
|
38
|
+
const result = present();
|
|
39
|
+
|
|
40
|
+
screenCallback()({ success: true, payload: { verified: true } });
|
|
41
|
+
|
|
42
|
+
await expect(result).resolves.toMatchObject({
|
|
43
|
+
success: true,
|
|
44
|
+
cancelled: false,
|
|
45
|
+
payload: { verified: true },
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it("dismisses the modal once the presented screen reports a result", async () => {
|
|
50
|
+
const result = present();
|
|
51
|
+
|
|
52
|
+
screenCallback()({ success: true, payload: {} });
|
|
53
|
+
await result;
|
|
54
|
+
|
|
55
|
+
expect(modalState().visible).toBe(false);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it("resolves as cancelled when the modal is dismissed without a result", async () => {
|
|
59
|
+
const result = present();
|
|
60
|
+
|
|
61
|
+
// What the hardware back button does: RN Modal's onRequestClose resets modal state.
|
|
62
|
+
modalStore.getState().dismissModal();
|
|
63
|
+
|
|
64
|
+
await expect(result).resolves.toMatchObject({
|
|
65
|
+
success: false,
|
|
66
|
+
cancelled: true,
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it("resolves as cancelled when the presented screen reports cancellation", async () => {
|
|
71
|
+
const result = present();
|
|
72
|
+
|
|
73
|
+
screenCallback()({ success: false, payload: {}, cancelled: true });
|
|
74
|
+
|
|
75
|
+
await expect(result).resolves.toMatchObject({
|
|
76
|
+
success: false,
|
|
77
|
+
cancelled: true,
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it("resolves as cancelled when another modal replaces the presented screen", async () => {
|
|
82
|
+
const result = present();
|
|
83
|
+
|
|
84
|
+
modalStore.getState().openModal({ item: { id: "other-modal" } as any });
|
|
85
|
+
|
|
86
|
+
await expect(result).resolves.toMatchObject({ cancelled: true });
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it("leaves the modal that replaced the presented screen open", async () => {
|
|
90
|
+
const result = present();
|
|
91
|
+
const other = { id: "other-modal" } as any;
|
|
92
|
+
|
|
93
|
+
modalStore.getState().openModal({ item: other });
|
|
94
|
+
await result;
|
|
95
|
+
|
|
96
|
+
expect(modalState().screen).toBe(other);
|
|
97
|
+
expect(modalState().visible).toBe(true);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it("ignores a result reported after the modal was already cancelled", async () => {
|
|
101
|
+
const result = present();
|
|
102
|
+
const callback = screenCallback();
|
|
103
|
+
|
|
104
|
+
modalStore.getState().dismissModal();
|
|
105
|
+
callback({ success: true, payload: {} });
|
|
106
|
+
|
|
107
|
+
await expect(result).resolves.toMatchObject({
|
|
108
|
+
success: false,
|
|
109
|
+
cancelled: true,
|
|
110
|
+
});
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it("hands the entry to the ModalProvider so it reaches ScreenDataContext", () => {
|
|
114
|
+
const entry = { id: "entry-1", type: { value: "video" } } as any;
|
|
115
|
+
|
|
116
|
+
void present({ entry });
|
|
117
|
+
|
|
118
|
+
expect(modalState().props?.entry).toBe(entry);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it("omits the entry when the screen is presented without one", () => {
|
|
122
|
+
void present();
|
|
123
|
+
|
|
124
|
+
expect(modalState().props).not.toHaveProperty("entry");
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it("fails without opening a modal when typeMapping has no content type", async () => {
|
|
128
|
+
const result = await present({ typeMapping: "unmapped" });
|
|
129
|
+
|
|
130
|
+
expect(result).toMatchObject({ success: false, cancelled: false });
|
|
131
|
+
expect(result.error).toEqual(expect.any(String));
|
|
132
|
+
expect(modalState().visible).toBe(false);
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
it("fails without opening a modal when the content type resolves to a missing river", async () => {
|
|
136
|
+
const result = await present({
|
|
137
|
+
contentTypes: { parent_lock: { screen_id: "gone" } },
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
expect(result).toMatchObject({ success: false, cancelled: false });
|
|
141
|
+
expect(result.error).toEqual(expect.any(String));
|
|
142
|
+
expect(modalState().visible).toBe(false);
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
it("fails without opening a modal on web, where no modal host exists", async () => {
|
|
146
|
+
(isWeb as jest.Mock).mockReturnValue(true);
|
|
147
|
+
|
|
148
|
+
const result = await present();
|
|
149
|
+
|
|
150
|
+
expect(result).toMatchObject({ success: false, cancelled: false });
|
|
151
|
+
expect(result.error).toEqual(expect.any(String));
|
|
152
|
+
expect(modalState().visible).toBe(false);
|
|
153
|
+
});
|
|
154
|
+
});
|
|
@@ -22,6 +22,8 @@ import { useLayoutVersion } from "../layout";
|
|
|
22
22
|
|
|
23
23
|
export { useNavigation } from "./useNavigation";
|
|
24
24
|
|
|
25
|
+
export { usePresentScreen, presentScreen } from "./usePresentScreen";
|
|
26
|
+
|
|
25
27
|
export { useRoute } from "./useRoute";
|
|
26
28
|
|
|
27
29
|
export { usePathname } from "./usePathname";
|