@applicaster/zapp-react-native-utils 16.0.0-alpha.1803022420 → 16.0.0-alpha.1816065263
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/actions/__tests__/showToast.test.ts +88 -0
- package/actionsExecutor/actions/showToast.ts +64 -8
- package/analyticsUtils/PlayerAnalyticsManager.ts +1 -1
- package/appUtils/playerManager/OverlayObserver/OverlaysObserver.ts +138 -51
- package/appUtils/playerManager/OverlayObserver/__tests__/liveContent.test.ts +205 -0
- package/appUtils/playerManager/__tests__/playerContent.test.ts +403 -0
- package/appUtils/playerManager/__tests__/playerFactory.test.ts +1 -1
- package/appUtils/playerManager/__tests__/usePlayerContent.test.tsx +64 -0
- package/appUtils/playerManager/{conts.ts → const.ts} +20 -0
- package/appUtils/playerManager/index.ts +1 -1
- package/appUtils/playerManager/player.ts +115 -3
- package/appUtils/playerManager/playerFactory.ts +1 -1
- package/appUtils/playerManager/playerNative.ts +1 -1
- package/appUtils/playerManager/usePlayerContent.tsx +43 -0
- 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 +185 -19
- 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/playerUtils/__tests__/contentDataKeys.test.ts +297 -0
- package/playerUtils/__tests__/resolvePlayerTitleSubtitle.test.ts +180 -0
- package/playerUtils/contentDataKeys.ts +134 -0
- package/playerUtils/index.ts +20 -15
- package/playerUtils/isAudioItem.ts +26 -0
- package/playerUtils/resolvePlayerTitleSubtitle.ts +76 -0
- package/reactHooks/feed/__tests__/useInflatedUrl.test.tsx +25 -0
- package/reactHooks/layout/index.ts +1 -1
- package/stringUtils/__tests__/stringUtils.test.js +42 -0
- package/stringUtils/index.ts +2 -2
- package/zappFrameworkUtils/__tests__/localStorageHelper.test.ts +146 -0
- package/zappFrameworkUtils/localStorageHelper.ts +19 -15
- package/appUtils/playerManager/usePlayerTitleSummaryOverlay.tsx +0 -56
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { get } from "@applicaster/zapp-react-native-utils/utils";
|
|
2
|
+
import { isNotEmptyString } from "@applicaster/zapp-react-native-utils/stringUtils";
|
|
3
|
+
|
|
4
|
+
type GetValue = <T = string | number | null>(key: string) => T;
|
|
5
|
+
|
|
6
|
+
export type PlayerContentField = "title" | "subtitle";
|
|
7
|
+
|
|
8
|
+
const CUSTOM_SOURCE = "other";
|
|
9
|
+
|
|
10
|
+
const DEFAULT_ENTRY_PATH: Record<PlayerContentField, string> = {
|
|
11
|
+
title: "title",
|
|
12
|
+
subtitle: "summary",
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Configuration keys backing each field, exported so callers can build a
|
|
17
|
+
* stable configuration getter without duplicating the key strings.
|
|
18
|
+
*/
|
|
19
|
+
export const PLAYER_CONTENT_DATA_KEYS: Record<
|
|
20
|
+
PlayerContentField,
|
|
21
|
+
{ source: string; custom: string }
|
|
22
|
+
> = {
|
|
23
|
+
title: {
|
|
24
|
+
source: "title_data_key_source",
|
|
25
|
+
custom: "title_custom_data_key",
|
|
26
|
+
},
|
|
27
|
+
subtitle: {
|
|
28
|
+
source: "subtitle_data_key_source",
|
|
29
|
+
custom: "subtitle_custom_data_key",
|
|
30
|
+
},
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export type PlayerContentDataKeys = Record<
|
|
34
|
+
PlayerContentField,
|
|
35
|
+
{ source: string; custom: string }
|
|
36
|
+
>;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* TV keeps separate Title/Summary configuration per media type. Mobile has
|
|
40
|
+
* no equivalent split - it shares a single `PLAYER_CONTENT_DATA_KEYS` set
|
|
41
|
+
* for both video and audio - which is why these sets carry a media-type
|
|
42
|
+
* prefix (`video_player_*` / `audio_player_*`) and mobile's doesn't.
|
|
43
|
+
*/
|
|
44
|
+
export const VIDEO_PLAYER_CONTENT_DATA_KEYS: PlayerContentDataKeys = {
|
|
45
|
+
title: {
|
|
46
|
+
source: "video_player_title_data_key_source",
|
|
47
|
+
custom: "video_player_title_custom_data_key",
|
|
48
|
+
},
|
|
49
|
+
subtitle: {
|
|
50
|
+
source: "video_player_summary_data_key_source",
|
|
51
|
+
custom: "video_player_summary_custom_data_key",
|
|
52
|
+
},
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
export const AUDIO_PLAYER_CONTENT_DATA_KEYS: PlayerContentDataKeys = {
|
|
56
|
+
title: {
|
|
57
|
+
source: "audio_player_title_data_key_source",
|
|
58
|
+
custom: "audio_player_title_custom_data_key",
|
|
59
|
+
},
|
|
60
|
+
subtitle: {
|
|
61
|
+
source: "audio_player_summary_data_key_source",
|
|
62
|
+
custom: "audio_player_summary_custom_data_key",
|
|
63
|
+
},
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Builds a GetValue over a plugin configuration object. Callers that hold a
|
|
68
|
+
* configuration rather than a `value` closure use this instead of hand-rolling
|
|
69
|
+
* the lookup.
|
|
70
|
+
*/
|
|
71
|
+
export const createConfigValueGetter =
|
|
72
|
+
(configuration: Option<Record<string, any>>): GetValue =>
|
|
73
|
+
<T>(key: string) =>
|
|
74
|
+
(configuration?.[key] ?? null) as T;
|
|
75
|
+
|
|
76
|
+
type ResolvePlayerContentTextParams = {
|
|
77
|
+
entry: Option<ZappEntry>;
|
|
78
|
+
field: PlayerContentField;
|
|
79
|
+
value: GetValue;
|
|
80
|
+
dataKeys?: PlayerContentDataKeys;
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Resolves the text shown for the player's Title or Subtitle.
|
|
85
|
+
*
|
|
86
|
+
* Unless the customer explicitly selected the "Other" data key source and
|
|
87
|
+
* supplied a resolvable key, this returns the hardcoded entry field the
|
|
88
|
+
* player has always used, which keeps existing apps unchanged.
|
|
89
|
+
*/
|
|
90
|
+
export const resolvePlayerContentText = ({
|
|
91
|
+
entry,
|
|
92
|
+
field,
|
|
93
|
+
value,
|
|
94
|
+
dataKeys,
|
|
95
|
+
}: ResolvePlayerContentTextParams): Option<string | number> => {
|
|
96
|
+
if (!entry) {
|
|
97
|
+
return undefined;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const defaultValue = get(entry, DEFAULT_ENTRY_PATH[field]);
|
|
101
|
+
|
|
102
|
+
const { source, custom } = (dataKeys ?? PLAYER_CONTENT_DATA_KEYS)[field];
|
|
103
|
+
|
|
104
|
+
if (value(source) !== CUSTOM_SOURCE) {
|
|
105
|
+
return defaultValue;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const customKey = value<Option<string>>(custom);
|
|
109
|
+
|
|
110
|
+
// Only trim when the config value really is a string - it comes from
|
|
111
|
+
// free-text Studio configuration and can be a non-string (or null/undefined),
|
|
112
|
+
// and calling .trim() on that would throw.
|
|
113
|
+
const trimmedKey = isNotEmptyString(customKey) ? customKey.trim() : customKey;
|
|
114
|
+
|
|
115
|
+
if (!isNotEmptyString(trimmedKey)) {
|
|
116
|
+
return defaultValue;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const customValue = get(entry, trimmedKey);
|
|
120
|
+
|
|
121
|
+
if (customValue === null || customValue === undefined || customValue === "") {
|
|
122
|
+
return defaultValue;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// The key is free text in the Studio, so it can easily land on an object or
|
|
126
|
+
// an array (`extensions.show` instead of `extensions.show.name`). Those
|
|
127
|
+
// reach <Text> untouched and throw "Objects are not valid as a React child",
|
|
128
|
+
// so they fall back like every other unusable key.
|
|
129
|
+
if (typeof customValue !== "string" && typeof customValue !== "number") {
|
|
130
|
+
return defaultValue;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
return customValue;
|
|
134
|
+
};
|
package/playerUtils/index.ts
CHANGED
|
@@ -9,6 +9,26 @@ import { Dimensions } from "react-native";
|
|
|
9
9
|
|
|
10
10
|
export { getPlayerActionButtons } from "./getPlayerActionButtons";
|
|
11
11
|
|
|
12
|
+
export {
|
|
13
|
+
resolvePlayerContentText,
|
|
14
|
+
PLAYER_CONTENT_DATA_KEYS,
|
|
15
|
+
VIDEO_PLAYER_CONTENT_DATA_KEYS,
|
|
16
|
+
AUDIO_PLAYER_CONTENT_DATA_KEYS,
|
|
17
|
+
createConfigValueGetter,
|
|
18
|
+
} from "./contentDataKeys";
|
|
19
|
+
|
|
20
|
+
export type {
|
|
21
|
+
PlayerContentField,
|
|
22
|
+
PlayerContentDataKeys,
|
|
23
|
+
} from "./contentDataKeys";
|
|
24
|
+
|
|
25
|
+
export {
|
|
26
|
+
resolvePlayerTitleSubtitle,
|
|
27
|
+
getPlayerContentDataKeys,
|
|
28
|
+
} from "./resolvePlayerTitleSubtitle";
|
|
29
|
+
|
|
30
|
+
export { isAudioItem } from "./isAudioItem";
|
|
31
|
+
|
|
12
32
|
export const HLS_MIME_TYPES = [
|
|
13
33
|
"application/x-mpegURL",
|
|
14
34
|
"application/vnd.apple.mpegurl",
|
|
@@ -102,21 +122,6 @@ export const isVideoItem = (item: Option<ZappEntry>) => {
|
|
|
102
122
|
);
|
|
103
123
|
};
|
|
104
124
|
|
|
105
|
-
export const isAudioItem = (item: Option<ZappEntry>) => {
|
|
106
|
-
if (
|
|
107
|
-
isString(item?.content?.type) &&
|
|
108
|
-
item?.content?.type?.includes?.("audio")
|
|
109
|
-
) {
|
|
110
|
-
return true;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
if (isString(item?.type?.value) && item?.type?.value?.includes?.("audio")) {
|
|
114
|
-
return true;
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
return false;
|
|
118
|
-
};
|
|
119
|
-
|
|
120
125
|
export const isInlineTV = (screenData) => {
|
|
121
126
|
return isTV() && isFilledArray(screenData?.ui_components);
|
|
122
127
|
};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { isString } from "@applicaster/zapp-react-native-utils/typeGuards";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Lives in its own module rather than in `playerUtils/index.ts` because that
|
|
5
|
+
* barrel imports `playerManager`, and files inside `appUtils/playerManager/`
|
|
6
|
+
* need this helper. Importing it from the barrel there would create a require
|
|
7
|
+
* cycle:
|
|
8
|
+
*
|
|
9
|
+
* playerManager/index -> player -> playerUtils/index -> playerManager/index
|
|
10
|
+
*
|
|
11
|
+
* Re-exported from the barrel, so existing importers are unaffected.
|
|
12
|
+
*/
|
|
13
|
+
export const isAudioItem = (item: Option<ZappEntry>) => {
|
|
14
|
+
if (
|
|
15
|
+
isString(item?.content?.type) &&
|
|
16
|
+
item?.content?.type?.includes?.("audio")
|
|
17
|
+
) {
|
|
18
|
+
return true;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
if (isString(item?.type?.value) && item?.type?.value?.includes?.("audio")) {
|
|
22
|
+
return true;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return false;
|
|
26
|
+
};
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createConfigValueGetter,
|
|
3
|
+
resolvePlayerContentText,
|
|
4
|
+
VIDEO_PLAYER_CONTENT_DATA_KEYS,
|
|
5
|
+
AUDIO_PLAYER_CONTENT_DATA_KEYS,
|
|
6
|
+
} from "./contentDataKeys";
|
|
7
|
+
import type { PlayerContentDataKeys } from "./contentDataKeys";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Resolves the player's Title/Subtitle text as raw values (string | number |
|
|
11
|
+
* null). Called by `Player.getContent()`, which publishes the result on the
|
|
12
|
+
* player's content channel; every consumer - the mobile/TV transport
|
|
13
|
+
* controls, `ProgramInfo`, the TV audio player, the Chromecast controller and
|
|
14
|
+
* the native bridge - reads it from there rather than resolving for itself.
|
|
15
|
+
*
|
|
16
|
+
* `dataKeys` selects which Studio-configured key set backs the resolution:
|
|
17
|
+
* omit it for the default (mobile) `PLAYER_CONTENT_DATA_KEYS` set, or pass
|
|
18
|
+
* an explicit set (e.g. `VIDEO_PLAYER_CONTENT_DATA_KEYS`) for platforms/content
|
|
19
|
+
* types that keep separate Studio configuration for this text. Getting the
|
|
20
|
+
* key set backwards would make one platform read another platform's
|
|
21
|
+
* configuration, so every caller must be explicit about which set it wants.
|
|
22
|
+
*
|
|
23
|
+
* Keeping this in one place is what stops the dot-path / key-set resolution
|
|
24
|
+
* logic (`resolvePlayerContentText`) from being duplicated per caller.
|
|
25
|
+
*/
|
|
26
|
+
export const resolvePlayerTitleSubtitle = ({
|
|
27
|
+
entry,
|
|
28
|
+
pluginConfiguration,
|
|
29
|
+
dataKeys,
|
|
30
|
+
}: {
|
|
31
|
+
entry: Option<ZappEntry>;
|
|
32
|
+
pluginConfiguration: Option<Record<string, any>>;
|
|
33
|
+
dataKeys?: PlayerContentDataKeys;
|
|
34
|
+
}): {
|
|
35
|
+
title: Option<string | number>;
|
|
36
|
+
summary: Option<string | number>;
|
|
37
|
+
} => {
|
|
38
|
+
const value = createConfigValueGetter(pluginConfiguration);
|
|
39
|
+
|
|
40
|
+
const title =
|
|
41
|
+
resolvePlayerContentText({ entry, field: "title", value, dataKeys }) ??
|
|
42
|
+
null;
|
|
43
|
+
|
|
44
|
+
const summary =
|
|
45
|
+
resolvePlayerContentText({
|
|
46
|
+
entry,
|
|
47
|
+
field: "subtitle",
|
|
48
|
+
value,
|
|
49
|
+
dataKeys,
|
|
50
|
+
}) ?? null;
|
|
51
|
+
|
|
52
|
+
return { title, summary };
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Picks which Studio-configured key set backs a Title/Subtitle resolution,
|
|
57
|
+
* from the full three-way choice: TV audio, TV video, or mobile. Each of
|
|
58
|
+
* those ships separate Studio configuration for this text; getting this
|
|
59
|
+
* backwards would make one platform (or content type) read another's
|
|
60
|
+
* configuration.
|
|
61
|
+
*/
|
|
62
|
+
export const getPlayerContentDataKeys = ({
|
|
63
|
+
isTV,
|
|
64
|
+
isAudioContent,
|
|
65
|
+
}: {
|
|
66
|
+
isTV: boolean;
|
|
67
|
+
isAudioContent: boolean;
|
|
68
|
+
}): PlayerContentDataKeys | undefined => {
|
|
69
|
+
if (!isTV) {
|
|
70
|
+
return undefined;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return isAudioContent
|
|
74
|
+
? AUDIO_PLAYER_CONTENT_DATA_KEYS
|
|
75
|
+
: VIDEO_PLAYER_CONTENT_DATA_KEYS;
|
|
76
|
+
};
|
|
@@ -242,4 +242,29 @@ describe("useGetUrlInflater", () => {
|
|
|
242
242
|
const url = result.current(source, mapping);
|
|
243
243
|
expect(url).toBe("https://foo.com/shows/A123?q=user%20query");
|
|
244
244
|
});
|
|
245
|
+
|
|
246
|
+
it("handles search queries with apostrophes without HTML entity escaping or URL truncation", () => {
|
|
247
|
+
const source =
|
|
248
|
+
"https://api.pc-cms.tele.quebec/api/applicaster/v1/mobile/search?q={{q}}";
|
|
249
|
+
|
|
250
|
+
const mapping = {
|
|
251
|
+
q: { source: "search", property: "q" },
|
|
252
|
+
};
|
|
253
|
+
|
|
254
|
+
const searchContext = getSearchContext("L'aventure", mapping);
|
|
255
|
+
|
|
256
|
+
const result = getInflatedDataSourceUrl({
|
|
257
|
+
source,
|
|
258
|
+
contexts: {
|
|
259
|
+
search: searchContext,
|
|
260
|
+
},
|
|
261
|
+
mapping,
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
expect(result).toBe(
|
|
265
|
+
"https://api.pc-cms.tele.quebec/api/applicaster/v1/mobile/search?q=L'aventure"
|
|
266
|
+
);
|
|
267
|
+
|
|
268
|
+
expect(result).not.toContain("'");
|
|
269
|
+
});
|
|
245
270
|
});
|
|
@@ -122,6 +122,48 @@ describe("inflate string", () => {
|
|
|
122
122
|
"this is a string with custom separators"
|
|
123
123
|
);
|
|
124
124
|
});
|
|
125
|
+
|
|
126
|
+
it("does not HTML-escape search variables containing single quotes or apostrophes", () => {
|
|
127
|
+
const string = "https://api.example.com/search?q={{search.query}}";
|
|
128
|
+
const data = { search: { query: "L'aventure" } };
|
|
129
|
+
|
|
130
|
+
expect(inflateString({ string, data })).toBe(
|
|
131
|
+
"https://api.example.com/search?q=L'aventure"
|
|
132
|
+
);
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
it("search context with straight apostrophe", () => {
|
|
136
|
+
const string =
|
|
137
|
+
"https://api.pc-cms.tele.quebec/api/applicaster/v1/tv/search?q={{search.q}}";
|
|
138
|
+
|
|
139
|
+
const data = { search: { q: "l'a" } };
|
|
140
|
+
|
|
141
|
+
expect(inflateString({ string, data })).toBe(
|
|
142
|
+
"https://api.pc-cms.tele.quebec/api/applicaster/v1/tv/search?q=l'a"
|
|
143
|
+
);
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
it("search context with backtick apostrophe", () => {
|
|
147
|
+
const string =
|
|
148
|
+
"https://api.pc-cms.tele.quebec/api/applicaster/v1/tv/search?q={{search.q}}";
|
|
149
|
+
|
|
150
|
+
const data = { search: { q: "l`a" } };
|
|
151
|
+
|
|
152
|
+
expect(inflateString({ string, data })).toBe(
|
|
153
|
+
"https://api.pc-cms.tele.quebec/api/applicaster/v1/tv/search?q=l`a"
|
|
154
|
+
);
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
it("search context with right single quotation mark", () => {
|
|
158
|
+
const string =
|
|
159
|
+
"https://api.pc-cms.tele.quebec/api/applicaster/v1/tv/search?q={{search.q}}";
|
|
160
|
+
|
|
161
|
+
const data = { search: { q: "l’a" } };
|
|
162
|
+
|
|
163
|
+
expect(inflateString({ string, data })).toBe(
|
|
164
|
+
"https://api.pc-cms.tele.quebec/api/applicaster/v1/tv/search?q=l’a"
|
|
165
|
+
);
|
|
166
|
+
});
|
|
125
167
|
});
|
|
126
168
|
|
|
127
169
|
describe("objectToReadableString", () => {
|
package/stringUtils/index.ts
CHANGED
|
@@ -72,8 +72,8 @@ const valueIsNull = (value) => () => value === null;
|
|
|
72
72
|
const encodeHelper = "encodeURL";
|
|
73
73
|
|
|
74
74
|
const encodeHandlebarsExpressions = R.compose(
|
|
75
|
-
R.replace(/{{([^}]+)}}/g, (
|
|
76
|
-
p1.includes("search") ?
|
|
75
|
+
R.replace(/{{([^}]+)}}/g, (_, p1) =>
|
|
76
|
+
p1.includes("search") ? `{{{${p1}}}}` : `{{{${encodeHelper} ${p1}}}}`
|
|
77
77
|
)
|
|
78
78
|
);
|
|
79
79
|
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import {
|
|
2
|
+
batchSaveOwnedValues,
|
|
3
|
+
batchRemoveOwnedValues,
|
|
4
|
+
} from "../localStorageHelper";
|
|
5
|
+
import { localStorage } from "@applicaster/zapp-react-native-bridge/ZappStorage/LocalStorage";
|
|
6
|
+
|
|
7
|
+
// Mock localStorage
|
|
8
|
+
jest.mock(
|
|
9
|
+
"@applicaster/zapp-react-native-bridge/ZappStorage/LocalStorage",
|
|
10
|
+
() => ({
|
|
11
|
+
localStorage: {
|
|
12
|
+
getItem: jest.fn(),
|
|
13
|
+
setItem: jest.fn(),
|
|
14
|
+
removeItem: jest.fn(),
|
|
15
|
+
},
|
|
16
|
+
})
|
|
17
|
+
);
|
|
18
|
+
|
|
19
|
+
describe("localStorageHelper", () => {
|
|
20
|
+
const ownershipKey = "test_ownership_key";
|
|
21
|
+
const ownershipNamespace = "test_namespace";
|
|
22
|
+
|
|
23
|
+
beforeEach(() => {
|
|
24
|
+
jest.clearAllMocks();
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
describe("addOwnedKeys via batchSaveOwnedValues", () => {
|
|
28
|
+
it("should add new keys to existing owned keys", async () => {
|
|
29
|
+
// Setup existing keys in storage
|
|
30
|
+
const existingKeys = {
|
|
31
|
+
namespace1: ["key1"],
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
(localStorage.getItem as jest.Mock).mockResolvedValue(
|
|
35
|
+
JSON.stringify(existingKeys)
|
|
36
|
+
);
|
|
37
|
+
|
|
38
|
+
const newValues = {
|
|
39
|
+
namespace1: { key2: "value2" },
|
|
40
|
+
namespace2: { key3: "value3" },
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
await batchSaveOwnedValues({
|
|
44
|
+
storageValues: newValues,
|
|
45
|
+
ownershipKey,
|
|
46
|
+
ownershipNamespace,
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
// Verify localStorage.setItem was called with correct updated keys
|
|
50
|
+
// Note: order of keys in array might vary depending on Set implementation, but usually insertion order
|
|
51
|
+
// However, to be safe, we can parse the argument and check contents
|
|
52
|
+
|
|
53
|
+
const lastCall = (localStorage.setItem as jest.Mock).mock.calls.find(
|
|
54
|
+
(call) => call[0] === ownershipKey
|
|
55
|
+
);
|
|
56
|
+
|
|
57
|
+
expect(lastCall).toBeDefined();
|
|
58
|
+
const savedData = JSON.parse(lastCall[1]);
|
|
59
|
+
|
|
60
|
+
expect(savedData.namespace1).toContain("key1");
|
|
61
|
+
expect(savedData.namespace1).toContain("key2");
|
|
62
|
+
expect(savedData.namespace2).toContain("key3");
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it("should handle empty existing keys", async () => {
|
|
66
|
+
(localStorage.getItem as jest.Mock).mockResolvedValue(null);
|
|
67
|
+
|
|
68
|
+
const newValues = {
|
|
69
|
+
namespace1: { key1: "value1" },
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
await batchSaveOwnedValues({
|
|
73
|
+
storageValues: newValues,
|
|
74
|
+
ownershipKey,
|
|
75
|
+
ownershipNamespace,
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
const lastCall = (localStorage.setItem as jest.Mock).mock.calls.find(
|
|
79
|
+
(call) => call[0] === ownershipKey
|
|
80
|
+
);
|
|
81
|
+
|
|
82
|
+
expect(lastCall).toBeDefined();
|
|
83
|
+
const savedData = JSON.parse(lastCall[1]);
|
|
84
|
+
|
|
85
|
+
expect(savedData.namespace1).toEqual(["key1"]);
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
describe("removeOwnedKeys via batchRemoveOwnedValues", () => {
|
|
90
|
+
it("should remove keys from owned keys", async () => {
|
|
91
|
+
const existingKeys = {
|
|
92
|
+
namespace1: ["key1", "key2"],
|
|
93
|
+
namespace2: ["key3"],
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
(localStorage.getItem as jest.Mock).mockResolvedValue(
|
|
97
|
+
JSON.stringify(existingKeys)
|
|
98
|
+
);
|
|
99
|
+
|
|
100
|
+
const keysToRemove = {
|
|
101
|
+
namespace1: ["key1"],
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
await batchRemoveOwnedValues({
|
|
105
|
+
storageValues: keysToRemove,
|
|
106
|
+
ownershipKey,
|
|
107
|
+
ownershipNamespace,
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
const lastCall = (localStorage.setItem as jest.Mock).mock.calls.find(
|
|
111
|
+
(call) => call[0] === ownershipKey
|
|
112
|
+
);
|
|
113
|
+
|
|
114
|
+
expect(lastCall).toBeDefined();
|
|
115
|
+
const savedData = JSON.parse(lastCall[1]);
|
|
116
|
+
|
|
117
|
+
expect(savedData.namespace1).toEqual(["key2"]);
|
|
118
|
+
expect(savedData.namespace2).toEqual(["key3"]);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it("should remove namespace if all keys removed", async () => {
|
|
122
|
+
const existingKeys = {
|
|
123
|
+
namespace1: ["key1"],
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
(localStorage.getItem as jest.Mock).mockResolvedValue(
|
|
127
|
+
JSON.stringify(existingKeys)
|
|
128
|
+
);
|
|
129
|
+
|
|
130
|
+
const keysToRemove = {
|
|
131
|
+
namespace1: ["key1"],
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
await batchRemoveOwnedValues({
|
|
135
|
+
storageValues: keysToRemove,
|
|
136
|
+
ownershipKey,
|
|
137
|
+
ownershipNamespace,
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
expect(localStorage.removeItem).toHaveBeenCalledWith(
|
|
141
|
+
ownershipKey,
|
|
142
|
+
ownershipNamespace
|
|
143
|
+
);
|
|
144
|
+
});
|
|
145
|
+
});
|
|
146
|
+
});
|
|
@@ -2,10 +2,10 @@ import { localStorage } from "@applicaster/zapp-react-native-bridge/ZappStorage/
|
|
|
2
2
|
import { isNilOrEmpty } from "../reactUtils/helpers";
|
|
3
3
|
import { parseJsonIfNeeded } from "@applicaster/zapp-react-native-utils/functionUtils";
|
|
4
4
|
import {
|
|
5
|
-
StorageOwnedValues,
|
|
6
5
|
NamespaceValues,
|
|
7
|
-
|
|
6
|
+
StorageOwnedValues,
|
|
8
7
|
StorageValuesToAdd,
|
|
8
|
+
StorageValuesToRemove,
|
|
9
9
|
} from "./types";
|
|
10
10
|
import { Storage } from "@applicaster/zapp-react-native-bridge/ZappStorage/Storage";
|
|
11
11
|
|
|
@@ -43,10 +43,10 @@ function mapOwnedKeysToAdd(
|
|
|
43
43
|
): StorageOwnedValues {
|
|
44
44
|
const mappedData = {};
|
|
45
45
|
|
|
46
|
-
Object.keys(storageValues)
|
|
46
|
+
for (const namespace of Object.keys(storageValues)) {
|
|
47
47
|
const data = storageValues[namespace];
|
|
48
48
|
mappedData[namespace] = Object.keys(data);
|
|
49
|
-
}
|
|
49
|
+
}
|
|
50
50
|
|
|
51
51
|
return mappedData;
|
|
52
52
|
}
|
|
@@ -59,6 +59,8 @@ export async function batchSave(
|
|
|
59
59
|
return false;
|
|
60
60
|
}
|
|
61
61
|
|
|
62
|
+
const promises = [];
|
|
63
|
+
|
|
62
64
|
for (const namespace of Object.keys(storageValues)) {
|
|
63
65
|
const namespaceData = storageValues[namespace];
|
|
64
66
|
|
|
@@ -66,10 +68,12 @@ export async function batchSave(
|
|
|
66
68
|
const value = namespaceData[key];
|
|
67
69
|
|
|
68
70
|
if (!isNilOrEmpty(value)) {
|
|
69
|
-
|
|
71
|
+
promises.push(storage.setItem(key, value, namespace));
|
|
70
72
|
}
|
|
71
73
|
}
|
|
72
74
|
}
|
|
75
|
+
|
|
76
|
+
await Promise.all(promises);
|
|
73
77
|
}
|
|
74
78
|
|
|
75
79
|
export async function batchSaveToLocalStorage(
|
|
@@ -82,13 +86,17 @@ export async function batchRemoveFromStorage(
|
|
|
82
86
|
storageValues: StorageValuesToRemove,
|
|
83
87
|
storage: Storage = localStorage
|
|
84
88
|
) {
|
|
89
|
+
const promises = [];
|
|
90
|
+
|
|
85
91
|
for (const namespace of Object.keys(storageValues)) {
|
|
86
92
|
const namespaceData = storageValues[namespace];
|
|
87
93
|
|
|
88
94
|
for (const key of namespaceData) {
|
|
89
|
-
|
|
95
|
+
promises.push(storage.removeItem(key, namespace));
|
|
90
96
|
}
|
|
91
97
|
}
|
|
98
|
+
|
|
99
|
+
await Promise.all(promises);
|
|
92
100
|
}
|
|
93
101
|
|
|
94
102
|
export async function batchRemoveFromLocalStorage(
|
|
@@ -111,12 +119,12 @@ async function addOwnedKeys({
|
|
|
111
119
|
ownershipNamespace
|
|
112
120
|
);
|
|
113
121
|
|
|
114
|
-
Object.keys(newKeys)
|
|
122
|
+
for (const namespace of Object.keys(newKeys)) {
|
|
115
123
|
const newOwnedKeys: string[] = newKeys[namespace];
|
|
116
124
|
const currentKeys = allStoragedOwnedKeys[namespace] || [];
|
|
117
125
|
const combinedSet = new Set([...currentKeys, ...newOwnedKeys]);
|
|
118
126
|
allStoragedOwnedKeys[namespace] = Array.from(combinedSet);
|
|
119
|
-
}
|
|
127
|
+
}
|
|
120
128
|
|
|
121
129
|
const data = JSON.stringify(allStoragedOwnedKeys);
|
|
122
130
|
await localStorage.setItem(ownershipKey, data, ownershipNamespace);
|
|
@@ -136,18 +144,14 @@ async function removeOwnedKeys({
|
|
|
136
144
|
ownershipNamespace
|
|
137
145
|
);
|
|
138
146
|
|
|
139
|
-
Object.keys(toRemoveKeys)
|
|
147
|
+
for (const namespace of Object.keys(toRemoveKeys)) {
|
|
140
148
|
const keysToRemove: string[] = toRemoveKeys[namespace] || [];
|
|
141
149
|
const currentKeys: string[] = currentKeysList[namespace] || [];
|
|
142
150
|
|
|
143
151
|
const storageOwnedSet = new Set(currentKeys);
|
|
144
152
|
const keysToRemoveSet = new Set(keysToRemove);
|
|
145
153
|
|
|
146
|
-
|
|
147
|
-
toBeRemovedSet.forEach(Set.prototype.delete, originalSet);
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
removeAll(storageOwnedSet, keysToRemoveSet);
|
|
154
|
+
keysToRemoveSet.forEach((key) => storageOwnedSet.delete(key));
|
|
151
155
|
const newLoginKeys = Array.from(storageOwnedSet);
|
|
152
156
|
|
|
153
157
|
if (isNilOrEmpty(newLoginKeys)) {
|
|
@@ -155,7 +159,7 @@ async function removeOwnedKeys({
|
|
|
155
159
|
} else {
|
|
156
160
|
currentKeysList[namespace] = newLoginKeys;
|
|
157
161
|
}
|
|
158
|
-
}
|
|
162
|
+
}
|
|
159
163
|
|
|
160
164
|
if (isNilOrEmpty(currentKeysList)) {
|
|
161
165
|
await localStorage.removeItem(ownershipKey, ownershipNamespace);
|