@adobe/acc-js-sdk 1.1.19 → 1.1.21

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/test/util.test.js CHANGED
@@ -116,18 +116,18 @@ describe('Util', function() {
116
116
  })
117
117
 
118
118
  describe("Safe storage", () => {
119
- it("Should support undefined delegate", () => {
119
+ it("Should support undefined delegate", async () => {
120
120
  const storage = new SafeStorage();
121
- expect(storage.getItem("Hello")).toBeUndefined();
121
+ await expect(storage.getItem("Hello")).resolves.toBeUndefined();
122
122
  storage.setItem("Hello", { text: "World" });
123
- expect(storage.getItem("Hello")).toBeUndefined();
123
+ await expect(storage.getItem("Hello")).resolves.toBeUndefined();
124
124
  storage.setItem("Hello", "World"); // value should be JSON but errors are ignored
125
- expect(storage.getItem("Hello")).toBeUndefined();
125
+ await expect(storage.getItem("Hello")).resolves.toBeUndefined();
126
126
  storage.removeItem("Hello");
127
- expect(storage.getItem("Hello")).toBeUndefined();
127
+ await expect(storage.getItem("Hello")).resolves.toBeUndefined();
128
128
  })
129
129
 
130
- it("Should handle map", () => {
130
+ it("Should handle map", async () => {
131
131
  const map = {};
132
132
  const delegate = {
133
133
  getItem: (key) => map[key],
@@ -135,18 +135,18 @@ describe('Util', function() {
135
135
  removeItem: (key) => { delete map[key] }
136
136
  };
137
137
  const storage = new SafeStorage(delegate);
138
- expect(storage.getItem("Hello")).toBeUndefined();
139
- storage.setItem("Hello", { text: "World" });
138
+ await expect(storage.getItem("Hello")).resolves.toBeUndefined();
139
+ await storage.setItem("Hello", { text: "World" });
140
140
  expect(map["Hello"]).toStrictEqual(JSON.stringify({text: "World"}));
141
- expect(storage.getItem("Hello")).toStrictEqual({"text": "World"});
142
- storage.setItem("Hello", "World"); // value should be JSON but errors are ignored
143
- expect(storage.getItem("Hello")).toBeUndefined();
144
- storage.setItem("Hello", { text: "World" });
145
- storage.removeItem("Hello");
146
- expect(storage.getItem("Hello")).toBeUndefined();
141
+ await expect(storage.getItem("Hello")).resolves.toStrictEqual({"text": "World"});
142
+ await storage.setItem("Hello", "World"); // value should be JSON but errors are ignored
143
+ await expect(storage.getItem("Hello")).resolves.toBeUndefined();
144
+ await storage.setItem("Hello", { text: "World" });
145
+ await storage.removeItem("Hello");
146
+ await expect(storage.getItem("Hello")).resolves.toBeUndefined();
147
147
  })
148
148
 
149
- it("Should handle root key", () => {
149
+ it("Should handle root key", async () => {
150
150
  const map = {};
151
151
  const delegate = {
152
152
  getItem: (key) => map[key],
@@ -154,17 +154,17 @@ describe('Util', function() {
154
154
  removeItem: (key) => { delete map[key] }
155
155
  };
156
156
  const storage = new SafeStorage(delegate, "root");
157
- expect(storage.getItem("Hello")).toBeUndefined();
158
- storage.setItem("Hello", { text: "World" });
157
+ await expect(storage.getItem("Hello")).resolves.toBeUndefined();
158
+ await storage.setItem("Hello", { text: "World" });
159
159
  expect(map["root$Hello"]).toStrictEqual(JSON.stringify({text: "World"}));
160
- expect(storage.getItem("Hello")).toStrictEqual({"text": "World"});
161
- storage.setItem("Hello", "World"); // value should be JSON but errors are ignored
160
+ await expect(storage.getItem("Hello")).resolves.toStrictEqual({"text": "World"});
161
+ await storage.setItem("Hello", "World"); // value should be JSON but errors are ignored
162
162
  expect(map["root$Hello"]).toBeUndefined();
163
- expect(storage.getItem("Hello")).toBeUndefined();
164
- storage.setItem("Hello", { text: "World" });
165
- storage.removeItem("Hello");
163
+ await expect(storage.getItem("Hello")).resolves.toBeUndefined();
164
+ await storage.setItem("Hello", { text: "World" });
165
+ await storage.removeItem("Hello");
166
166
  expect(map["root$Hello"]).toBeUndefined();
167
- expect(storage.getItem("Hello")).toBeUndefined();
167
+ await expect(storage.getItem("Hello")).resolves.toBeUndefined();
168
168
  })
169
169
 
170
170
  it("Edge cases", () => {
@@ -176,7 +176,7 @@ describe('Util', function() {
176
176
  expect(new SafeStorage(null, "")._rootKey).toBe("");
177
177
  })
178
178
 
179
- it("Should remove invalid items on get", () => {
179
+ it("Should remove invalid items on get", async () => {
180
180
  const map = {};
181
181
  const delegate = {
182
182
  getItem: (key) => map[key],
@@ -186,12 +186,12 @@ describe('Util', function() {
186
186
  const storage = new SafeStorage(delegate, "root");
187
187
  // value is not valid because not a JSON serialized string
188
188
  map["root$Hello"] = "Invalid";
189
- expect(storage.getItem("Hello")).toBeUndefined();
189
+ await expect(storage.getItem("Hello")).resolves.toBeUndefined();
190
190
  // Get should have removed invalid value
191
191
  expect(map["root$Hello"]).toBeUndefined()
192
192
  })
193
193
 
194
- it("Should handle cache last cleared", () => {
194
+ it("Should handle cache last cleared", async () => {
195
195
  const map = {};
196
196
  const delegate = {
197
197
  getItem: (key) => map[key],
@@ -199,18 +199,18 @@ describe('Util', function() {
199
199
  removeItem: (key) => { delete map[key] }
200
200
  };
201
201
  const cache = new Cache(delegate, "root");
202
- cache.put("Hello", "World");
202
+ await cache.put("Hello", "World");
203
203
  expect(JSON.parse(map["root$Hello"])).toMatchObject({ value:"World" });
204
- expect(cache.get("Hello")).toBe("World");
205
- cache.clear();
204
+ await expect(cache.get("Hello")).resolves.toBe("World");
205
+ await cache.clear();
206
206
  // Clear could not remove the item from the map
207
207
  expect(JSON.parse(map["root$Hello"])).toMatchObject({ value:"World" });
208
208
  // But get from cache will
209
- expect(cache.get("Hello")).toBeUndefined();
209
+ await expect(cache.get("Hello")).resolves.toBeUndefined();
210
210
  })
211
211
  })
212
212
 
213
- it("Should preserve last cleared", () => {
213
+ it("Should preserve last cleared", async () => {
214
214
  const map = {};
215
215
  const delegate = {
216
216
  getItem: (key) => map[key],
@@ -218,19 +218,22 @@ describe('Util', function() {
218
218
  removeItem: (key) => { delete map[key] }
219
219
  };
220
220
  const cache = new Cache(delegate, "root");
221
- expect(cache._lastCleared).toBeUndefined();
222
- cache.put("Hello", "World");
223
- cache.clear();
221
+ expect(cache._lastCleared).toBeNull(); // means we do not have the last cleared value yet
222
+ await cache.get("dummy"); // trigger getItem to lastCleared, and then dummy
223
+ await cache.put("Hello", "World");
224
+ await cache.clear();
224
225
  const lastCleared = cache._lastCleared;
225
226
  expect(lastCleared).not.toBeUndefined();
226
- expect(cache.get("Hello")).toBeUndefined();
227
+ await expect(cache.get("Hello")).resolves.toBeUndefined();
227
228
  expect(map["root$lastCleared"]).toBe(JSON.stringify({timestamp:lastCleared}));
228
229
  // New cache with same delegate storage should preserve lastCleared date
229
230
  const cache2 = new Cache(delegate, "root");
231
+ expect(cache2._lastCleared).toBeNull(); // means we do not have the last cleared value yet
232
+ await cache2.get("dummy"); // trigger getItem to lastCleared, and then dummy
230
233
  expect(cache2._lastCleared).toBe(lastCleared);
231
234
  })
232
235
 
233
- it("Should cache in memory value which is in local storage", () => {
236
+ it("Should cache in memory value which is in local storage", async () => {
234
237
  const map = {};
235
238
  const delegate = {
236
239
  getItem: (key) => map[key],
@@ -239,7 +242,7 @@ describe('Util', function() {
239
242
  };
240
243
  const cache = new Cache(delegate, "root");
241
244
  map["root$Hello"] = JSON.stringify({ value: "World", cachedAt:Date.now() + 99999999 });
242
- const value = cache.get("Hello");
245
+ const value = await cache.get("Hello");
243
246
  expect(value).toBe("World");
244
247
  expect(cache._cache["Hello"].value).toBe("World");
245
248
  })
@@ -409,5 +412,23 @@ describe('Util', function() {
409
412
  expect(Util.isBrowser()).toBe(false);
410
413
  });
411
414
  });
415
+
416
+ describe("Promises", () => {
417
+ it("Should tell if an object is a promise", () => {
418
+ expect(Util.isPromise()).toBe(false);
419
+ expect(Util.isPromise(null)).toBe(false);
420
+ expect(Util.isPromise({})).toBe(false);
421
+ expect(Util.isPromise([])).toBe(false);
422
+ expect(Util.isPromise({ then: 3 })).toBe(false);
423
+ expect(Util.isPromise(new Promise((resolve, reject) => {}))).toBe(true);
424
+ expect(Util.isPromise(Promise.resolve(3))).toBe(true);
425
+ });
426
+
427
+ it("Should ensure a promise", async () => {
428
+ await expect(Util.asPromise(null)).resolves.toBe(null);
429
+ await expect(Util.asPromise(3)).resolves.toBe(3);
430
+ await expect(Util.asPromise(Promise.resolve(3))).resolves.toBe(3);
431
+ });
432
+ });
412
433
  });
413
434