@applicaster/zapp-react-native-utils 12.0.0-rc.25 → 12.0.0-rc.26

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.
@@ -1,18 +1,18 @@
1
+ import * as ReactNative from "react-native";
2
+
1
3
  const mock_postEvent = jest.fn();
2
4
  const mock_setPlugins = jest.fn();
3
5
  const mock_postTimedEvent = jest.fn();
4
6
  const mock_endTimedEvent = jest.fn();
5
7
 
6
- jest.mock("react-native", () => ({
7
- NativeModules: {
8
- AnalyticsBridge: {
9
- postEvent: mock_postEvent,
10
- setPlugins: mock_setPlugins,
11
- postTimedEvent: mock_postTimedEvent,
12
- endTimedEvent: mock_endTimedEvent,
13
- },
14
- },
15
- }));
8
+ const AnalyticsBridge = {
9
+ postEvent: mock_postEvent,
10
+ setPlugins: mock_setPlugins,
11
+ postTimedEvent: mock_postTimedEvent,
12
+ endTimedEvent: mock_endTimedEvent,
13
+ };
14
+
15
+ ReactNative.NativeModules.AnalyticsBridge = AnalyticsBridge;
16
16
 
17
17
  const eventName = "event";
18
18
  const payload = {};
@@ -26,90 +26,92 @@ function clearAllMocks() {
26
26
  mock_endTimedEvent.mockClear();
27
27
  }
28
28
 
29
- describe("isValidAnalyticsData", () => {
30
- beforeEach(clearAllMocks);
29
+ describe("AnalyticsManager", () => {
30
+ describe("isValidAnalyticsData", () => {
31
+ beforeEach(clearAllMocks);
31
32
 
32
- it("returns false if eventName is not a string", () => {
33
- expect(analyticsManager.isValidAnalyticsData(42)).toBe(false);
34
- expect(analyticsManager.isValidAnalyticsData({})).toBe(false);
35
- expect(analyticsManager.isValidAnalyticsData([])).toBe(false);
36
- expect(analyticsManager.isValidAnalyticsData(() => ({}))).toBe(false);
37
- });
33
+ it("returns false if eventName is not a string", () => {
34
+ expect(analyticsManager.isValidAnalyticsData(42)).toBe(false);
35
+ expect(analyticsManager.isValidAnalyticsData({})).toBe(false);
36
+ expect(analyticsManager.isValidAnalyticsData([])).toBe(false);
37
+ expect(analyticsManager.isValidAnalyticsData(() => ({}))).toBe(false);
38
+ });
38
39
 
39
- it("returns false if payload is not an object", () => {
40
- expect(analyticsManager.isValidAnalyticsData(eventName, "string")).toBe(
41
- false
42
- );
40
+ it("returns false if payload is not an object", () => {
41
+ expect(analyticsManager.isValidAnalyticsData(eventName, "string")).toBe(
42
+ false
43
+ );
43
44
 
44
- expect(analyticsManager.isValidAnalyticsData(eventName, 42)).toBe(false);
45
+ expect(analyticsManager.isValidAnalyticsData(eventName, 42)).toBe(false);
45
46
 
46
- expect(analyticsManager.isValidAnalyticsData(eventName, () => ({}))).toBe(
47
- false
48
- );
49
- });
47
+ expect(analyticsManager.isValidAnalyticsData(eventName, () => ({}))).toBe(
48
+ false
49
+ );
50
+ });
50
51
 
51
- it("returns true if eventName is a string and payload is null or an object", () => {
52
- expect(analyticsManager.isValidAnalyticsData(eventName)).toBe(true);
52
+ it("returns true if eventName is a string and payload is null or an object", () => {
53
+ expect(analyticsManager.isValidAnalyticsData(eventName)).toBe(true);
53
54
 
54
- expect(analyticsManager.isValidAnalyticsData(eventName, payload)).toBe(
55
- true
56
- );
55
+ expect(analyticsManager.isValidAnalyticsData(eventName, payload)).toBe(
56
+ true
57
+ );
58
+ });
57
59
  });
58
- });
59
60
 
60
- describe("postAnalyticEvent", () => {
61
- beforeEach(clearAllMocks);
61
+ describe("postAnalyticEvent", () => {
62
+ beforeEach(clearAllMocks);
62
63
 
63
- it("calls the postEvent method of the analytic bridge", () => {
64
- analyticsManager.postAnalyticEvent(eventName, payload);
65
- expect(mock_postEvent).toHaveBeenCalledWith(eventName, payload);
66
- });
64
+ it("calls the postEvent method of the analytic bridge", () => {
65
+ analyticsManager.postAnalyticEvent(eventName, payload);
66
+ expect(mock_postEvent).toHaveBeenCalledWith(eventName, payload);
67
+ });
67
68
 
68
- it("skips the event if it is not valid", () => {
69
- analyticsManager.postAnalyticEvent(42);
70
- expect(mock_postTimedEvent).not.toHaveBeenCalled();
69
+ it("skips the event if it is not valid", () => {
70
+ analyticsManager.postAnalyticEvent(42);
71
+ expect(mock_postTimedEvent).not.toHaveBeenCalled();
72
+ });
71
73
  });
72
- });
73
74
 
74
- describe("providePlugins", () => {
75
- beforeEach(clearAllMocks);
75
+ describe("providePlugins", () => {
76
+ beforeEach(clearAllMocks);
76
77
 
77
- it("calls the setPlugins method of the analytic bridge", () => {
78
- const plugins = [];
79
- const plugin_configurations = {};
80
- const callback = jest.fn();
78
+ it("calls the setPlugins method of the analytic bridge", () => {
79
+ const plugins = [];
80
+ const plugin_configurations = {};
81
+ const callback = jest.fn();
81
82
 
82
- analyticsManager.providePlugins(plugins, plugin_configurations, callback);
83
+ analyticsManager.providePlugins(plugins, plugin_configurations, callback);
83
84
 
84
- expect(mock_setPlugins).toHaveBeenCalledWith(
85
- plugins,
86
- plugin_configurations,
87
- callback
88
- );
85
+ expect(mock_setPlugins).toHaveBeenCalledWith(
86
+ plugins,
87
+ plugin_configurations,
88
+ callback
89
+ );
90
+ });
89
91
  });
90
- });
91
92
 
92
- describe("startAnalyticsTimedEvent", () => {
93
- beforeEach(clearAllMocks);
93
+ describe("startAnalyticsTimedEvent", () => {
94
+ beforeEach(clearAllMocks);
94
95
 
95
- it("skips the event if it is not valid", () => {
96
- analyticsManager.startAnalyticsTimedEvent(42);
97
- expect(mock_postTimedEvent).not.toHaveBeenCalled();
96
+ it("skips the event if it is not valid", () => {
97
+ analyticsManager.startAnalyticsTimedEvent(42);
98
+ expect(mock_postTimedEvent).not.toHaveBeenCalled();
99
+ });
98
100
  });
99
- });
100
101
 
101
- describe("endAnalyticsTimedEvent", () => {
102
- beforeEach(clearAllMocks);
102
+ describe("endAnalyticsTimedEvent", () => {
103
+ beforeEach(clearAllMocks);
103
104
 
104
- it("emits a time_on_screen event and adds duration to payload", () => {
105
- analyticsManager.endAnalyticsTimedEvent(eventName, payload);
106
- const newEventName = "time_on_screen";
107
- payload.duration = "N/A";
108
- expect(mock_postEvent).toHaveBeenCalledWith(newEventName, payload);
109
- });
105
+ it("emits a time_on_screen event and adds duration to payload", () => {
106
+ analyticsManager.endAnalyticsTimedEvent(eventName, payload);
107
+ const newEventName = "time_on_screen";
108
+ payload.duration = "N/A";
109
+ expect(mock_postEvent).toHaveBeenCalledWith(newEventName, payload);
110
+ });
110
111
 
111
- it("skips the event if it is not valid", () => {
112
- analyticsManager.endAnalyticsTimedEvent(42);
113
- expect(mock_postEvent).not.toHaveBeenCalled();
112
+ it("skips the event if it is not valid", () => {
113
+ analyticsManager.endAnalyticsTimedEvent(42);
114
+ expect(mock_postEvent).not.toHaveBeenCalled();
115
+ });
114
116
  });
115
117
  });
@@ -1,6 +1,6 @@
1
1
  // Jest Snapshot v1, https://goo.gl/fbAQLP
2
2
 
3
- exports[`keyCode returns an object for a given key event 1`] = `
3
+ exports[`keyCodes keyCode returns an object for a given key event 1`] = `
4
4
  Object {
5
5
  "direction": [Function],
6
6
  "matches": [Function],
@@ -1,3 +1,7 @@
1
+ const { Platform } = require("react-native");
2
+
3
+ Platform.OS = "web";
4
+
1
5
  import { directionHelper } from "../../directionHelper";
2
6
 
3
7
  const globalTizen = {
@@ -6,105 +10,96 @@ const globalTizen = {
6
10
  },
7
11
  };
8
12
 
9
- jest.mock("react-native", () => {
10
- return { Platform: { OS: "web" } };
11
- });
12
-
13
- describe("keyCode", () => {
14
- const { keyCode } = require("../index");
15
- const { KEYS } = require("../keys.web");
16
-
17
- it("returns an object for a given key event", () => {
18
- expect(keyCode(KEYS.Enter)).toMatchSnapshot();
19
- });
20
-
21
- it("has a matcher method for a single key", () => {
22
- expect(keyCode(KEYS.Enter).matches("Enter")).toBeTruthy();
23
- expect(keyCode(KEYS.Enter).matches(KEYS.Enter)).toBeTruthy();
24
- expect(keyCode(KEYS.Back).matches("Enter")).toBeFalsy();
25
- });
26
-
27
- it("has matcher for multiple keys", () => {
28
- expect(
29
- keyCode(KEYS.Enter).matchesAny("Enter", "Back", "ArrowUp")
30
- ).toBeTruthy();
13
+ describe("keyCodes", () => {
14
+ describe("keyCode", () => {
15
+ const { keyCode } = require("..");
16
+ const { KEYS } = require("../keys.web");
31
17
 
32
- expect(
33
- keyCode(KEYS.Enter).matchesAny("Back", "ArrowUp", "ArrowDown")
34
- ).toBeFalsy();
35
- });
18
+ it("returns an object for a given key event", () => {
19
+ expect(keyCode(KEYS.Enter)).toMatchSnapshot();
20
+ });
36
21
 
37
- it("returns the direction if it matches an arrow key", () => {
38
- expect(keyCode(KEYS.Enter).direction()).toBe(null);
22
+ it("has a matcher method for a single key", () => {
23
+ expect(keyCode(KEYS.Enter).matches("Enter")).toBeTruthy();
24
+ expect(keyCode(KEYS.Enter).matches(KEYS.Enter)).toBeTruthy();
25
+ expect(keyCode(KEYS.Back).matches("Enter")).toBeFalsy();
26
+ });
39
27
 
40
- expect(keyCode(KEYS.ArrowDown).direction()).toEqual(
41
- directionHelper(KEYS.ArrowDown)
42
- );
43
- });
28
+ it("has matcher for multiple keys", () => {
29
+ expect(
30
+ keyCode(KEYS.Enter).matchesAny("Enter", "Back", "ArrowUp")
31
+ ).toBeTruthy();
44
32
 
45
- describe("in web env", () => {
46
- it("uses the proper keyCode for the environment", () => {
47
- expect(keyCode({ keyCode: 13 }).matches(KEYS.Enter)).toBeTruthy();
33
+ expect(
34
+ keyCode(KEYS.Enter).matchesAny("Back", "ArrowUp", "ArrowDown")
35
+ ).toBeFalsy();
48
36
  });
49
- });
50
- });
51
37
 
52
- describe("in Tizen Env", () => {
53
- jest.mock("react-native", () => {
54
- return { Platform: { OS: "samsung_tv" } };
55
- });
38
+ it("returns the direction if it matches an arrow key", () => {
39
+ expect(keyCode(KEYS.Enter).direction()).toBe(null);
56
40
 
57
- beforeEach(() => {
58
- global.tizen = globalTizen;
59
- jest.resetModules();
60
- });
41
+ expect(keyCode(KEYS.ArrowDown).direction()).toEqual(
42
+ directionHelper(KEYS.ArrowDown)
43
+ );
44
+ });
61
45
 
62
- afterEach(() => {
63
- global.tizen = undefined;
46
+ describe("in web env", () => {
47
+ it("uses the proper keyCode for the environment", () => {
48
+ expect(keyCode({ keyCode: 13 }).matches(KEYS.Enter)).toBeTruthy();
49
+ });
50
+ });
64
51
  });
65
52
 
66
- it("uses the proper keyCode for the environment", () => {
67
- const { keyCode } = require("../index");
68
- const { KEYS } = require("../keys.web");
53
+ describe("in Tizen Env", () => {
54
+ beforeEach(() => {
55
+ jest.resetModules();
56
+ global.tizen = globalTizen;
57
+ });
69
58
 
70
- expect(globalTizen.tvinputdevice.registerKeyBatch).toHaveBeenCalledWith([
71
- "Back",
72
- "MediaPlay",
73
- "MediaPlayPause",
74
- "MediaPause",
75
- "MediaFastForward",
76
- "MediaRewind",
77
- "MediaStop",
78
- "Exit",
79
- "0",
80
- "1",
81
- "2",
82
- "3",
83
- "4",
84
- "5",
85
- "6",
86
- "7",
87
- "8",
88
- "9",
89
- ]);
90
-
91
- expect(keyCode({ keyCode: 10009 }).matches(KEYS.Back)).toBeTruthy();
92
- });
93
- });
59
+ afterEach(() => {
60
+ global.tizen = undefined;
61
+ });
94
62
 
95
- describe("in WebOS Env", () => {
96
- jest.mock("react-native", () => {
97
- return { Platform: { OS: "lg_tv" } };
63
+ it("uses the proper keyCode for the environment", () => {
64
+ Platform.OS = "samsung_tv";
65
+ const { keyCode } = require("../");
66
+ const { KEYS } = require("../keys.web");
67
+
68
+ expect(globalTizen.tvinputdevice.registerKeyBatch).toHaveBeenCalledWith([
69
+ "MediaPlay",
70
+ "MediaPlayPause",
71
+ "MediaPause",
72
+ "MediaFastForward",
73
+ "MediaRewind",
74
+ "MediaStop",
75
+ "Exit",
76
+ "0",
77
+ "1",
78
+ "2",
79
+ "3",
80
+ "4",
81
+ "5",
82
+ "6",
83
+ "7",
84
+ "8",
85
+ "9",
86
+ ]);
87
+
88
+ expect(keyCode({ keyCode: 10009 }).matches(KEYS.Back)).toBeTruthy();
89
+ });
98
90
  });
99
91
 
100
- beforeEach(() => {
101
- jest.resetModules();
102
- });
92
+ describe("in WebOS Env", () => {
93
+ beforeEach(() => {
94
+ jest.resetModules();
95
+ });
103
96
 
104
- it("uses the proper keyCode for the environment", () => {
105
- const { keyCode } = require("../index");
106
- const { KEYS } = require("../keys.web");
107
- // Back is reserved on LG
108
- expect(keyCode({ keyCode: 417 }).matches(KEYS.Forward)).toBeTruthy();
97
+ it("uses the proper keyCode for the environment", () => {
98
+ Platform.OS = "lg_tv";
99
+ const { keyCode } = require("..");
100
+ const { KEYS } = require("../keys.web");
101
+ // Back is reserved on LG
102
+ expect(keyCode({ keyCode: 417 }).matches(KEYS.Forward)).toBeTruthy();
103
+ });
109
104
  });
110
105
  });
@@ -1,5 +1,6 @@
1
1
  /* global tizen */
2
2
  const isTizen = typeof tizen !== "undefined";
3
+
3
4
  import * as platformKeys from "./keysByPlatform";
4
5
  import { Platform } from "react-native";
5
6
 
@@ -14,6 +14,7 @@ import {
14
14
  castAndFallbackColor,
15
15
  compact,
16
16
  getValue,
17
+ getColorFromData,
17
18
  } from "../";
18
19
 
19
20
  import {
@@ -23,6 +24,7 @@ import {
23
24
  EMPTY,
24
25
  FALLBACK,
25
26
  TRANSFORM,
27
+ defaultColor,
26
28
  } from "./fixtures";
27
29
 
28
30
  describe("getKeyForLabel", () => {
@@ -388,3 +390,60 @@ describe("getValue", () => {
388
390
  expect(getValue({ value })(value)).toBeNull();
389
391
  });
390
392
  });
393
+
394
+ describe("getColorFromData", () => {
395
+ it("should return value directly if valid color", () => {
396
+ const result = getColorFromData({
397
+ data: entry,
398
+ key: "background_color",
399
+ getValue: getValue(componentStyle),
400
+ defaultColor,
401
+ });
402
+
403
+ expect(result).toBe("#123456");
404
+ });
405
+
406
+ it("should return valid rgba color name", () => {
407
+ const result = getColorFromData({
408
+ data: entry,
409
+ key: "component_background_color",
410
+ getValue: getValue(componentStyle),
411
+ defaultColor,
412
+ });
413
+
414
+ expect(result).toBe("rgba(255, 255, 255, 0)");
415
+ });
416
+
417
+ it("should return path value if path value is set and is valid color", () => {
418
+ const result = getColorFromData({
419
+ data: entry,
420
+ key: VAL.colorKeyPath,
421
+ getValue: getValue(componentStyle),
422
+ defaultColor,
423
+ });
424
+
425
+ expect(result).toBe("#123456");
426
+ });
427
+
428
+ it("should return defaultColor if key value and path value are invalid", () => {
429
+ const result = getColorFromData({
430
+ data: entry,
431
+ key: VAL.invalidColorKey,
432
+ getValue: getValue(componentStyle),
433
+ defaultColor,
434
+ });
435
+
436
+ expect(result).toBe(defaultColor);
437
+ });
438
+
439
+ it("should return defaultColor if path does not exist", () => {
440
+ const result = getColorFromData({
441
+ data: entry,
442
+ key: VAL.invalidColorKeyPath,
443
+ getValue: getValue(componentStyle),
444
+ defaultColor,
445
+ });
446
+
447
+ expect(result).toBe(defaultColor);
448
+ });
449
+ });
@@ -1,5 +1,7 @@
1
1
  /* eslint-disable max-len */
2
2
 
3
+ export const defaultColor = "#ffffff";
4
+
3
5
  export const entry = {
4
6
  type: {
5
7
  value: "video",
@@ -26,7 +28,9 @@ export const entry = {
26
28
  type: "video/mp4",
27
29
  src: "https://wino-feed.s3.amazonaws.com/movieFeed/Ford+v+Ferrari.mp4",
28
30
  },
31
+ background_color: "#123456",
29
32
  extensions: {
33
+ background_color: "#123456",
30
34
  free: false,
31
35
  duration: 9000,
32
36
  section: "New Releases",
@@ -46,7 +50,11 @@ export const componentStyle = {
46
50
  horizontal_list_cell_width: "400",
47
51
  cell_plugin_configuration_id: "267bd05a-08bd-4675-bdd3-9b73899773f0",
48
52
  horizontal_list_scaled_switch: false,
53
+ background_color: "#123456",
49
54
  component_background_color: "rgba(255, 255, 255, 0)",
55
+ background_color_1: "extensions.background_color",
56
+ background_color_2: "invalidColorKey",
57
+ background_color_3: "extensions.invalidColorKeyPath",
50
58
  component_padding_left: "",
51
59
  horizontal_list_next_offset: 20,
52
60
  component_margin_bottom: "",
@@ -93,6 +101,11 @@ export const VAL = {
93
101
  upper: "STRING",
94
102
  width: 400,
95
103
  height: 225,
104
+ colorKey: "background_color",
105
+ colorKeyRgba: "component_background_color",
106
+ colorKeyPath: "background_color_1",
107
+ invalidColorKey: "background_color_2",
108
+ invalidColorKeyPath: "background_color_3",
96
109
  };
97
110
 
98
111
  export const TRANSFORM = {
@@ -1,5 +1,6 @@
1
1
  import * as R from "ramda";
2
2
  import dayjs from "dayjs";
3
+ import validateColor from "validate-color";
3
4
 
4
5
  import { toNumberWithDefault, toNumberWithDefaultZero } from "../numberUtils";
5
6
  import { transformColorCode as fixColorHexCode } from "../transform";
@@ -443,3 +444,31 @@ export const toResizeMode = (
443
444
  resizeMode: "contain",
444
445
  };
445
446
  };
447
+
448
+ type GetColorFromData = {
449
+ data: Record<string, any>;
450
+ key: string;
451
+ getValue: Function;
452
+ defaultColor: string;
453
+ };
454
+
455
+ export const getColorFromData = ({
456
+ data,
457
+ key,
458
+ getValue,
459
+ defaultColor,
460
+ }: GetColorFromData): string => {
461
+ const value = getValue(key);
462
+
463
+ if (validateColor(value)) {
464
+ return value;
465
+ }
466
+
467
+ const pathValue = R.path(value.split("."), data);
468
+
469
+ if (pathValue && validateColor(pathValue)) {
470
+ return pathValue;
471
+ }
472
+
473
+ return defaultColor;
474
+ };
@@ -1,6 +1,6 @@
1
1
  // Jest Snapshot v1, https://goo.gl/fbAQLP
2
2
 
3
- exports[`subscriber can be invoked without args 1`] = `
3
+ exports[`functionUtils subscriber can be invoked without args 1`] = `
4
4
  Object {
5
5
  "handlers": Object {},
6
6
  "invokeHandler": [Function],
@@ -9,7 +9,7 @@ Object {
9
9
  }
10
10
  `;
11
11
 
12
- exports[`subscriber returns the original object with additional methods 1`] = `
12
+ exports[`functionUtils subscriber returns the original object with additional methods 1`] = `
13
13
  Object {
14
14
  "foo": "bar",
15
15
  "handlers": Object {},