@applicaster/zapp-react-native-bridge 16.0.0-rc.47 → 16.0.0-rc.49

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.
@@ -7,6 +7,8 @@ import {
7
7
  getItemError,
8
8
  setItemError,
9
9
  removeItemError,
10
+ throwUndefinedKeyError,
11
+ throwUndefinedValueError,
10
12
  } from "../errors";
11
13
 
12
14
  import { Storage, stringifyIfNeeded } from "../Storage";
@@ -14,6 +16,11 @@ import { platformSelect } from "@applicaster/zapp-react-native-utils/reactUtils"
14
16
 
15
17
  const storage = "LocalStorage";
16
18
 
19
+ const UNDEFINED_KEY_MESSAGE = "Requested key is null or undefined";
20
+
21
+ const UNDEFINED_VALUE_MESSAGE =
22
+ "Value to set in storage cannot be null or undefined";
23
+
17
24
  function getDefaultLocalStorage<T = NativeLocalStorageI>(): T {
18
25
  const { LocalStorage } = NativeModules as NativeModulesStatic & {
19
26
  LocalStorage: T;
@@ -80,6 +87,12 @@ class LocalStorageModule extends Storage {
80
87
  }
81
88
 
82
89
  async setKeychainItem(key, value, namespace = DEFAULT_NAMESPACE) {
90
+ if (!key) throwUndefinedKeyError(storage, UNDEFINED_KEY_MESSAGE);
91
+
92
+ if (typeof value === "undefined" || value === null) {
93
+ throwUndefinedValueError(storage, UNDEFINED_VALUE_MESSAGE, key);
94
+ }
95
+
83
96
  try {
84
97
  const result = await LocalStorage.setKeychainItem(
85
98
  key,
@@ -70,6 +70,11 @@ export class SecureStorageMock extends StorageMock {
70
70
  this.securedItems = new Map();
71
71
  }
72
72
 
73
+ __clear() {
74
+ super.__clear();
75
+ this.securedItems = new Map();
76
+ }
77
+
73
78
  async getKeychainItem(key, namespace = DEFAULT_NAMESPACE) {
74
79
  if (key.includes("fail")) throw new Error("failed to get secure item");
75
80
 
@@ -23,3 +23,13 @@ exports[`localStorage setItem throws if there is an error in the native module 1
23
23
  exports[`localStorage setItem throws if value is null 1`] = `"Value to set in storage cannot be null or undefined"`;
24
24
 
25
25
  exports[`localStorage setItem throws if value is undefined 1`] = `"Value to set in storage cannot be null or undefined"`;
26
+
27
+ exports[`localStorage setKeychainItem throws if key is null 1`] = `"Requested key is null or undefined"`;
28
+
29
+ exports[`localStorage setKeychainItem throws if key is undefined 1`] = `"Requested key is null or undefined"`;
30
+
31
+ exports[`localStorage setKeychainItem throws if there is an error in the native module 1`] = `"An error occured while setting an item in native secure storage: failed to set secure item"`;
32
+
33
+ exports[`localStorage setKeychainItem throws if value is null 1`] = `"Value to set in storage cannot be null or undefined"`;
34
+
35
+ exports[`localStorage setKeychainItem throws if value is undefined 1`] = `"Value to set in storage cannot be null or undefined"`;
@@ -262,6 +262,104 @@ describe("localStorage", () => {
262
262
  });
263
263
  });
264
264
 
265
+ describe("setKeychainItem", () => {
266
+ beforeEach(async () => {
267
+ NativeLocalStorage.__clear();
268
+ });
269
+
270
+ it("sets an item in secure storage", async () => {
271
+ const key = "test_key";
272
+ const value = "test_value";
273
+ const result = await localStorage.setKeychainItem(key, value);
274
+
275
+ expect(result).toBe(true);
276
+
277
+ expect(
278
+ NativeLocalStorage.securedItems.get(DEFAULT_NAMESPACE).get(key)
279
+ ).toEqual(value);
280
+ });
281
+
282
+ it("works as a bound method", async () => {
283
+ const key = "test_key";
284
+ const value = "test_value";
285
+ const setKeychainItem = localStorage.setKeychainItem;
286
+ const result = await setKeychainItem(key, value);
287
+
288
+ expect(result).toBe(true);
289
+
290
+ expect(
291
+ NativeLocalStorage.securedItems.get(DEFAULT_NAMESPACE).get(key)
292
+ ).toEqual(value);
293
+ });
294
+
295
+ it("stringifies the value if its not a string", async () => {
296
+ const key = "test_key";
297
+ const value = { objVal: "test_value" };
298
+
299
+ const result = await localStorage.setKeychainItem(key, value);
300
+
301
+ expect(result).toBe(true);
302
+
303
+ expect(
304
+ NativeLocalStorage.securedItems.get(DEFAULT_NAMESPACE).get(key)
305
+ ).toEqual(JSON.stringify(value));
306
+ });
307
+
308
+ it("sets a value in a specific namespace", async () => {
309
+ const key = "test_key";
310
+ const value = "test_value";
311
+ const namespace = "test_namespace";
312
+ const result = await localStorage.setKeychainItem(key, value, namespace);
313
+
314
+ expect(result).toBe(true);
315
+
316
+ expect(NativeLocalStorage.securedItems.get(namespace).get(key)).toEqual(
317
+ value
318
+ );
319
+ });
320
+
321
+ it("throws if key is null", async () => {
322
+ const value = "test_value";
323
+
324
+ expect(() =>
325
+ localStorage.setKeychainItem(null, value)
326
+ ).rejects.toThrowErrorMatchingSnapshot();
327
+ });
328
+
329
+ it("throws if key is undefined", async () => {
330
+ const value = "test_value";
331
+
332
+ expect(() =>
333
+ localStorage.setKeychainItem(undefined, value)
334
+ ).rejects.toThrowErrorMatchingSnapshot();
335
+ });
336
+
337
+ it("throws if value is null", async () => {
338
+ const key = "test_key";
339
+
340
+ expect(() =>
341
+ localStorage.setKeychainItem(key, null)
342
+ ).rejects.toThrowErrorMatchingSnapshot();
343
+ });
344
+
345
+ it("throws if value is undefined", async () => {
346
+ const key = "test_key";
347
+
348
+ expect(() =>
349
+ localStorage.setKeychainItem(key, undefined)
350
+ ).rejects.toThrowErrorMatchingSnapshot();
351
+ });
352
+
353
+ it("throws if there is an error in the native module", async () => {
354
+ const key = "fail";
355
+ const value = "value";
356
+
357
+ expect(() =>
358
+ localStorage.setKeychainItem(key, value)
359
+ ).rejects.toThrowErrorMatchingSnapshot();
360
+ });
361
+ });
362
+
265
363
  describe("getItem", () => {
266
364
  beforeAll(setUpStorage);
267
365
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@applicaster/zapp-react-native-bridge",
3
- "version": "16.0.0-rc.47",
3
+ "version": "16.0.0-rc.49",
4
4
  "description": "Applicaster Zapp React Native modules",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -32,7 +32,7 @@
32
32
  "homepage": "https://github.com/applicaster/quickbrick#readme",
33
33
  "dependencies": {
34
34
  "@applicaster/zapp-pipes-dev-kit": "1.4.7",
35
- "@applicaster/zapp-react-native-utils": "16.0.0-rc.47",
35
+ "@applicaster/zapp-react-native-utils": "16.0.0-rc.49",
36
36
  "md5": "^2.3.0",
37
37
  "uuid": "^3.3.2"
38
38
  },