@applicaster/zapp-react-native-utils 15.0.0-alpha.6351072502 → 15.0.0-alpha.6423012104
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/README.md +0 -6
- package/actionsExecutor/ActionExecutorContext.tsx +44 -1
- package/appUtils/HooksManager/index.ts +35 -0
- package/appUtils/focusManager/treeDataStructure/Tree/__tests__/Tree.test.js +46 -0
- package/appUtils/focusManager/treeDataStructure/Tree/index.js +18 -18
- package/appUtils/focusManagerAux/utils/index.ts +2 -4
- package/appUtils/focusManagerAux/utils/utils.ios.ts +6 -3
- package/appUtils/playerManager/playerNative.ts +2 -1
- package/manifestUtils/defaultManifestConfigurations/player.js +0 -16
- package/manifestUtils/platformIsTV.js +13 -0
- package/package.json +2 -2
- package/reactHooks/events/index.ts +20 -0
- package/reactHooks/feed/index.ts +0 -2
- package/reactHooks/index.ts +2 -0
- package/reactHooks/state/__tests__/useComponentScreenState.test.ts +246 -0
- package/reactHooks/state/index.ts +2 -0
- package/reactHooks/state/useComponentScreenState.ts +45 -0
- package/refreshUtils/RefreshCoordinator/__tests__/refreshCoordinator.test.ts +161 -0
- package/refreshUtils/RefreshCoordinator/index.ts +216 -0
- package/refreshUtils/RefreshCoordinator/utils/__tests__/getDataRefreshConfig.test.ts +104 -0
- package/refreshUtils/RefreshCoordinator/utils/index.ts +29 -0
- package/screenPickerUtils/index.ts +5 -0
- package/screenUtils/index.ts +3 -0
- package/utils/__tests__/clone.test.ts +158 -0
- package/utils/__tests__/path.test.ts +7 -0
- package/utils/clone.ts +7 -0
- package/utils/index.ts +2 -1
- package/reactHooks/feed/__tests__/useFeedRefresh.test.tsx +0 -75
- package/reactHooks/feed/useFeedRefresh.tsx +0 -65
package/README.md
CHANGED
|
@@ -245,12 +245,6 @@ const connectionType = useConnectionInfo(true);
|
|
|
245
245
|
|
|
246
246
|
`@applicaster/zapp-react-native/reactHooks`
|
|
247
247
|
|
|
248
|
-
- `useFeedRefresh: ({ reloadData: function, component: { id: boolean | string, rules: {enable_data_refreshing: boolean, refreshing_interval: number} } }) => void` - Hook will call `reloadData` function, in the specified intervals if `enable_data_refreshing` is set to true;
|
|
249
|
-
|
|
250
|
-
```javascript
|
|
251
|
-
useFeedRefresh({ reloadData, component });
|
|
252
|
-
```
|
|
253
|
-
|
|
254
248
|
- `useFeedLoader: ({ feedUrl: string, pipesOptions?: { clearCache?: boolean, loadLocalFavourites?: boolean, silentRefresh?: boolean} }) => ({data: ?ApplicasterFeed, loading: boolean, url: string, error: Error,reloadData: (silentRefresh?: boolean) => void, loadNext: () => void})` - Hook will load data to the redux store and return a feed for the provided DSP URL. If the data for the provided url was already loaded, it will return that value
|
|
255
249
|
|
|
256
250
|
```javascript
|
|
@@ -315,6 +315,44 @@ export function withActionExecutor(Component) {
|
|
|
315
315
|
return ActionResult.Error;
|
|
316
316
|
}
|
|
317
317
|
|
|
318
|
+
const navigationAction = action.options?.navigationAction;
|
|
319
|
+
const entrySource = action.options?.entry;
|
|
320
|
+
|
|
321
|
+
const entry = entrySource
|
|
322
|
+
? entrySource === "@{entry/}"
|
|
323
|
+
? context?.entry
|
|
324
|
+
: entrySource
|
|
325
|
+
: null;
|
|
326
|
+
|
|
327
|
+
if (entry) {
|
|
328
|
+
if (typeof entry !== "object") {
|
|
329
|
+
log_error(
|
|
330
|
+
`navigateToScreen: entry option is not an object, entry: ${entry}`
|
|
331
|
+
);
|
|
332
|
+
|
|
333
|
+
return ActionResult.Error;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
log_info(
|
|
337
|
+
`navigateToScreen: navigating to screen type: ${screenType} with entry id: ${entry.id}`
|
|
338
|
+
);
|
|
339
|
+
|
|
340
|
+
const overriddenEntry = {
|
|
341
|
+
...entry,
|
|
342
|
+
type: {
|
|
343
|
+
value: screenType,
|
|
344
|
+
},
|
|
345
|
+
};
|
|
346
|
+
|
|
347
|
+
if (navigationAction === "push") {
|
|
348
|
+
navigator.push(overriddenEntry);
|
|
349
|
+
} else {
|
|
350
|
+
navigator.replace(overriddenEntry);
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
return ActionResult.Success;
|
|
354
|
+
}
|
|
355
|
+
|
|
318
356
|
const screenId = contentTypes?.[screenType]?.screen_id || null;
|
|
319
357
|
|
|
320
358
|
if (!screenId) {
|
|
@@ -334,7 +372,12 @@ export function withActionExecutor(Component) {
|
|
|
334
372
|
}
|
|
335
373
|
|
|
336
374
|
context?.callback?.({ success: false, error: null, abort: true });
|
|
337
|
-
|
|
375
|
+
|
|
376
|
+
if (navigationAction === "push") {
|
|
377
|
+
navigator.push(river);
|
|
378
|
+
} else {
|
|
379
|
+
navigator.replace(river);
|
|
380
|
+
}
|
|
338
381
|
|
|
339
382
|
return ActionResult.Success;
|
|
340
383
|
}
|
|
@@ -11,6 +11,10 @@ import { HOOKS_EVENTS, HOOKS_TYPE } from "./constants";
|
|
|
11
11
|
|
|
12
12
|
import { hooksManagerLogger } from "./logger";
|
|
13
13
|
import { HookManager, HookManagerArgs } from "./types";
|
|
14
|
+
import {
|
|
15
|
+
actionExecutor,
|
|
16
|
+
ActionResult,
|
|
17
|
+
} from "../../actionsExecutor/ActionExecutor";
|
|
14
18
|
|
|
15
19
|
/**
|
|
16
20
|
* orders the hooks according to their weight
|
|
@@ -280,6 +284,8 @@ export function HooksManager({
|
|
|
280
284
|
{}
|
|
281
285
|
);
|
|
282
286
|
|
|
287
|
+
actionExecutor.unregisterAction("finishHook");
|
|
288
|
+
|
|
283
289
|
return;
|
|
284
290
|
}
|
|
285
291
|
|
|
@@ -294,6 +300,8 @@ export function HooksManager({
|
|
|
294
300
|
}
|
|
295
301
|
);
|
|
296
302
|
|
|
303
|
+
actionExecutor.unregisterAction("finishHook");
|
|
304
|
+
|
|
297
305
|
return hookPlugin.setStateAndNotify(HOOKS_EVENTS.ERROR, {
|
|
298
306
|
error,
|
|
299
307
|
hookPlugin,
|
|
@@ -316,6 +324,8 @@ export function HooksManager({
|
|
|
316
324
|
// TODO: Temporary hack to pass getLoginProtocol to other plugins to refresh in case token expired, need be deleted later
|
|
317
325
|
delete payload.getLoginProtocol;
|
|
318
326
|
|
|
327
|
+
actionExecutor.unregisterAction("finishHook");
|
|
328
|
+
|
|
319
329
|
hookPlugin.setStateAndNotify(HOOKS_EVENTS.CANCEL, {
|
|
320
330
|
hookPlugin,
|
|
321
331
|
payload,
|
|
@@ -381,6 +391,27 @@ export function HooksManager({
|
|
|
381
391
|
};
|
|
382
392
|
}
|
|
383
393
|
|
|
394
|
+
function registerFinishHookAction(payload, callback) {
|
|
395
|
+
// Ensure no stale finishHook remains (e.g. presentUI re-entry from runInBackground)
|
|
396
|
+
actionExecutor.unregisterAction("finishHook");
|
|
397
|
+
|
|
398
|
+
actionExecutor.registerAction("finishHook", async (action: ActionType) => {
|
|
399
|
+
const { success, errorMessage, abort } = action.options;
|
|
400
|
+
|
|
401
|
+
actionExecutor.unregisterAction("finishHook");
|
|
402
|
+
|
|
403
|
+
if (errorMessage) {
|
|
404
|
+
callback({ success, error: new Error(errorMessage), payload, abort });
|
|
405
|
+
} else {
|
|
406
|
+
callback({ success, error: null, payload, abort });
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
hooksManagerLogger.info("finishHook action executed, finishing flow");
|
|
410
|
+
|
|
411
|
+
return ActionResult.Success;
|
|
412
|
+
});
|
|
413
|
+
}
|
|
414
|
+
|
|
384
415
|
/**
|
|
385
416
|
* presents a screen hook by triggering an event invoking the handler with
|
|
386
417
|
* the appropriate route & payload
|
|
@@ -408,6 +439,8 @@ export function HooksManager({
|
|
|
408
439
|
}
|
|
409
440
|
);
|
|
410
441
|
|
|
442
|
+
registerFinishHookAction(payload, callback);
|
|
443
|
+
|
|
411
444
|
hookPlugin.setStateAndNotify(HOOKS_EVENTS.PRESENT_SCREEN_HOOK, {
|
|
412
445
|
hookPlugin,
|
|
413
446
|
route: targetScreenRoute,
|
|
@@ -467,6 +500,8 @@ export function HooksManager({
|
|
|
467
500
|
}
|
|
468
501
|
);
|
|
469
502
|
|
|
503
|
+
registerFinishHookAction(payload, callback);
|
|
504
|
+
|
|
470
505
|
hookPlugin.module.runInBackground(
|
|
471
506
|
payload,
|
|
472
507
|
callback,
|
|
@@ -373,3 +373,49 @@ describe("addNode", () => {
|
|
|
373
373
|
checkParents(tree.root);
|
|
374
374
|
});
|
|
375
375
|
});
|
|
376
|
+
|
|
377
|
+
describe("findInTree", () => {
|
|
378
|
+
function createNode(id, children) {
|
|
379
|
+
return {
|
|
380
|
+
id,
|
|
381
|
+
children,
|
|
382
|
+
};
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
it("returns a direct child match from root children", () => {
|
|
386
|
+
const tree = new Tree(treeLoaded);
|
|
387
|
+
const direct = createNode("direct-node");
|
|
388
|
+
|
|
389
|
+
tree.root.children = [direct];
|
|
390
|
+
|
|
391
|
+
expect(tree.findInTree("direct-node")).toEqual(direct);
|
|
392
|
+
});
|
|
393
|
+
|
|
394
|
+
it("returns a nested descendant match", () => {
|
|
395
|
+
const tree = new Tree(treeLoaded);
|
|
396
|
+
const nested = createNode("nested-node");
|
|
397
|
+
const intermediate = createNode("intermediate-node", [nested]);
|
|
398
|
+
const rootNode = createNode("root-node", [intermediate]);
|
|
399
|
+
|
|
400
|
+
tree.root.children = [rootNode];
|
|
401
|
+
|
|
402
|
+
expect(tree.findInTree("nested-node")).toEqual(nested);
|
|
403
|
+
});
|
|
404
|
+
|
|
405
|
+
it("returns null when node id does not exist", () => {
|
|
406
|
+
const tree = new Tree(treeLoaded);
|
|
407
|
+
const leaf = createNode("leaf-node");
|
|
408
|
+
const rootNode = createNode("root-node", [leaf]);
|
|
409
|
+
|
|
410
|
+
tree.root.children = [rootNode];
|
|
411
|
+
|
|
412
|
+
expect(tree.findInTree("missing-node")).toEqual(null);
|
|
413
|
+
});
|
|
414
|
+
|
|
415
|
+
it("returns null when tree has no children", () => {
|
|
416
|
+
const tree = new Tree(treeLoaded);
|
|
417
|
+
tree.root.children = null;
|
|
418
|
+
|
|
419
|
+
expect(tree.findInTree("any-node")).toEqual(null);
|
|
420
|
+
});
|
|
421
|
+
});
|
|
@@ -205,31 +205,31 @@ export class Tree {
|
|
|
205
205
|
* @returns founded node or null
|
|
206
206
|
*/
|
|
207
207
|
findInTree(id) {
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
return this.findInArray(id, this.root.children, retVal);
|
|
208
|
+
return this.findInArray(id, this.root.children);
|
|
211
209
|
}
|
|
212
210
|
|
|
213
|
-
findInArray(id, children
|
|
214
|
-
if (!
|
|
215
|
-
|
|
211
|
+
findInArray(id, children) {
|
|
212
|
+
if (!children) {
|
|
213
|
+
return null;
|
|
214
|
+
}
|
|
216
215
|
|
|
217
|
-
|
|
218
|
-
children.forEach((child) => {
|
|
219
|
-
if (child.children) {
|
|
220
|
-
retVal = this.findInArray(id, child.children, retVal);
|
|
216
|
+
const directMatch = children.find((obj) => obj.id === id);
|
|
221
217
|
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
218
|
+
if (directMatch) {
|
|
219
|
+
return directMatch;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
for (const child of children) {
|
|
223
|
+
if (child.children) {
|
|
224
|
+
const nestedMatch = this.findInArray(id, child.children);
|
|
225
|
+
|
|
226
|
+
if (nestedMatch) {
|
|
227
|
+
return nestedMatch;
|
|
228
|
+
}
|
|
229
229
|
}
|
|
230
230
|
}
|
|
231
231
|
|
|
232
|
-
return
|
|
232
|
+
return null;
|
|
233
233
|
}
|
|
234
234
|
|
|
235
235
|
/**
|
|
@@ -13,7 +13,7 @@ import {
|
|
|
13
13
|
|
|
14
14
|
import {
|
|
15
15
|
getFocusableId,
|
|
16
|
-
|
|
16
|
+
isTabsScreenContentContainerId,
|
|
17
17
|
} from "@applicaster/zapp-react-native-utils/screenPickerUtils";
|
|
18
18
|
|
|
19
19
|
// run check each 300 ms
|
|
@@ -24,8 +24,6 @@ const isTopMenu = (node) => startsWith(QUICK_BRICK_NAVBAR, node?.id);
|
|
|
24
24
|
const isContent = (node) => startsWith(QUICK_BRICK_CONTENT, node?.id);
|
|
25
25
|
const isRoot = (node) => node?.id === "root";
|
|
26
26
|
|
|
27
|
-
const isScrenPicker = (node) => startsWith(SCREEN_PICKER_CONTAINER, node?.id);
|
|
28
|
-
|
|
29
27
|
type Props = {
|
|
30
28
|
maxTimeout: number;
|
|
31
29
|
conditionFn: () => boolean;
|
|
@@ -136,7 +134,7 @@ export const isTabsScreenOnContentFocused = (node) => {
|
|
|
136
134
|
return false;
|
|
137
135
|
}
|
|
138
136
|
|
|
139
|
-
if (
|
|
137
|
+
if (isTabsScreenContentContainerId(node?.id)) {
|
|
140
138
|
return true;
|
|
141
139
|
}
|
|
142
140
|
|
|
@@ -106,9 +106,8 @@ const focusableNativeViewRegistration = ({ focusableView, focusableGroup }) => {
|
|
|
106
106
|
);
|
|
107
107
|
};
|
|
108
108
|
|
|
109
|
-
export const
|
|
109
|
+
export const firstFocusableViewInContentRegistrationFactory = () =>
|
|
110
110
|
focusableViewRegistrationSubject$.pipe(
|
|
111
|
-
take(1), // we care about only first FocusableView registration
|
|
112
111
|
switchMap((focusableView) =>
|
|
113
112
|
// start waiting registration of its parent FocusableGroup
|
|
114
113
|
focusableGroupRegistrationSubject$.pipe(
|
|
@@ -126,7 +125,11 @@ export const firstFocusableViewRegistrationFactory = () =>
|
|
|
126
125
|
focusableView,
|
|
127
126
|
focusableGroup,
|
|
128
127
|
})
|
|
129
|
-
)
|
|
128
|
+
),
|
|
129
|
+
filter(({ focusableView }) =>
|
|
130
|
+
isPartOfContent(focusManager.focusableTree, focusableView.id)
|
|
131
|
+
),
|
|
132
|
+
take(1) // we care about only first FocusableView registration
|
|
130
133
|
);
|
|
131
134
|
|
|
132
135
|
// registration on RN level(into RN focusManager)
|
|
@@ -229,8 +229,9 @@ export class PlayerNative extends Player {
|
|
|
229
229
|
};
|
|
230
230
|
|
|
231
231
|
closeNativePlayer = () => {
|
|
232
|
-
// TODO: Delete does not work
|
|
232
|
+
// TODO: Delete, does not work (component is null)
|
|
233
233
|
this.currentPlayerComponent()?.closeNativePlayer?.();
|
|
234
|
+
this.getPlayerModule()?.stopBackgroundPlayback?.();
|
|
234
235
|
};
|
|
235
236
|
|
|
236
237
|
togglePlayPause = () => {
|
|
@@ -707,22 +707,6 @@ function getPlayerConfiguration({ platform, version }) {
|
|
|
707
707
|
}
|
|
708
708
|
|
|
709
709
|
if (isMobile(platform)) {
|
|
710
|
-
localizations.fields.push(
|
|
711
|
-
{
|
|
712
|
-
type: "text_input",
|
|
713
|
-
label: "Restrict playback on mobile networks alert title",
|
|
714
|
-
key: "mobile_connection_restricted_alert_title",
|
|
715
|
-
initial_value: "Restricted Connection Type",
|
|
716
|
-
},
|
|
717
|
-
{
|
|
718
|
-
type: "text_input",
|
|
719
|
-
label: "Restrict playback on mobile networks alert message",
|
|
720
|
-
key: "mobile_connection_restricted_alert_message",
|
|
721
|
-
initial_value:
|
|
722
|
-
"This content can only be viewed over a Wi-Fi or LAN network.",
|
|
723
|
-
}
|
|
724
|
-
);
|
|
725
|
-
|
|
726
710
|
general.fields.push(
|
|
727
711
|
{
|
|
728
712
|
section: "Default Timestamp Type",
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@applicaster/zapp-react-native-utils",
|
|
3
|
-
"version": "15.0.0-alpha.
|
|
3
|
+
"version": "15.0.0-alpha.6423012104",
|
|
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.0.0-alpha.
|
|
30
|
+
"@applicaster/applicaster-types": "15.0.0-alpha.6423012104",
|
|
31
31
|
"buffer": "^5.2.1",
|
|
32
32
|
"camelize": "^1.0.0",
|
|
33
33
|
"dayjs": "^1.11.10",
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import { scrollEndReached$ } from "@applicaster/zapp-react-native-ui-components/events";
|
|
3
|
+
import { filter } from "rxjs/operators";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Subscribe to scroll-end-reached and call loadNextData only when this item is last in the river.
|
|
7
|
+
* Use in Grid/List so they load next page when the user scrolls to the end (river or tabs scroll container).
|
|
8
|
+
*/
|
|
9
|
+
export function useScrollEndReachedLoadNext(
|
|
10
|
+
isLast: boolean,
|
|
11
|
+
loadNextData: () => void
|
|
12
|
+
): void {
|
|
13
|
+
React.useEffect(() => {
|
|
14
|
+
const sub = scrollEndReached$.pipe(filter(() => isLast)).subscribe(() => {
|
|
15
|
+
loadNextData();
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
return () => sub.unsubscribe();
|
|
19
|
+
}, [isLast, loadNextData]);
|
|
20
|
+
}
|
package/reactHooks/feed/index.ts
CHANGED
package/reactHooks/index.ts
CHANGED
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
import { renderHook, act } from "@testing-library/react-native";
|
|
2
|
+
import { useComponentScreenState } from "../useComponentScreenState";
|
|
3
|
+
import { useScreenContextV2 } from "@applicaster/zapp-react-native-utils/reactHooks/screen/useScreenContext";
|
|
4
|
+
|
|
5
|
+
import { create } from "zustand";
|
|
6
|
+
|
|
7
|
+
const createMockStore = () => create(() => ({}));
|
|
8
|
+
|
|
9
|
+
// Mock the external dependencies
|
|
10
|
+
jest.mock(
|
|
11
|
+
"@applicaster/zapp-react-native-utils/reactHooks/screen/useScreenContext",
|
|
12
|
+
() => ({
|
|
13
|
+
useScreenContextV2: jest.fn(),
|
|
14
|
+
})
|
|
15
|
+
);
|
|
16
|
+
|
|
17
|
+
const mockUseScreenContextV2 = useScreenContextV2 as jest.MockedFunction<
|
|
18
|
+
typeof useScreenContextV2
|
|
19
|
+
>;
|
|
20
|
+
|
|
21
|
+
describe("useComponentScreenState", () => {
|
|
22
|
+
beforeEach(() => {
|
|
23
|
+
jest.clearAllMocks();
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it("should return initial value when no state exists for componentId", () => {
|
|
27
|
+
const mockStore = createMockStore();
|
|
28
|
+
|
|
29
|
+
mockUseScreenContextV2.mockReturnValue({
|
|
30
|
+
_componentStateStore: mockStore,
|
|
31
|
+
} as any);
|
|
32
|
+
|
|
33
|
+
const initialValue = "default-value";
|
|
34
|
+
const componentId = "test-component";
|
|
35
|
+
|
|
36
|
+
const { result } = renderHook(() =>
|
|
37
|
+
useComponentScreenState(componentId, initialValue)
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
expect(result.current[0]).toBe(initialValue);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it("should return stored value when state exists for componentId", () => {
|
|
44
|
+
const mockStore = createMockStore();
|
|
45
|
+
mockStore.setState({ "existing-component": "stored-value" });
|
|
46
|
+
|
|
47
|
+
mockUseScreenContextV2.mockReturnValue({
|
|
48
|
+
_componentStateStore: mockStore,
|
|
49
|
+
} as any);
|
|
50
|
+
|
|
51
|
+
const { result } = renderHook(() =>
|
|
52
|
+
useComponentScreenState("existing-component", "default")
|
|
53
|
+
);
|
|
54
|
+
|
|
55
|
+
expect(result.current[0]).toBe("stored-value");
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it("should update state when setter is called", () => {
|
|
59
|
+
const mockStore = createMockStore();
|
|
60
|
+
|
|
61
|
+
mockUseScreenContextV2.mockReturnValue({
|
|
62
|
+
_componentStateStore: mockStore,
|
|
63
|
+
} as any);
|
|
64
|
+
|
|
65
|
+
const { result } = renderHook(() =>
|
|
66
|
+
useComponentScreenState("update-test", "initial")
|
|
67
|
+
);
|
|
68
|
+
|
|
69
|
+
// Verify initial value
|
|
70
|
+
expect(result.current[0]).toBe("initial");
|
|
71
|
+
|
|
72
|
+
// Update the value
|
|
73
|
+
act(() => {
|
|
74
|
+
result.current[1]("updated-value");
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
// Verify updated value
|
|
78
|
+
expect(result.current[0]).toBe("updated-value");
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it("should handle different data types correctly", () => {
|
|
82
|
+
const mockStore = createMockStore();
|
|
83
|
+
|
|
84
|
+
mockUseScreenContextV2.mockReturnValue({
|
|
85
|
+
_componentStateStore: mockStore,
|
|
86
|
+
} as any);
|
|
87
|
+
|
|
88
|
+
// Test with number
|
|
89
|
+
const { result: numberResult } = renderHook(() =>
|
|
90
|
+
useComponentScreenState<number>("number-test", 42)
|
|
91
|
+
);
|
|
92
|
+
|
|
93
|
+
expect(numberResult.current[0]).toBe(42);
|
|
94
|
+
|
|
95
|
+
// Test with boolean
|
|
96
|
+
const { result: boolResult } = renderHook(() =>
|
|
97
|
+
useComponentScreenState<boolean>("bool-test", true)
|
|
98
|
+
);
|
|
99
|
+
|
|
100
|
+
expect(boolResult.current[0]).toBe(true);
|
|
101
|
+
|
|
102
|
+
// Test with object
|
|
103
|
+
const testObj = { key: "value" };
|
|
104
|
+
|
|
105
|
+
const { result: objResult } = renderHook(() =>
|
|
106
|
+
useComponentScreenState<{ key: string }>("obj-test", testObj)
|
|
107
|
+
);
|
|
108
|
+
|
|
109
|
+
expect(objResult.current[0]).toEqual(testObj);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it("should maintain separate states for different componentIds", () => {
|
|
113
|
+
const mockStore = createMockStore();
|
|
114
|
+
|
|
115
|
+
mockUseScreenContextV2.mockReturnValue({
|
|
116
|
+
_componentStateStore: mockStore,
|
|
117
|
+
} as any);
|
|
118
|
+
|
|
119
|
+
const { result: firstResult } = renderHook(() =>
|
|
120
|
+
useComponentScreenState("first", "first-initial")
|
|
121
|
+
);
|
|
122
|
+
|
|
123
|
+
const { result: secondResult } = renderHook(() =>
|
|
124
|
+
useComponentScreenState("second", "second-initial")
|
|
125
|
+
);
|
|
126
|
+
|
|
127
|
+
// Verify initial values are separate
|
|
128
|
+
expect(firstResult.current[0]).toBe("first-initial");
|
|
129
|
+
expect(secondResult.current[0]).toBe("second-initial");
|
|
130
|
+
|
|
131
|
+
// Update first component's state
|
|
132
|
+
act(() => {
|
|
133
|
+
firstResult.current[1]("first-updated");
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
// Verify only first component's state changed
|
|
137
|
+
expect(firstResult.current[0]).toBe("first-updated");
|
|
138
|
+
expect(secondResult.current[0]).toBe("second-initial");
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
it("should return a memoized setter function", () => {
|
|
142
|
+
const mockStore = createMockStore();
|
|
143
|
+
|
|
144
|
+
mockUseScreenContextV2.mockReturnValue({
|
|
145
|
+
_componentStateStore: mockStore,
|
|
146
|
+
} as any);
|
|
147
|
+
|
|
148
|
+
const { result, rerender } = renderHook(
|
|
149
|
+
({ id, initial }) => useComponentScreenState(id, initial),
|
|
150
|
+
{
|
|
151
|
+
initialProps: { id: "memo-test", initial: "initial" },
|
|
152
|
+
}
|
|
153
|
+
);
|
|
154
|
+
|
|
155
|
+
const firstSetter = result.current[1];
|
|
156
|
+
|
|
157
|
+
// Rerender with same props
|
|
158
|
+
rerender({ id: "memo-test", initial: "initial" });
|
|
159
|
+
|
|
160
|
+
const secondSetter = result.current[1];
|
|
161
|
+
|
|
162
|
+
// Setter should be the same instance due to useCallback
|
|
163
|
+
expect(firstSetter).toBe(secondSetter);
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
it("should update setter when componentId changes", () => {
|
|
167
|
+
const mockStore = createMockStore();
|
|
168
|
+
|
|
169
|
+
mockUseScreenContextV2.mockReturnValue({
|
|
170
|
+
_componentStateStore: mockStore,
|
|
171
|
+
} as any);
|
|
172
|
+
|
|
173
|
+
const { result, rerender } = renderHook(
|
|
174
|
+
({ id, initial }) => useComponentScreenState(id, initial),
|
|
175
|
+
{
|
|
176
|
+
initialProps: { id: "old-id", initial: "initial" },
|
|
177
|
+
}
|
|
178
|
+
);
|
|
179
|
+
|
|
180
|
+
const firstSetter = result.current[1];
|
|
181
|
+
|
|
182
|
+
// Rerender with different componentId
|
|
183
|
+
rerender({ id: "new-id", initial: "initial" });
|
|
184
|
+
|
|
185
|
+
const secondSetter = result.current[1];
|
|
186
|
+
|
|
187
|
+
// Setter should be different because componentId changed
|
|
188
|
+
expect(firstSetter).not.toBe(secondSetter);
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
it("should call store.setState with correct parameters", () => {
|
|
192
|
+
const mockStore = createMockStore();
|
|
193
|
+
const setStateSpy = jest.spyOn(mockStore, "setState");
|
|
194
|
+
|
|
195
|
+
mockUseScreenContextV2.mockReturnValue({
|
|
196
|
+
_componentStateStore: mockStore,
|
|
197
|
+
} as any);
|
|
198
|
+
|
|
199
|
+
const { result } = renderHook(() =>
|
|
200
|
+
useComponentScreenState("state-test", "initial")
|
|
201
|
+
);
|
|
202
|
+
|
|
203
|
+
const newValue = "new-value";
|
|
204
|
+
|
|
205
|
+
act(() => {
|
|
206
|
+
result.current[1](newValue);
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
expect(setStateSpy).toHaveBeenCalledWith({
|
|
210
|
+
"state-test": newValue,
|
|
211
|
+
});
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
it("should handle falsy initial values correctly", () => {
|
|
215
|
+
const mockStore = createMockStore();
|
|
216
|
+
|
|
217
|
+
mockUseScreenContextV2.mockReturnValue({
|
|
218
|
+
_componentStateStore: mockStore,
|
|
219
|
+
} as any);
|
|
220
|
+
|
|
221
|
+
// Test with falsy values
|
|
222
|
+
const { result: nullResult } = renderHook(() =>
|
|
223
|
+
useComponentScreenState("null-test", null)
|
|
224
|
+
);
|
|
225
|
+
|
|
226
|
+
expect(nullResult.current[0]).toBeNull();
|
|
227
|
+
|
|
228
|
+
const { result: falseResult } = renderHook(() =>
|
|
229
|
+
useComponentScreenState("false-test", false)
|
|
230
|
+
);
|
|
231
|
+
|
|
232
|
+
expect(falseResult.current[0]).toBe(false);
|
|
233
|
+
|
|
234
|
+
const { result: zeroResult } = renderHook(() =>
|
|
235
|
+
useComponentScreenState("zero-test", 0)
|
|
236
|
+
);
|
|
237
|
+
|
|
238
|
+
expect(zeroResult.current[0]).toBe(0);
|
|
239
|
+
|
|
240
|
+
const { result: emptyStrResult } = renderHook(() =>
|
|
241
|
+
useComponentScreenState("empty-test", "")
|
|
242
|
+
);
|
|
243
|
+
|
|
244
|
+
expect(emptyStrResult.current[0]).toBe("");
|
|
245
|
+
});
|
|
246
|
+
});
|