@applicaster/zapp-react-native-utils 16.0.0-alpha.3439504142 → 16.0.0-alpha.3502052160
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 +8 -10
- package/actionsExecutor/ActionExecutorContext.tsx +40 -345
- package/actionsExecutor/actions/__tests__/showToast.test.ts +88 -0
- package/actionsExecutor/actions/appRestart.ts +24 -0
- package/actionsExecutor/actions/confirmDialog.ts +53 -0
- package/actionsExecutor/actions/index.ts +30 -0
- package/actionsExecutor/actions/localStorageRemove.ts +27 -0
- package/actionsExecutor/actions/localStorageSet.ts +28 -0
- package/actionsExecutor/actions/localStorageToggleFlag.ts +28 -0
- package/actionsExecutor/actions/navigateToScreen.ts +123 -0
- package/actionsExecutor/actions/refreshComponent.ts +79 -0
- package/actionsExecutor/actions/screenSetVariable.ts +35 -0
- package/actionsExecutor/actions/screenToggleFlag.ts +38 -0
- package/actionsExecutor/actions/sendCloudEvent.ts +88 -0
- package/actionsExecutor/actions/sessionStorageRemove.ts +19 -0
- package/actionsExecutor/actions/sessionStorageSet.ts +20 -0
- package/actionsExecutor/actions/sessionStorageToggleFlag.ts +15 -0
- package/actionsExecutor/actions/showToast.ts +87 -0
- package/actionsExecutor/actions/switchLayout.ts +40 -0
- package/actionsExecutor/types.ts +59 -0
- package/analyticsUtils/PlayerAnalyticsManager.ts +1 -1
- package/appUtils/contextKeysManager/__tests__/getKey/failure.test.ts +1 -1
- package/appUtils/contextKeysManager/__tests__/removeKey/failure.test.ts +1 -1
- package/appUtils/contextKeysManager/__tests__/setKey/failure/invalidKey.test.ts +4 -4
- package/appUtils/contextKeysManager/index.ts +1 -1
- package/appUtils/playerManager/OverlayObserver/OverlaysObserver.ts +12 -6
- package/appUtils/playerManager/__tests__/playerFactory.test.ts +1 -1
- package/appUtils/playerManager/{conts.ts → const.ts} +20 -0
- package/appUtils/playerManager/index.ts +1 -1
- package/appUtils/playerManager/player.ts +1 -1
- package/appUtils/playerManager/playerFactory.ts +1 -1
- package/appUtils/playerManager/playerNative.ts +1 -1
- package/appUtils/playerManager/usePlayerControllerSetup.tsx +1 -1
- package/appUtils/playerManager/userLanguagePreference.ts +1 -1
- package/arrayUtils/__tests__/{anyThruthy.test.ts → anyTruthy.test.ts} +8 -0
- package/arrayUtils/__tests__/arrayUtils.test.ts +165 -108
- package/arrayUtils/__tests__/isEmptyArray.test.ts +11 -0
- package/arrayUtils/__tests__/isFilledArray.test.ts +12 -0
- package/arrayUtils/__tests__/isFirst.test.ts +17 -0
- package/arrayUtils/__tests__/isIndexInRange.test.ts +14 -0
- package/arrayUtils/__tests__/isLast.test.ts +31 -0
- package/arrayUtils/__tests__/makeListOf.test.ts +31 -0
- package/arrayUtils/__tests__/makeListOfIndexes.test.ts +20 -0
- package/arrayUtils/__tests__/sample.test.ts +61 -0
- package/arrayUtils/index.ts +57 -20
- package/bottomSheet/__tests__/scrollContext.test.tsx +35 -0
- package/bottomSheet/index.ts +17 -0
- package/manifestUtils/defaultManifestConfigurations/player.js +19 -19
- package/manifestUtils/keys.js +8 -0
- package/manifestUtils/mobileAction/button/index.js +16 -0
- package/manifestUtils/mobileAction/container/index.js +3 -1
- package/manifestUtils/mobileAction/groups/defaults.js +3 -1
- package/numberUtils/__tests__/isMinusZero.test.ts +20 -0
- package/numberUtils/__tests__/isZero.test.ts +27 -0
- package/numberUtils/__tests__/requireNumber.test.ts +50 -0
- package/numberUtils/__tests__/toNumber.test.ts +27 -0
- package/numberUtils/__tests__/toNumberWithDefault.test.ts +64 -0
- package/numberUtils/__tests__/toNumberWithDefaultZero.test.ts +48 -0
- package/numberUtils/index.ts +27 -8
- package/package.json +2 -2
- package/reactHooks/actions/index.ts +51 -1
- package/reactHooks/cell-click/index.ts +3 -3
- package/reactHooks/feed/__tests__/useInflatedUrl.test.tsx +25 -0
- package/reactHooks/feed/useInflatedUrl.ts +6 -6
- package/reactHooks/layout/index.ts +1 -1
- package/reactHooks/navigation/__mocks__/index.ts +4 -0
- package/reactHooks/navigation/__tests__/useIsScreenActive.test.ts +42 -0
- package/reactHooks/navigation/index.ts +1 -1
- package/reactHooks/navigation/useIsScreenActive.ts +29 -8
- package/reactHooks/state/useComponentScreenState.ts +1 -1
- package/stringUtils/__tests__/stringUtils.test.js +42 -0
- package/stringUtils/index.ts +2 -2
- package/uiActionsRegistrator/index.ts +203 -0
- package/zappFrameworkUtils/__tests__/localStorageHelper.test.ts +146 -0
- package/zappFrameworkUtils/localStorageHelper.ts +19 -15
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/// <reference types="@applicaster/applicaster-types" />
|
|
2
|
+
import { ActionResult } from "./ActionExecutor";
|
|
3
|
+
import { useScreenStateStore } from "../reactHooks/navigation/useScreenStateStore";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Base context object passed to action handlers during execution.
|
|
7
|
+
* Contains references to the current entry, screen, and other execution state.
|
|
8
|
+
*/
|
|
9
|
+
export interface ActionExecutionContext {
|
|
10
|
+
/** The entry associated with this action (e.g., the item that was tapped) */
|
|
11
|
+
entry?: ZappEntry;
|
|
12
|
+
|
|
13
|
+
/** The current screen/river data */
|
|
14
|
+
screenData?: ZappRiver;
|
|
15
|
+
|
|
16
|
+
/** The entry that defines the screen (if screen was opened from an entry) */
|
|
17
|
+
screenEntry?: ZappEntry;
|
|
18
|
+
|
|
19
|
+
/** The UI component that triggered the action.
|
|
20
|
+
* From UIComponentContext as `{ ...component, parent }`.
|
|
21
|
+
*/
|
|
22
|
+
component?: ZappUIComponent & { parent?: ZappUIComponent };
|
|
23
|
+
|
|
24
|
+
/** The route identifier for the current screen */
|
|
25
|
+
screenRoute?: string;
|
|
26
|
+
|
|
27
|
+
/** The screen state store for managing screen-level state (Zustand store) */
|
|
28
|
+
screenStateStore?: ReturnType<typeof useScreenStateStore>;
|
|
29
|
+
|
|
30
|
+
/** Context entry for additional data */
|
|
31
|
+
entryContext?: ZappEntry;
|
|
32
|
+
|
|
33
|
+
/** Callback function for action completion */
|
|
34
|
+
callback?: (result: { success: boolean; error: any; abort: boolean }) => void;
|
|
35
|
+
|
|
36
|
+
screenState?: Record<any, any>;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Type for action handler functions.
|
|
41
|
+
* Handlers receive the action (which may have typed options) and execution context.
|
|
42
|
+
*
|
|
43
|
+
* @template _TOptions - Optional type hint for the action's options object (for documentation/intellisense)
|
|
44
|
+
* @param action - The action to execute
|
|
45
|
+
* @param context - The execution context
|
|
46
|
+
* @returns A promise that resolves to the action result
|
|
47
|
+
*/
|
|
48
|
+
export type ActionHandler<_TOptions = any> = (
|
|
49
|
+
action: ActionType,
|
|
50
|
+
context?: ActionExecutionContext
|
|
51
|
+
) => Promise<ActionResult>;
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Factory function that creates an action handler.
|
|
55
|
+
* Used for actions that need to capture dependencies at registration time.
|
|
56
|
+
*/
|
|
57
|
+
export type ActionHandlerFactory<_TOptions = any> = (
|
|
58
|
+
...dependencies: any[]
|
|
59
|
+
) => ActionHandler<_TOptions>;
|
|
@@ -8,7 +8,7 @@ import {
|
|
|
8
8
|
} from "../appUtils/playerManager";
|
|
9
9
|
import { Player } from "../appUtils/playerManager/player";
|
|
10
10
|
import { AnalyticPlayerListener } from "./AnalyticPlayerListener";
|
|
11
|
-
import { PlayerRole } from "../appUtils/playerManager/
|
|
11
|
+
import { PlayerRole } from "../appUtils/playerManager/const";
|
|
12
12
|
|
|
13
13
|
enum PlayerAnalyticsTrackerType {
|
|
14
14
|
PlayerAnalyticsTracker = "PlayerAnalyticsTracker",
|
|
@@ -46,7 +46,7 @@ describe("Context Keys Manager - getKey", () => {
|
|
|
46
46
|
|
|
47
47
|
expect(mockedLogger.warn).toHaveBeenCalledWith({
|
|
48
48
|
message:
|
|
49
|
-
"Provided key is not valid -
|
|
49
|
+
"Provided key is not valid - expecting an object with namespace and key properties",
|
|
50
50
|
data: { key: invalidKey },
|
|
51
51
|
});
|
|
52
52
|
|
|
@@ -47,7 +47,7 @@ describe("Context Keys Manager - removeKey", () => {
|
|
|
47
47
|
|
|
48
48
|
expect(mockedLogger.warn).toHaveBeenCalledWith({
|
|
49
49
|
message:
|
|
50
|
-
"Provided key is not valid -
|
|
50
|
+
"Provided key is not valid - expecting an object with namespace and key properties",
|
|
51
51
|
data: { key },
|
|
52
52
|
});
|
|
53
53
|
|
|
@@ -48,7 +48,7 @@ describe("Context Keys Manager - setKey", () => {
|
|
|
48
48
|
|
|
49
49
|
expect(mockedLogger.warn).toHaveBeenCalledWith({
|
|
50
50
|
message:
|
|
51
|
-
"Provided key is not valid -
|
|
51
|
+
"Provided key is not valid - expecting an object with namespace and key properties",
|
|
52
52
|
data: { key },
|
|
53
53
|
});
|
|
54
54
|
|
|
@@ -105,7 +105,7 @@ describe("Context Keys Manager - setKey", () => {
|
|
|
105
105
|
|
|
106
106
|
expect(mockedLogger.warn).toHaveBeenCalledWith({
|
|
107
107
|
message:
|
|
108
|
-
"Provided key is not valid -
|
|
108
|
+
"Provided key is not valid - expecting an object with namespace and key properties",
|
|
109
109
|
data: { key },
|
|
110
110
|
});
|
|
111
111
|
|
|
@@ -162,7 +162,7 @@ describe("Context Keys Manager - setKey", () => {
|
|
|
162
162
|
|
|
163
163
|
expect(mockedLogger.warn).toHaveBeenCalledWith({
|
|
164
164
|
message:
|
|
165
|
-
"Provided key is not valid -
|
|
165
|
+
"Provided key is not valid - expecting an object with namespace and key properties",
|
|
166
166
|
data: { key },
|
|
167
167
|
});
|
|
168
168
|
|
|
@@ -217,7 +217,7 @@ describe("Context Keys Manager - setKey", () => {
|
|
|
217
217
|
|
|
218
218
|
expect(mockedLogger.warn).toHaveBeenCalledWith({
|
|
219
219
|
message:
|
|
220
|
-
"Provided key is not valid -
|
|
220
|
+
"Provided key is not valid - expecting an object with namespace and key properties",
|
|
221
221
|
data: { key },
|
|
222
222
|
});
|
|
223
223
|
|
|
@@ -109,7 +109,7 @@ export class ContextKeysManager {
|
|
|
109
109
|
if (!keyIsValid(parsedKey)) {
|
|
110
110
|
this._logger.warn({
|
|
111
111
|
message:
|
|
112
|
-
"Provided key is not valid -
|
|
112
|
+
"Provided key is not valid - expecting an object with namespace and key properties",
|
|
113
113
|
data: { key },
|
|
114
114
|
});
|
|
115
115
|
|
|
@@ -59,6 +59,7 @@ type PlayNextConfig = {
|
|
|
59
59
|
export type PlayNextState = PlayNextConfig & {
|
|
60
60
|
triggerTime: number;
|
|
61
61
|
handleUserCancelPlayNext: () => void;
|
|
62
|
+
hidePlayNext: () => void;
|
|
62
63
|
};
|
|
63
64
|
|
|
64
65
|
export class OverlaysObserver {
|
|
@@ -76,7 +77,7 @@ export class OverlaysObserver {
|
|
|
76
77
|
private chapterMarkerEvents: ChapterMarkerEvent[];
|
|
77
78
|
readonly player: Player;
|
|
78
79
|
private playNextConfig?: PlayNextConfig;
|
|
79
|
-
private
|
|
80
|
+
private isPlayNextSuppressed = false;
|
|
80
81
|
|
|
81
82
|
constructor({ player }: ChapterMarkersObserverProps) {
|
|
82
83
|
this.chapterSubject = new BehaviorSubject(null);
|
|
@@ -143,11 +144,15 @@ export class OverlaysObserver {
|
|
|
143
144
|
return this.titleSummarySubject.asObservable().pipe(distinctUntilChanged());
|
|
144
145
|
}
|
|
145
146
|
|
|
146
|
-
|
|
147
|
-
this.
|
|
147
|
+
hidePlayNext = () => {
|
|
148
|
+
this.isPlayNextSuppressed = true;
|
|
148
149
|
this.playNextSubject.next(null);
|
|
149
150
|
};
|
|
150
151
|
|
|
152
|
+
handleUserCancelPlayNext = () => {
|
|
153
|
+
this.hidePlayNext();
|
|
154
|
+
};
|
|
155
|
+
|
|
151
156
|
preparePlayNext = async () => {
|
|
152
157
|
try {
|
|
153
158
|
const plugins = appStore.get("plugins");
|
|
@@ -270,7 +275,7 @@ export class OverlaysObserver {
|
|
|
270
275
|
|
|
271
276
|
// TODO: Hack for video end, will be replaced with playlist prev/next in the future
|
|
272
277
|
getPlayNextEntry = () =>
|
|
273
|
-
!this.
|
|
278
|
+
!this.isPlayNextSuppressed ? this.playNextConfig?.entry : null;
|
|
274
279
|
|
|
275
280
|
prepareChapterMarkers = () => {
|
|
276
281
|
const chapterMarkers: ChapterMarkerOriginal[] =
|
|
@@ -353,7 +358,7 @@ export class OverlaysObserver {
|
|
|
353
358
|
const shouldPlayNextBeVisible = currentTime > triggerTime;
|
|
354
359
|
|
|
355
360
|
if (shouldPlayNextBeVisible) {
|
|
356
|
-
if (this.
|
|
361
|
+
if (this.isPlayNextSuppressed) {
|
|
357
362
|
return;
|
|
358
363
|
}
|
|
359
364
|
|
|
@@ -361,10 +366,11 @@ export class OverlaysObserver {
|
|
|
361
366
|
...this.playNextConfig,
|
|
362
367
|
triggerTime,
|
|
363
368
|
handleUserCancelPlayNext: this.handleUserCancelPlayNext,
|
|
369
|
+
hidePlayNext: this.hidePlayNext,
|
|
364
370
|
});
|
|
365
371
|
} else {
|
|
366
372
|
this.playNextSubject.next(null);
|
|
367
|
-
this.
|
|
373
|
+
this.isPlayNextSuppressed = false;
|
|
368
374
|
}
|
|
369
375
|
};
|
|
370
376
|
|
|
@@ -10,7 +10,7 @@ jest.mock("@applicaster/zapp-react-native-utils/pluginUtils", () => ({
|
|
|
10
10
|
mockFindPluginByIdentifier(id, plugins),
|
|
11
11
|
}));
|
|
12
12
|
|
|
13
|
-
import { PlayerRole } from "../
|
|
13
|
+
import { PlayerRole } from "../const";
|
|
14
14
|
import { playerFactory } from "../playerFactory";
|
|
15
15
|
|
|
16
16
|
const fakeComponent = () => null;
|
|
@@ -67,3 +67,23 @@ export enum PlayerRole {
|
|
|
67
67
|
Chromecast = "Chromecast",
|
|
68
68
|
Unspecified = "Unspecified",
|
|
69
69
|
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* The playback engine backing a web player. Published on
|
|
73
|
+
* `window.applicaster.playerType` when a player is created, so that plugins
|
|
74
|
+
* observing the player can tell which engine they are looking at instead of
|
|
75
|
+
* inferring it from the API the instance happens to expose.
|
|
76
|
+
*
|
|
77
|
+
* Keep in sync with `ApplicasterGlobals["playerType"]` in applicaster-types.
|
|
78
|
+
*/
|
|
79
|
+
export enum WebPlayerEngine {
|
|
80
|
+
Videojs = "videojs",
|
|
81
|
+
Hlsjs = "hlsjs",
|
|
82
|
+
Dashjs = "dashjs",
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export enum PLAYER_EVENTS {
|
|
86
|
+
PlayNextTriggered = "play_next_triggered",
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export type PlayNextTriggeredEvent = { id: string };
|
|
@@ -9,7 +9,7 @@ import {
|
|
|
9
9
|
unsubscribeToDeviceNetworkChanges,
|
|
10
10
|
} from "@applicaster/quick-brick-core/App/NetworkStatusProvider/utils";
|
|
11
11
|
import { setUserLanguagePreference } from "./userLanguagePreference";
|
|
12
|
-
import { PlayerRole } from "./
|
|
12
|
+
import { PlayerRole } from "./const";
|
|
13
13
|
import { RNPlayerManager } from "./nativePlayerManager";
|
|
14
14
|
|
|
15
15
|
const { log_error, log_debug, log_warning } = createLogger({
|
|
@@ -3,7 +3,7 @@ import { BehaviorSubject } from "rxjs";
|
|
|
3
3
|
import { isEmptyOrNil } from "@applicaster/zapp-react-native-utils/cellUtils";
|
|
4
4
|
import { createLogger, utilsLogger } from "../../logger";
|
|
5
5
|
import { getBoolFromConfigValue } from "../../configurationUtils";
|
|
6
|
-
import { PlayerRole, SharedPlayerCallBacksKeys } from "./
|
|
6
|
+
import { PlayerRole, SharedPlayerCallBacksKeys } from "./const";
|
|
7
7
|
import { OverlaysObserver } from "./OverlayObserver/OverlaysObserver";
|
|
8
8
|
import { TVSeekController } from "../../reactHooks/player/TVSeekControlller/TVSeekController";
|
|
9
9
|
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { appStore } from "@applicaster/zapp-react-native-redux/AppStore";
|
|
2
2
|
import { findPluginByIdentifier } from "@applicaster/zapp-react-native-utils/pluginUtils";
|
|
3
3
|
import { Player } from "./player";
|
|
4
|
-
import { PlayerRole } from "./
|
|
4
|
+
import { PlayerRole } from "./const";
|
|
5
5
|
|
|
6
6
|
type PlayerFactoryItem = {
|
|
7
7
|
Component: any;
|
|
@@ -3,7 +3,7 @@ import { isEmptyOrNil } from "@applicaster/zapp-react-native-utils/cellUtils";
|
|
|
3
3
|
import { createLogger, utilsLogger } from "../../logger";
|
|
4
4
|
import { Player } from "./player";
|
|
5
5
|
import { findNodeHandle } from "react-native";
|
|
6
|
-
import { PlayerRole } from "./
|
|
6
|
+
import { PlayerRole } from "./const";
|
|
7
7
|
import { toBooleanWithDefaultFalse } from "../../booleanUtils";
|
|
8
8
|
import { RNPlayerManager } from "./nativePlayerManager";
|
|
9
9
|
|
|
@@ -3,7 +3,7 @@ import { useConfiguration } from "../../reactHooks/useConfiguration";
|
|
|
3
3
|
import { useEffect } from "react";
|
|
4
4
|
import { playerManager } from "./index";
|
|
5
5
|
import { useValidatePlayerConfig } from "../../playerUtils/useValidatePlayerConfig";
|
|
6
|
-
import { PlayerRole } from "./
|
|
6
|
+
import { PlayerRole } from "./const";
|
|
7
7
|
import { isAppleTV } from "@applicaster/zapp-react-native-ui-components/Helpers/Platform";
|
|
8
8
|
import { TVSeekController } from "../../reactHooks/player/TVSeekControlller/TVSeekController";
|
|
9
9
|
import { KeyInputHandler } from "../keyInputHandler/KeyInputHandler";
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { localStorage } from "@applicaster/zapp-react-native-bridge/ZappStorage/LocalStorage";
|
|
2
2
|
import { sessionStorage } from "@applicaster/zapp-react-native-bridge/ZappStorage/SessionStorage";
|
|
3
3
|
import { createLogger, utilsLogger } from "../../logger";
|
|
4
|
-
import { userPreferencesNamespace } from "./
|
|
4
|
+
import { userPreferencesNamespace } from "./const";
|
|
5
5
|
|
|
6
6
|
export const preferredAudioLanguageStorageKey = "preferred_audio_language";
|
|
7
7
|
|
|
@@ -21,4 +21,12 @@ describe("anyTruthy", () => {
|
|
|
21
21
|
expect(anyTruthy([true])).toBe(true);
|
|
22
22
|
expect(anyTruthy([false])).toBe(false);
|
|
23
23
|
});
|
|
24
|
+
|
|
25
|
+
it("should return true when the first value is true", () => {
|
|
26
|
+
expect(anyTruthy([true, false, false])).toBe(true);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it("should return true when only the last value is true", () => {
|
|
30
|
+
expect(anyTruthy([false, false, true])).toBe(true);
|
|
31
|
+
});
|
|
24
32
|
});
|
|
@@ -28,6 +28,30 @@ describe("shiftArray", () => {
|
|
|
28
28
|
expect(shiftArray(negativeOffset, array)).toMatchSnapshot();
|
|
29
29
|
expect(shiftArray(2 * negativeOffset, array)).toMatchSnapshot();
|
|
30
30
|
});
|
|
31
|
+
|
|
32
|
+
it("returns a new array and does not mutate the original", () => {
|
|
33
|
+
const original = ["a", "b", "c"];
|
|
34
|
+
const result = shiftArray(1, original);
|
|
35
|
+
|
|
36
|
+
expect(result).toEqual(["b", "c", "a"]);
|
|
37
|
+
expect(result).not.toBe(original);
|
|
38
|
+
expect(original).toEqual(["a", "b", "c"]);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it("returns an empty array when shifting an empty array", () => {
|
|
42
|
+
expect(shiftArray(1, [])).toEqual([]);
|
|
43
|
+
expect(shiftArray(-1, [])).toEqual([]);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it("returns the same order for a single-element array", () => {
|
|
47
|
+
expect(shiftArray(1, [1])).toEqual([1]);
|
|
48
|
+
expect(shiftArray(-1, [1])).toEqual([1]);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it("handles an offset equal to the array length", () => {
|
|
52
|
+
expect(shiftArray(4, array)).toEqual(array);
|
|
53
|
+
expect(shiftArray(-4, array)).toEqual(array);
|
|
54
|
+
});
|
|
31
55
|
});
|
|
32
56
|
|
|
33
57
|
describe("removeItemFromList", () => {
|
|
@@ -58,6 +82,38 @@ describe("removeItemFromList", () => {
|
|
|
58
82
|
arrayOfObjects
|
|
59
83
|
);
|
|
60
84
|
});
|
|
85
|
+
|
|
86
|
+
it("returns the same reference when the item is not found", () => {
|
|
87
|
+
const list = [1, 2, 3];
|
|
88
|
+
|
|
89
|
+
expect(removeItemFromList(4, list)).toBe(list);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it("returns a new array when the item is removed", () => {
|
|
93
|
+
const list = [1, 2, 3];
|
|
94
|
+
const result = removeItemFromList(2, list);
|
|
95
|
+
|
|
96
|
+
expect(result).toEqual([1, 3]);
|
|
97
|
+
expect(result).not.toBe(list);
|
|
98
|
+
expect(list).toEqual([1, 2, 3]);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it("removes only the first occurrence of a duplicate item", () => {
|
|
102
|
+
expect(removeItemFromList(2, [1, 2, 2, 3])).toEqual([1, 2, 3]);
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it("removes the first and last items", () => {
|
|
106
|
+
expect(removeItemFromList(1, [1, 2, 3])).toEqual([2, 3]);
|
|
107
|
+
expect(removeItemFromList(3, [1, 2, 3])).toEqual([1, 2]);
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it("returns an empty list when removing from an empty list", () => {
|
|
111
|
+
expect(removeItemFromList(1, [])).toEqual([]);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it("returns an empty list when removing the only item", () => {
|
|
115
|
+
expect(removeItemFromList(1, [1])).toEqual([]);
|
|
116
|
+
});
|
|
61
117
|
});
|
|
62
118
|
|
|
63
119
|
describe("mapPromises", () => {
|
|
@@ -84,145 +140,146 @@ describe("mapPromises", () => {
|
|
|
84
140
|
values.forEach((value) => {
|
|
85
141
|
expect(promiseFn).toHaveBeenNthCalledWith(value, value, value - 1);
|
|
86
142
|
});
|
|
87
|
-
});
|
|
88
143
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
const arrayOfPrimitives = [1, 2, 3, 4];
|
|
92
|
-
expect(removeItemFromList(1, arrayOfPrimitives)).toEqual([2, 3, 4]);
|
|
144
|
+
expect.assertions(values.length + 1);
|
|
145
|
+
});
|
|
93
146
|
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
];
|
|
147
|
+
it("resolves to an empty array for an empty input", async () => {
|
|
148
|
+
await expect(mapPromises(promiseFn, [])).resolves.toEqual([]);
|
|
149
|
+
expect(promiseFn).not.toHaveBeenCalled();
|
|
150
|
+
});
|
|
99
151
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
});
|
|
152
|
+
it("rejects when any promise rejects", async () => {
|
|
153
|
+
const failingFn = jest.fn((num) =>
|
|
154
|
+
num === 2 ? Promise.reject(new Error("fail")) : Promise.resolve(num)
|
|
155
|
+
);
|
|
105
156
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
{ prop: "value2" },
|
|
110
|
-
{ prop: "value3" },
|
|
111
|
-
];
|
|
157
|
+
await expect(mapPromises(failingFn, [1, 2, 3])).rejects.toThrow("fail");
|
|
158
|
+
});
|
|
159
|
+
});
|
|
112
160
|
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
161
|
+
describe("reducePromises", () => {
|
|
162
|
+
const promiseFn = jest.fn((current, previous, index, cb) => {
|
|
163
|
+
return new Promise((resolve) => {
|
|
164
|
+
setTimeout(() => {
|
|
165
|
+
resolve(previous + current);
|
|
166
|
+
cb?.(index);
|
|
167
|
+
}, 1);
|
|
116
168
|
});
|
|
117
169
|
});
|
|
118
170
|
|
|
119
|
-
|
|
120
|
-
|
|
171
|
+
beforeEach(() => {
|
|
172
|
+
promiseFn.mockClear();
|
|
173
|
+
});
|
|
121
174
|
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
});
|
|
175
|
+
it("is a curried function; and returns the reduced promise values", async () => {
|
|
176
|
+
const promiseReducer = reducePromises(promiseFn);
|
|
125
177
|
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
178
|
+
const initialValue = 0;
|
|
179
|
+
const values = [1, 2, 3];
|
|
180
|
+
const sumOfValues = initialValue + values.reduce((a, b) => a + b, 0);
|
|
129
181
|
|
|
130
|
-
|
|
131
|
-
await expect(promiseMapper(values)).resolves.toEqual([2, 3, 4]);
|
|
132
|
-
});
|
|
182
|
+
expect(typeof promiseReducer).toBe("function");
|
|
133
183
|
|
|
134
|
-
|
|
135
|
-
|
|
184
|
+
await expect(promiseReducer(initialValue, values)).resolves.toEqual(
|
|
185
|
+
sumOfValues
|
|
186
|
+
);
|
|
187
|
+
});
|
|
136
188
|
|
|
137
|
-
|
|
138
|
-
|
|
189
|
+
it("calls promises one after the other", (done) => {
|
|
190
|
+
const values = [1, 2, 3];
|
|
191
|
+
const previousValues = [0, 1, 3];
|
|
192
|
+
|
|
193
|
+
reducePromises(
|
|
194
|
+
(current, previous, index) =>
|
|
195
|
+
promiseFn(current, previous, index, (index) => {
|
|
196
|
+
expect(promiseFn).toHaveBeenCalledTimes(index + 1);
|
|
197
|
+
|
|
198
|
+
expect(promiseFn).toHaveBeenNthCalledWith(
|
|
199
|
+
index + 1,
|
|
200
|
+
values[index],
|
|
201
|
+
previousValues[index],
|
|
202
|
+
index,
|
|
203
|
+
expect.any(Function)
|
|
204
|
+
);
|
|
205
|
+
|
|
206
|
+
if (index === 2) done();
|
|
207
|
+
}),
|
|
208
|
+
0,
|
|
209
|
+
values
|
|
210
|
+
);
|
|
139
211
|
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
});
|
|
212
|
+
expect.assertions(values.length * 2);
|
|
213
|
+
});
|
|
143
214
|
|
|
144
|
-
|
|
145
|
-
|
|
215
|
+
it("returns the initial value for an empty list", async () => {
|
|
216
|
+
await expect(reducePromises(promiseFn, 10, [])).resolves.toBe(10);
|
|
217
|
+
expect(promiseFn).not.toHaveBeenCalled();
|
|
146
218
|
});
|
|
147
219
|
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
setTimeout(() => {
|
|
152
|
-
resolve(previous + current);
|
|
153
|
-
cb?.(index);
|
|
154
|
-
}, 1);
|
|
155
|
-
});
|
|
156
|
-
});
|
|
220
|
+
it("reduces a single-element list", async () => {
|
|
221
|
+
await expect(reducePromises(promiseFn, 5, [2])).resolves.toBe(7);
|
|
222
|
+
});
|
|
157
223
|
|
|
158
|
-
|
|
159
|
-
|
|
224
|
+
it("rejects when a promise rejects", async () => {
|
|
225
|
+
const failingFn = jest.fn(async (current, previous) => {
|
|
226
|
+
if (current === 2) throw new Error("reduce fail");
|
|
227
|
+
|
|
228
|
+
return previous + current;
|
|
160
229
|
});
|
|
161
230
|
|
|
162
|
-
|
|
163
|
-
|
|
231
|
+
await expect(reducePromises(failingFn, 0, [1, 2, 3])).rejects.toThrow(
|
|
232
|
+
"reduce fail"
|
|
233
|
+
);
|
|
234
|
+
});
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
describe("mapAndSplit", () => {
|
|
238
|
+
it("maps over a list of values and split it in chunks", () => {
|
|
239
|
+
const values = new Array(20).fill(0).map((_, i) => i);
|
|
240
|
+
|
|
241
|
+
const alphabet = Array.from(Array(26)).map((_, i) =>
|
|
242
|
+
String.fromCharCode(i + 65)
|
|
243
|
+
);
|
|
164
244
|
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
const sumOfValues = initialValue + values.reduce((a, b) => a + b, 0);
|
|
245
|
+
const getLetterForInt = (num) => alphabet[num];
|
|
246
|
+
const chunkSize = 5;
|
|
168
247
|
|
|
169
|
-
|
|
248
|
+
const result = mapAndSplit<number, string>(
|
|
249
|
+
getLetterForInt,
|
|
250
|
+
chunkSize
|
|
251
|
+
)(values);
|
|
170
252
|
|
|
171
|
-
|
|
172
|
-
sumOfValues
|
|
173
|
-
);
|
|
174
|
-
});
|
|
253
|
+
expect(result.length).toBe(values.length / chunkSize);
|
|
175
254
|
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
reducePromises(
|
|
181
|
-
(current, previous, index) =>
|
|
182
|
-
promiseFn(current, previous, index, (index) => {
|
|
183
|
-
expect(promiseFn).toHaveBeenCalledTimes(index + 1);
|
|
184
|
-
|
|
185
|
-
expect(promiseFn).toHaveBeenNthCalledWith(
|
|
186
|
-
index + 1,
|
|
187
|
-
values[index],
|
|
188
|
-
previousValues[index],
|
|
189
|
-
index,
|
|
190
|
-
expect.any(Function)
|
|
191
|
-
);
|
|
192
|
-
|
|
193
|
-
if (index === 2) done();
|
|
194
|
-
}),
|
|
195
|
-
0,
|
|
196
|
-
values
|
|
197
|
-
);
|
|
198
|
-
|
|
199
|
-
expect.assertions(values.length * 2);
|
|
255
|
+
result.forEach((chunk, chunkIndex) => {
|
|
256
|
+
chunk.forEach((value, index) => {
|
|
257
|
+
expect(value).toBe(alphabet[chunkIndex * chunkSize + index]);
|
|
258
|
+
});
|
|
200
259
|
});
|
|
201
260
|
});
|
|
202
261
|
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
262
|
+
it("is a curried function", () => {
|
|
263
|
+
const mapper = (n: number) => n * 2;
|
|
264
|
+
const splitBy2 = mapAndSplit(mapper, 2);
|
|
206
265
|
|
|
207
|
-
|
|
208
|
-
String.fromCharCode(i + 65)
|
|
209
|
-
);
|
|
266
|
+
expect(typeof splitBy2).toBe("function");
|
|
210
267
|
|
|
211
|
-
|
|
212
|
-
|
|
268
|
+
expect(splitBy2([1, 2, 3, 4])).toEqual([
|
|
269
|
+
[2, 4],
|
|
270
|
+
[6, 8],
|
|
271
|
+
]);
|
|
272
|
+
});
|
|
213
273
|
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
)(values);
|
|
274
|
+
it("returns an empty list for an empty input", () => {
|
|
275
|
+
expect(mapAndSplit((n: number) => n, 2)([])).toEqual([]);
|
|
276
|
+
});
|
|
218
277
|
|
|
219
|
-
|
|
278
|
+
it("puts remaining items in a final smaller chunk", () => {
|
|
279
|
+
expect(mapAndSplit((n: number) => n, 2)([1, 2, 3])).toEqual([[1, 2], [3]]);
|
|
280
|
+
});
|
|
220
281
|
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
expect(value).toBe(alphabet[chunkIndex * chunkSize + index]);
|
|
224
|
-
});
|
|
225
|
-
});
|
|
226
|
-
});
|
|
282
|
+
it("returns a single chunk when chunk size exceeds the list length", () => {
|
|
283
|
+
expect(mapAndSplit((n: number) => n * 10, 10)([1, 2])).toEqual([[10, 20]]);
|
|
227
284
|
});
|
|
228
285
|
});
|
|
@@ -60,4 +60,15 @@ describe("isEmptyArray", () => {
|
|
|
60
60
|
|
|
61
61
|
expect(isEmptyArray(value)).toBe(false);
|
|
62
62
|
});
|
|
63
|
+
|
|
64
|
+
it("array with falsy values is not empty", () => {
|
|
65
|
+
expect(isEmptyArray([0])).toBe(false);
|
|
66
|
+
expect(isEmptyArray([false])).toBe(false);
|
|
67
|
+
expect(isEmptyArray([null])).toBe(false);
|
|
68
|
+
expect(isEmptyArray([undefined])).toBe(false);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it("nested empty array is not empty", () => {
|
|
72
|
+
expect(isEmptyArray([[]])).toBe(false);
|
|
73
|
+
});
|
|
63
74
|
});
|