@applicaster/zapp-react-native-utils 16.0.0-alpha.5652127751 → 16.0.0-alpha.5803191443

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.
@@ -0,0 +1,65 @@
1
+ import {
2
+ resolveAssets,
3
+ resolveConfiguredAsset,
4
+ } from "@applicaster/zapp-react-native-utils/actionUtils";
5
+
6
+ describe("actionUtils", () => {
7
+ describe("resolveConfiguredAsset", () => {
8
+ it("returns configured asset when value is a non-empty string", () => {
9
+ expect(resolveConfiguredAsset("configured", "fallback")).toBe(
10
+ "configured"
11
+ );
12
+
13
+ expect(
14
+ resolveConfiguredAsset("https://example.com/icon.png", "fallback")
15
+ ).toBe("https://example.com/icon.png");
16
+ });
17
+
18
+ it("returns fallback when configured asset is empty or undefined", () => {
19
+ expect(resolveConfiguredAsset("", "fallback")).toBe("fallback");
20
+ expect(resolveConfiguredAsset(" ", "fallback")).toBe("fallback");
21
+ expect(resolveConfiguredAsset(" ", "fallback")).toBe("fallback");
22
+ expect(resolveConfiguredAsset(undefined, "fallback")).toBe("fallback");
23
+ });
24
+
25
+ it("returns fallback for non-string configured asset values", () => {
26
+ expect(resolveConfiguredAsset(null as any, "fallback")).toBe("fallback");
27
+ expect(resolveConfiguredAsset(123 as any, "fallback")).toBe("fallback");
28
+ expect(resolveConfiguredAsset({} as any, "fallback")).toBe("fallback");
29
+ });
30
+ });
31
+
32
+ describe("resolveAssets", () => {
33
+ it("returns configured assets when both flavour values are configured", () => {
34
+ expect(
35
+ resolveAssets("asset-1", "asset-2", ["default-1", "default-2"])
36
+ ).toEqual(["asset-1", "asset-2"]);
37
+ });
38
+
39
+ it("falls back to code defaults when configured flavour values are empty", () => {
40
+ expect(resolveAssets("", undefined, ["default-1", "default-2"])).toEqual([
41
+ "default-1",
42
+ "default-2",
43
+ ]);
44
+ });
45
+
46
+ it("falls back per flavour independently", () => {
47
+ expect(resolveAssets("", "asset-2", ["default-1", "default-2"])).toEqual([
48
+ "default-1",
49
+ "asset-2",
50
+ ]);
51
+ });
52
+
53
+ it("uses the cell fallback before code defaults", () => {
54
+ expect(
55
+ resolveAssets("", undefined, ["default-1", "default-2"], "cell")
56
+ ).toEqual(["cell", "cell"]);
57
+ });
58
+
59
+ it("preserves configured assets before using the cell fallback", () => {
60
+ expect(
61
+ resolveAssets("asset-1", "", ["default-1", "default-2"], "cell")
62
+ ).toEqual(["asset-1", "cell"]);
63
+ });
64
+ });
65
+ });
@@ -1,7 +1,30 @@
1
1
  import { Image } from "react-native";
2
2
  import { isTV } from "../reactUtils";
3
3
 
4
+ export type Asset = [string, string] | [string];
5
+
4
6
  export const resolveDefaultAssetUri = (source: string | number): string =>
5
7
  isTV()
6
8
  ? (source as string)
7
9
  : Image.resolveAssetSource(source as number)?.uri || "";
10
+
11
+ const isConfiguredAsset = (asset?: string | null): asset is string =>
12
+ typeof asset === "string" && asset.trim().length > 0;
13
+
14
+ export const resolveConfiguredAsset = (
15
+ configuredAsset: string | undefined,
16
+ fallbackAsset: string
17
+ ): string =>
18
+ isConfiguredAsset(configuredAsset) ? configuredAsset : fallbackAsset;
19
+
20
+ /** Resolves assets in the following order: configured asset(can be empty), fallback asset(cell asset, can be empty), default asset */
21
+ export const resolveAssets = <T extends Asset>(
22
+ flavour1Asset: string | undefined,
23
+ flavour2Asset: string | undefined,
24
+ defaultAssets: T,
25
+ fallbackAssetSrc?: string
26
+ ): T =>
27
+ [
28
+ resolveConfiguredAsset(flavour1Asset, fallbackAssetSrc || defaultAssets[0]),
29
+ resolveConfiguredAsset(flavour2Asset, fallbackAssetSrc || defaultAssets[1]),
30
+ ] as T;
@@ -19,35 +19,52 @@ type PlayerFactoryProps = {
19
19
  playerRole: PlayerRole;
20
20
  };
21
21
 
22
- interface PlayerControllerConstructor {
22
+ interface PlayerConstructor {
23
23
  new (params: any): Player;
24
24
  }
25
25
 
26
- type PlayerProtocol = {
27
- Component: any;
28
- controllerClass: PlayerControllerConstructor;
29
- };
30
-
31
- export const playerFactory = async (
26
+ export const playerFactory = (
32
27
  config: PlayerFactoryProps
33
- ): Promise<PlayerFactoryItem | null> => {
34
- if (!config?.playerPluginId) return null;
28
+ ): PlayerFactoryItem | null => {
29
+ const IDENTIFIER = config?.playerPluginId;
30
+
31
+ if (!IDENTIFIER) {
32
+ return null;
33
+ }
35
34
 
36
35
  const plugins = appStore.get("plugins");
37
- if (!plugins) return null;
38
36
 
39
- const playerPlugin = findPluginByIdentifier(config.playerPluginId, plugins);
40
- if (!playerPlugin?.module?.playerProtocol) return null;
37
+ if (!plugins) {
38
+ return null;
39
+ }
40
+
41
+ const playerPlugin = findPluginByIdentifier(IDENTIFIER, plugins);
41
42
 
42
- const playerProtocol: PlayerProtocol | null =
43
- await playerPlugin.module.playerProtocol(config.screenConfig, config.entry);
43
+ if (!playerPlugin?.module?.playerProtocol) {
44
+ return null;
45
+ }
46
+
47
+ const playerProtocol: PlayerConstructor = playerPlugin.module.playerProtocol(
48
+ config?.screenConfig,
49
+ config?.entry
50
+ );
44
51
 
45
- if (!playerProtocol?.Component || !playerProtocol.controllerClass) {
52
+ const playerView = (playerProtocol as any)?.Component || null;
53
+
54
+ if (!playerView) {
46
55
  return null;
47
56
  }
48
57
 
58
+ const Controller = (playerProtocol as any)?.controllerClass || null;
59
+
60
+ if (Controller === null) {
61
+ return null;
62
+ }
63
+
64
+ const controller = new Controller(config);
65
+
49
66
  return {
50
- controller: new playerProtocol.controllerClass(config),
51
- Component: playerProtocol.Component,
67
+ controller,
68
+ Component: playerView,
52
69
  };
53
70
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@applicaster/zapp-react-native-utils",
3
- "version": "16.0.0-alpha.5652127751",
3
+ "version": "16.0.0-alpha.5803191443",
4
4
  "description": "Applicaster Zapp React Native utilities package",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -27,7 +27,7 @@
27
27
  },
28
28
  "homepage": "https://github.com/applicaster/quickbrick#readme",
29
29
  "dependencies": {
30
- "@applicaster/applicaster-types": "16.0.0-alpha.5652127751",
30
+ "@applicaster/applicaster-types": "16.0.0-alpha.5803191443",
31
31
  "buffer": "^5.2.1",
32
32
  "camelize": "^1.0.0",
33
33
  "dayjs": "^1.11.10",
@@ -1,150 +0,0 @@
1
- const mockAppStoreGet = jest.fn();
2
- const mockFindPluginByIdentifier = jest.fn();
3
-
4
- jest.mock("@applicaster/zapp-react-native-redux/AppStore", () => ({
5
- appStore: { get: (key: string) => mockAppStoreGet(key) },
6
- }));
7
-
8
- jest.mock("@applicaster/zapp-react-native-utils/pluginUtils", () => ({
9
- findPluginByIdentifier: (id: string, plugins: any) =>
10
- mockFindPluginByIdentifier(id, plugins),
11
- }));
12
-
13
- import { PlayerRole } from "../conts";
14
- import { playerFactory } from "../playerFactory";
15
-
16
- const fakeComponent = () => null;
17
-
18
- class FakeController {
19
- receivedConfig: any;
20
- constructor(config: any) {
21
- this.receivedConfig = config;
22
- }
23
- }
24
-
25
- const baseConfig = {
26
- player: {},
27
- playerId: "p1",
28
- autoplay: false,
29
- entry: { id: "entry-1" } as any,
30
- muted: false,
31
- playerPluginId: "test-plugin",
32
- screenConfig: { foo: "bar" },
33
- playerRole: PlayerRole.Cell,
34
- };
35
-
36
- const installPluginWithProtocol = (
37
- protocolReturn: any | ((screenConfig: any, entry: any) => any)
38
- ) => {
39
- const playerProtocol =
40
- typeof protocolReturn === "function"
41
- ? protocolReturn
42
- : () => protocolReturn;
43
-
44
- mockAppStoreGet.mockImplementation((key: string) =>
45
- key === "plugins" ? { "test-plugin": {} } : null
46
- );
47
-
48
- mockFindPluginByIdentifier.mockReturnValue({
49
- module: { playerProtocol },
50
- });
51
- };
52
-
53
- describe("playerFactory", () => {
54
- beforeEach(() => {
55
- mockAppStoreGet.mockReset();
56
- mockFindPluginByIdentifier.mockReset();
57
- });
58
-
59
- it("resolves with controller and Component when playerProtocol returns synchronously", async () => {
60
- installPluginWithProtocol({
61
- Component: fakeComponent,
62
- controllerClass: FakeController,
63
- });
64
-
65
- const item = await playerFactory(baseConfig);
66
-
67
- expect(item).not.toBeNull();
68
- expect(item?.Component).toBe(fakeComponent);
69
- expect(item?.controller).toBeInstanceOf(FakeController);
70
-
71
- expect((item?.controller as FakeController).receivedConfig).toBe(
72
- baseConfig
73
- );
74
- });
75
-
76
- it("resolves with controller and Component when playerProtocol returns a Promise (delayed import)", async () => {
77
- installPluginWithProtocol(async () => ({
78
- Component: fakeComponent,
79
- controllerClass: FakeController,
80
- }));
81
-
82
- const item = await playerFactory(baseConfig);
83
-
84
- expect(item).not.toBeNull();
85
- expect(item?.Component).toBe(fakeComponent);
86
- expect(item?.controller).toBeInstanceOf(FakeController);
87
- });
88
-
89
- it("passes screenConfig and entry through to playerProtocol", async () => {
90
- const playerProtocol = jest.fn(() => ({
91
- Component: fakeComponent,
92
- controllerClass: FakeController,
93
- }));
94
-
95
- mockAppStoreGet.mockImplementation((key: string) =>
96
- key === "plugins" ? { "test-plugin": {} } : null
97
- );
98
-
99
- mockFindPluginByIdentifier.mockReturnValue({ module: { playerProtocol } });
100
-
101
- await playerFactory(baseConfig);
102
-
103
- expect(playerProtocol).toHaveBeenCalledWith(
104
- baseConfig.screenConfig,
105
- baseConfig.entry
106
- );
107
- });
108
-
109
- it("resolves to null when playerPluginId is missing", async () => {
110
- expect(
111
- await playerFactory({ ...baseConfig, playerPluginId: "" } as any)
112
- ).toBeNull();
113
- });
114
-
115
- it("resolves to null when appStore has no plugins", async () => {
116
- mockAppStoreGet.mockReturnValue(null);
117
-
118
- expect(await playerFactory(baseConfig)).toBeNull();
119
- });
120
-
121
- it("resolves to null when the plugin has no playerProtocol", async () => {
122
- mockAppStoreGet.mockImplementation((key: string) =>
123
- key === "plugins" ? { "test-plugin": {} } : null
124
- );
125
-
126
- mockFindPluginByIdentifier.mockReturnValue({ module: {} });
127
-
128
- expect(await playerFactory(baseConfig)).toBeNull();
129
- });
130
-
131
- it("resolves to null when playerProtocol returns without a Component", async () => {
132
- installPluginWithProtocol({ controllerClass: FakeController });
133
-
134
- expect(await playerFactory(baseConfig)).toBeNull();
135
- });
136
-
137
- it("resolves to null when playerProtocol returns without a controllerClass", async () => {
138
- installPluginWithProtocol({ Component: fakeComponent });
139
-
140
- expect(await playerFactory(baseConfig)).toBeNull();
141
- });
142
-
143
- it("rejects when playerProtocol throws synchronously", async () => {
144
- installPluginWithProtocol(() => {
145
- throw new Error("boom");
146
- });
147
-
148
- await expect(playerFactory(baseConfig)).rejects.toThrow("boom");
149
- });
150
- });