@luvio/adapter-test-library 0.142.2 → 0.142.3

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.
Files changed (51) hide show
  1. package/package.json +13 -6
  2. package/babel.config.js +0 -1
  3. package/dist/umd/es2018/test-library.js +0 -600
  4. package/dist/umd/es2018/types/MockDurableStore.d.ts +0 -16
  5. package/dist/umd/es2018/types/adapterRoundtrip.d.ts +0 -35
  6. package/dist/umd/es2018/types/customMatchers.d.ts +0 -10
  7. package/dist/umd/es2018/types/durableStorePersistence.d.ts +0 -13
  8. package/dist/umd/es2018/types/jest.setup.d.ts +0 -1
  9. package/dist/umd/es2018/types/main.d.ts +0 -9
  10. package/dist/umd/es2018/types/mocks.d.ts +0 -17
  11. package/dist/umd/es2018/types/network.d.ts +0 -38
  12. package/dist/umd/es2018/types/utils.d.ts +0 -39
  13. package/dist/umd/es2018/types/verification.d.ts +0 -2
  14. package/dist/umd/es5/test-library.js +0 -818
  15. package/dist/umd/es5/types/MockDurableStore.d.ts +0 -16
  16. package/dist/umd/es5/types/adapterRoundtrip.d.ts +0 -35
  17. package/dist/umd/es5/types/customMatchers.d.ts +0 -10
  18. package/dist/umd/es5/types/durableStorePersistence.d.ts +0 -13
  19. package/dist/umd/es5/types/jest.setup.d.ts +0 -1
  20. package/dist/umd/es5/types/main.d.ts +0 -9
  21. package/dist/umd/es5/types/mocks.d.ts +0 -17
  22. package/dist/umd/es5/types/network.d.ts +0 -38
  23. package/dist/umd/es5/types/utils.d.ts +0 -39
  24. package/dist/umd/es5/types/verification.d.ts +0 -2
  25. package/jest.config.js +0 -16
  26. package/matcher-types.d.ts +0 -10
  27. package/project.json +0 -8
  28. package/rollup.config.js +0 -33
  29. package/src/MockDurableStore.ts +0 -145
  30. package/src/__tests__/MockDurableStore.spec.ts +0 -53
  31. package/src/adapterRoundtrip.ts +0 -101
  32. package/src/customMatchers.ts +0 -17
  33. package/src/durableStorePersistence.ts +0 -34
  34. package/src/jest.setup.ts +0 -4
  35. package/src/main.ts +0 -26
  36. package/src/mocks.ts +0 -31
  37. package/src/network.ts +0 -240
  38. package/src/utils.ts +0 -207
  39. package/src/verification.ts +0 -60
  40. package/tsconfig.json +0 -4
  41. /package/dist/{es/es2018/test-library.js → test-library.js} +0 -0
  42. /package/dist/{es/es2018/types → types}/MockDurableStore.d.ts +0 -0
  43. /package/dist/{es/es2018/types → types}/adapterRoundtrip.d.ts +0 -0
  44. /package/dist/{es/es2018/types → types}/customMatchers.d.ts +0 -0
  45. /package/dist/{es/es2018/types → types}/durableStorePersistence.d.ts +0 -0
  46. /package/dist/{es/es2018/types → types}/jest.setup.d.ts +0 -0
  47. /package/dist/{es/es2018/types → types}/main.d.ts +0 -0
  48. /package/dist/{es/es2018/types → types}/mocks.d.ts +0 -0
  49. /package/dist/{es/es2018/types → types}/network.d.ts +0 -0
  50. /package/dist/{es/es2018/types → types}/utils.d.ts +0 -0
  51. /package/dist/{es/es2018/types → types}/verification.d.ts +0 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@luvio/adapter-test-library",
3
- "version": "0.142.2",
3
+ "version": "0.142.3",
4
4
  "description": "Test library for luvio adapters",
5
5
  "repository": {
6
6
  "type": "git",
@@ -8,9 +8,16 @@
8
8
  "directory": "packages/@luvio/adapter-test-library"
9
9
  },
10
10
  "license": "MIT",
11
- "main": "dist/umd/es2018/test-library.js",
12
- "module": "dist/es/es2018/test-library.js",
13
- "types": "dist/es/es2018/types/main.d.ts",
11
+ "exports": {
12
+ "types": "./dist/types/main.d.ts",
13
+ "import": "./dist/test-library.js",
14
+ "default": "./dist/test-library.js"
15
+ },
16
+ "module": "dist/test-library.js",
17
+ "types": "dist/types/main.d.ts",
18
+ "files": [
19
+ "dist/"
20
+ ],
14
21
  "scripts": {
15
22
  "build": "rollup --bundleConfigAsCjs --config rollup.config.js",
16
23
  "clean": "rm -rf dist",
@@ -21,8 +28,8 @@
21
28
  "sinon": "^14.0.0"
22
29
  },
23
30
  "devDependencies": {
24
- "@luvio/engine": "^0.142.2",
25
- "@luvio/environments": "^0.142.2",
31
+ "@luvio/engine": "^0.142.3",
32
+ "@luvio/environments": "^0.142.3",
26
33
  "@types/sinon": "^7.5.2"
27
34
  },
28
35
  "volta": {
package/babel.config.js DELETED
@@ -1 +0,0 @@
1
- module.exports = require('../../../babel.config');
@@ -1,600 +0,0 @@
1
- (function (global, factory) {
2
- typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('sinon'), require('@luvio/engine')) :
3
- typeof define === 'function' && define.amd ? define(['exports', 'sinon', '@luvio/engine'], factory) :
4
- (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.luvioAdapterTestLibrary = {}, global.sinon, global.engine));
5
- })(this, (function (exports, sinon, engine) { 'use strict';
6
-
7
- function isImmutable(value) {
8
- return !doesThrow(() => {
9
- // if verifyImmutable does not throw then the object is immutable
10
- verifyImmutable(value, '$');
11
- });
12
- }
13
- function verifyImmutable(value, path) {
14
- if (typeof value !== 'object' || value === null) {
15
- return;
16
- }
17
- if (Array.isArray(value)) {
18
- if (doesThrow(() => {
19
- const len = value.length;
20
- value.push('__test');
21
- if (len === value.length) {
22
- throw new Error('IE11 does not throw when mutating a frozen object');
23
- }
24
- }) === false) {
25
- throw new Error(`Unexpected mutable property found at ${path}: Array is extensible!`);
26
- }
27
- value.forEach((item, index) => {
28
- verifyImmutable(item, `${path}.${index}`);
29
- });
30
- return;
31
- }
32
- if (doesThrow(() => {
33
- value['__test_____'] = true;
34
- if (value['__test_____'] !== true) {
35
- throw new Error('IE11 does not throw when mutating a frozen object');
36
- }
37
- }) === false) {
38
- throw new Error(`Unexpected mutable property found at ${path}: Object is extensible!`);
39
- }
40
- Object.keys(value).forEach((key) => {
41
- if (doesThrow(() => {
42
- const old = value[key];
43
- value[key] = '_______foo';
44
- if (value[key] === old) {
45
- throw new Error('IE11 does not throw when mutating a frozen object');
46
- }
47
- }) === false) {
48
- throw new Error(`Unexpected mutable property found at ${path}: "${path}.${key}" is mutable!`);
49
- }
50
- verifyImmutable(value[key], `${path}.${key}`);
51
- });
52
- }
53
-
54
- /**
55
- * Clone an object
56
- *
57
- * @param {object} data The object to clone
58
- * @returns {object} The cloned object
59
- */
60
- function clone(data) {
61
- if (data === undefined) {
62
- return undefined;
63
- }
64
- return JSON.parse(JSON.stringify(data));
65
- }
66
- /**
67
- * Strips the given properties from an object. Useful for comparing data payloads
68
- * where the given props may not be present in the data returned from an adapter.
69
- *
70
- * Does not mutate the passed in object.
71
- *
72
- * @param {object} obj The object from which to strip properties
73
- * @param {string[]} props The properties to be removed
74
- * @returns {object} The updated object with the given props stripped out
75
- */
76
- function stripProperties(obj, props) {
77
- // don't mutate the original object
78
- const cloned = clone(obj);
79
- stripPropertiesMutating(cloned, props);
80
- return cloned;
81
- }
82
- // private version of strip properties that mutates
83
- function stripPropertiesMutating(obj, props) {
84
- props.forEach((prop) => {
85
- delete obj[prop];
86
- });
87
- Object.keys(obj).forEach((key) => {
88
- const value = obj[key];
89
- if (typeof value === 'object' && value !== null) {
90
- stripPropertiesMutating(value, props);
91
- }
92
- });
93
- }
94
- function doesThrow(predicate) {
95
- try {
96
- predicate();
97
- }
98
- catch (e) {
99
- return true;
100
- }
101
- return false;
102
- }
103
- function flushPromises() {
104
- return new Promise((resolve) => setTimeout(resolve, 0));
105
- }
106
- // Copied from engine to avoid build time dependency
107
- // BEGIN OF COPY BLOCK
108
- var SnapshotState;
109
- (function (SnapshotState) {
110
- SnapshotState["Fulfilled"] = "Fulfilled";
111
- SnapshotState["Unfulfilled"] = "Unfulfilled";
112
- SnapshotState["Error"] = "Error";
113
- SnapshotState["Pending"] = "Pending";
114
- SnapshotState["Stale"] = "Stale";
115
- })(SnapshotState || (SnapshotState = {}));
116
- function isErrorSnapshot(snapshot) {
117
- return snapshot.state === SnapshotState.Error;
118
- }
119
- function isFulfilledSnapshot(snapshot) {
120
- return snapshot.state === SnapshotState.Fulfilled;
121
- }
122
- function isStaleSnapshot(snapshot) {
123
- return snapshot.state === SnapshotState.Stale;
124
- }
125
- // END OF COPY BLOCK
126
- const customMatchers = {
127
- toEqualFulfilledSnapshotWithData: (snapshot, expected, privateProperties) => {
128
- if (isFulfilledSnapshot(snapshot)) {
129
- const expectedWithoutPrivateProperties = stripProperties(expected, privateProperties || []);
130
- expect(snapshot.data).toEqual(expectedWithoutPrivateProperties);
131
- expect(isImmutable(snapshot.data)).toBe(true);
132
- return { pass: true, message: () => 'Snapshot is a FulfilledSnapshot' };
133
- }
134
- return {
135
- pass: false,
136
- message: () => 'Actual Snapshot is not a FulfilledSnapshot.',
137
- };
138
- },
139
- toEqualStaleSnapshotWithData: (snapshot, expected, privateProperties) => {
140
- if (isStaleSnapshot(snapshot)) {
141
- const expectedWithoutPrivateProperties = stripProperties(expected, privateProperties || []);
142
- expect(snapshot.data).toEqual(expectedWithoutPrivateProperties);
143
- expect(isImmutable(snapshot.data)).toBe(true);
144
- return { pass: true, message: () => 'Snapshot is a StaleSnapshot' };
145
- }
146
- return {
147
- pass: false,
148
- message: () => 'Actual Snapshot is not a StaleSnapshot.',
149
- };
150
- },
151
- toEqualErrorSnapshot: (actual, expectedStatus) => {
152
- if (isErrorSnapshot(actual)) {
153
- expect(isImmutable(actual.error)).toBe(true);
154
- expect(actual.data).toBeUndefined();
155
- if (expectedStatus !== undefined) {
156
- expect(actual.error.status).toBe(expectedStatus);
157
- }
158
- return { pass: true, message: () => 'Snapshot is an ErrorSnapshot' };
159
- }
160
- return {
161
- pass: false,
162
- message: () => 'Actual Snapshot is not a ErrorSnapshot.',
163
- };
164
- },
165
- toEqualOfflineErrorSnapshot: (actual) => {
166
- if (isErrorSnapshot(actual)) {
167
- expect(isImmutable(actual.error)).toBe(true);
168
- expect(actual.data).toBeUndefined();
169
- expect(actual.error).toBeInstanceOf(Error);
170
- expect(actual.error.errorType).toBe('networkAdapterError');
171
- return {
172
- pass: true,
173
- message: () => 'Snapshot is an ErrorSnapshot',
174
- };
175
- }
176
- return {
177
- pass: false,
178
- message: () => 'Actual Snapshot is not a ErrorSnapshot.',
179
- };
180
- },
181
- toHaveBeenHitTimes: (mockNetworkAdapter, expected) => {
182
- const callCount = getMockNetworkAdapterCallCount(mockNetworkAdapter);
183
- if (callCount !== expected) {
184
- return {
185
- pass: false,
186
- message: () => `Number of network calls made are different. Actual ${callCount}, Expected ${expected}.`,
187
- };
188
- }
189
- return { pass: true, message: () => `Network calls were made ${expected} times.` };
190
- },
191
- toHaveBeenHitOnce: (mockNetworkAdapter) => {
192
- const callCount = getMockNetworkAdapterCallCount(mockNetworkAdapter);
193
- if (callCount !== 1) {
194
- return {
195
- pass: false,
196
- message: () => `Number of network calls was not 1. Actual ${callCount}.`,
197
- };
198
- }
199
- return { pass: true, message: () => 'Number of network calls was 1' };
200
- },
201
- toEqualFetchResponse: (actual, expected) => {
202
- expect(actual).toEqual({ ...expected, errorType: 'fetchResponse' });
203
- expect(isImmutable(actual)).toBe(true);
204
- return { pass: true, message: () => 'Actual response equals expected response' };
205
- },
206
- };
207
-
208
- const networkConnectivityStateMap = new WeakMap();
209
- exports.ConnectivityState = void 0;
210
- (function (ConnectivityState) {
211
- ConnectivityState[ConnectivityState["Online"] = 0] = "Online";
212
- ConnectivityState[ConnectivityState["Offline"] = 1] = "Offline";
213
- })(exports.ConnectivityState || (exports.ConnectivityState = {}));
214
- const callCountMap = new WeakMap();
215
- const mockPayloadsMap = new WeakMap();
216
- function buildOfflineError() {
217
- return new Error('Failed to fetch, request timeout');
218
- }
219
- function buildMockSetupError() {
220
- return new Error('A mock network response was not setup for this request');
221
- }
222
- /**
223
- * This function sorts two payloads based on number of parameters. It sorts in
224
- * ascending order, meaning a MockPayload with fewer parameters in the networkArgs
225
- * will come before a MockPayload with more parameters in the networkArgs.
226
- *
227
- * Sorting really only matters when a two networkArg objects have all the same
228
- * fields except for uri/query/headers, so this sorting algorithm is simplistic.
229
- * If two networkArg objects have different baseUri or basePath properties, for
230
- * example, then the sinon matcher won't have a collision so sorting doesn't
231
- * matter in that situation.
232
- *
233
- * @param {MockPayload} a
234
- * @param {MockPayload} b
235
- * @returns {number}
236
- */
237
- function sortPayloads(a, b) {
238
- const { networkArgs: { urlParams: bUrlParams, queryParams: bQueryParams, headers: bHeaders }, } = b;
239
- const { networkArgs: { urlParams: aUrlParams, queryParams: aQueryParams, headers: aHeaders }, } = a;
240
- const aParams = Object.keys(aUrlParams !== null && aUrlParams !== void 0 ? aUrlParams : {}).length +
241
- Object.keys(aQueryParams !== null && aQueryParams !== void 0 ? aQueryParams : {}).length +
242
- Object.keys(aHeaders !== null && aHeaders !== void 0 ? aHeaders : {}).length;
243
- const bParams = Object.keys(bUrlParams !== null && bUrlParams !== void 0 ? bUrlParams : {}).length +
244
- Object.keys(bQueryParams !== null && bQueryParams !== void 0 ? bQueryParams : {}).length +
245
- Object.keys(bHeaders !== null && bHeaders !== void 0 ? bHeaders : {}).length;
246
- return aParams - bParams;
247
- }
248
- function isOkResponse(status) {
249
- return status >= 200 && status <= 299;
250
- }
251
- /**
252
- * Flushes any pending network requests. Useful for tests that need to ensure all
253
- * un-awaited background refreshes are complete
254
- *
255
- * @param _mockNetworkAdapter {NetworkAdapter} The network adapter instance to flush
256
- */
257
- async function flushPendingNetworkRequests(_mockNetworkAdapter) {
258
- // since the network mock is actually synchronous (just returns things wrapped
259
- // in Promise.resolve/reject) the only thing necessary to flush any pending
260
- // network activity is to flush the pending microtask queue
261
- await flushPromises();
262
- }
263
- function buildMockNetworkAdapter(mockPayloads) {
264
- // any endpoints not setup with a fake will return a rejected promise
265
- const networkAdapter = sinon.stub().rejects(buildMockSetupError());
266
- callCountMap.set(networkAdapter, 0);
267
- mockPayloadsMap.set(networkAdapter, mockPayloads);
268
- setMockNetworkPayloads(networkAdapter, mockPayloads);
269
- return networkAdapter;
270
- }
271
- function setNetworkConnectivity(mockNetworkAdapter, connectivityState) {
272
- const stub = mockNetworkAdapter;
273
- networkConnectivityStateMap.set(stub, connectivityState);
274
- }
275
- function onNetworkStubCalled(stub, response) {
276
- var _a;
277
- const { body, status, statusText, ok, headers } = response;
278
- const currentCallCount = callCountMap.get(stub);
279
- callCountMap.set(stub, currentCallCount ? currentCallCount + 1 : 1);
280
- const connectivityState = (_a = networkConnectivityStateMap.get(stub)) !== null && _a !== void 0 ? _a : exports.ConnectivityState.Online;
281
- if (connectivityState === exports.ConnectivityState.Offline) {
282
- return Promise.reject(buildOfflineError());
283
- }
284
- return Promise.resolve({
285
- status,
286
- statusText,
287
- ok,
288
- body: clone(body),
289
- headers: clone(headers),
290
- });
291
- }
292
- function setMockNetworkPayloads(mockNetworkAdapter, mockPayloads) {
293
- const stub = mockNetworkAdapter;
294
- // sort mock payloads so least-specific network args are registered first
295
- mockPayloads.sort(sortPayloads).forEach((mockPayload) => {
296
- const { networkArgs, response } = mockPayload;
297
- const args = sinon.match(networkArgs);
298
- stub.withArgs(args).callsFake(() => {
299
- return onNetworkStubCalled(stub, response);
300
- });
301
- });
302
- }
303
- /**
304
- * Overrides the mock network adapter responses with the given "responses". Each request to
305
- * the network adapter will "pop" a response from the response array. If more requests
306
- * to the network adapter are made than responses in the given array then an error
307
- * will be returned.
308
- *
309
- * This differs from setMockNetworkPayloads because it does not look at the incoming
310
- * network request args to determine what the response should be, it just returns
311
- * the response based on the request order.
312
- *
313
- * NOTE: this overrides any previously setup MockPayloads for the given network adapter
314
- * until resetMockNetworkAdapter is called.
315
- */
316
- function overrideMockNetworkResponses(mockNetworkAdapter, responses) {
317
- const stub = mockNetworkAdapter;
318
- let index = 0;
319
- stub.withArgs(sinon.match.any).callsFake(() => {
320
- const response = responses[index++];
321
- if (response === undefined) {
322
- // if they have more requests than expected
323
- return Promise.reject(buildMockSetupError());
324
- }
325
- return onNetworkStubCalled(stub, response);
326
- });
327
- }
328
- function resetMockNetworkAdapter(mockNetworkAdapter, newPayloads) {
329
- const stub = mockNetworkAdapter;
330
- // reset stub in case a test modified payload responses
331
- stub.reset();
332
- // reset call count
333
- callCountMap.set(stub, 0);
334
- // set mock payloads up - Allow for the test to provide a different set of payloads
335
- // Default behavior is to reset to the original payloads.
336
- const payloads = newPayloads ? newPayloads : mockPayloadsMap.get(stub);
337
- if (payloads) {
338
- setMockNetworkPayloads(stub, payloads);
339
- }
340
- }
341
- function getMockNetworkAdapterCallCount(mockNetworkAdapter) {
342
- const stub = mockNetworkAdapter;
343
- return callCountMap.get(stub);
344
- }
345
- function buildFetchResponse(body, status = 200, statusText = 'Ok', headers = {}) {
346
- return {
347
- headers,
348
- body,
349
- status,
350
- statusText,
351
- ok: isOkResponse(status),
352
- };
353
- }
354
- function buildSuccessMockPayload(networkArgs, body, headers = {}) {
355
- const mockPayload = {
356
- networkArgs,
357
- response: buildFetchResponse(body, 200, 'Ok', headers),
358
- };
359
- return mockPayload;
360
- }
361
- function buildErrorMockPayload(networkArgs, body, errorStatusCode, errorStatusText, headers = {}) {
362
- const mockPayload = {
363
- networkArgs,
364
- response: buildFetchResponse(body, errorStatusCode, errorStatusText, headers),
365
- };
366
- return mockPayload;
367
- }
368
-
369
- const mockFulfilledSnapshot = { state: 'Fulfilled' };
370
- const mockFulfilledSnapshotPromise = {
371
- then() {
372
- return mockFulfilledSnapshot;
373
- },
374
- };
375
- const mockLuvio = {
376
- storeLookup() {
377
- return mockFulfilledSnapshot;
378
- },
379
- snapshotAvailable() {
380
- return true;
381
- },
382
- dispatchResourceRequest() {
383
- return mockFulfilledSnapshotPromise;
384
- },
385
- applyCachePolicy() {
386
- return mockFulfilledSnapshot;
387
- },
388
- };
389
- function getMockLuvioWithFulfilledSnapshot() {
390
- return mockLuvio;
391
- }
392
- function getMockFulfilledSnapshot() {
393
- return mockFulfilledSnapshot;
394
- }
395
-
396
- function setupCustomLuvioMatchers() {
397
- expect.extend(customMatchers);
398
- }
399
-
400
- class MemoryDurableStorePersistence {
401
- constructor() {
402
- this.store = {};
403
- }
404
- async get(key) {
405
- return this.store[key];
406
- }
407
- async set(key, value) {
408
- // simulate a more realistic durable store by making the write wait a
409
- // tick before actually setting the value
410
- await flushPromises();
411
- this.store[key] = value;
412
- }
413
- async delete(key) {
414
- delete this.store[key];
415
- }
416
- async flushPendingWork() {
417
- // since this implementation does actual "IO" synchronously the only
418
- // thing necessary to await any pending IO is to flush the current
419
- // microtask queue
420
- await flushPromises();
421
- }
422
- }
423
-
424
- function waitForPromiseSet(set) {
425
- // NOTE: we are building an array from the Set at this point in time. If
426
- // more Promises are added to the Set while this is awaiting it won't
427
- // await the newly-added Promise. That's what we want.
428
- return Promise.all(Array.from(set)).then();
429
- }
430
- class MockDurableStore {
431
- constructor(persistence) {
432
- // for read/write synchronization
433
- this.writePromises = new Set();
434
- this.listeners = new Set();
435
- this.persistence = persistence || new MemoryDurableStorePersistence();
436
- }
437
- async getEntries(entryIds, segment) {
438
- // await any current write operations
439
- if (this.writePromises.size > 0) {
440
- await waitForPromiseSet(this.writePromises);
441
- }
442
- const entries = await this.persistence.get(segment);
443
- if (entries === undefined) {
444
- return undefined;
445
- }
446
- const returnSource = Object.create(null);
447
- for (const entryId of entryIds) {
448
- const entry = entries[entryId];
449
- if (entry !== undefined) {
450
- returnSource[entryId] = clone(entry);
451
- }
452
- }
453
- return returnSource;
454
- }
455
- async getAllEntries(segment) {
456
- // await any current write operations
457
- if (this.writePromises.size > 0) {
458
- await waitForPromiseSet(this.writePromises);
459
- }
460
- const entries = await this.persistence.get(segment);
461
- return entries;
462
- }
463
- setEntries(entries, segment) {
464
- return this.batchOperations([{ entries, segment, type: 'setEntries' }]);
465
- }
466
- evictEntries(ids, segment) {
467
- return this.batchOperations([{ ids, segment, type: 'evictEntries' }]);
468
- }
469
- registerOnChangedListener(listener) {
470
- this.listeners.add(listener);
471
- return () => {
472
- this.listeners.delete(listener);
473
- return Promise.resolve();
474
- };
475
- }
476
- async batchOperations(operations) {
477
- // await any current write operations
478
- if (this.writePromises.size > 0) {
479
- await waitForPromiseSet(this.writePromises);
480
- }
481
- const changes = [];
482
- const writePromise = (async () => {
483
- for (const operation of operations) {
484
- changes.push(await this.performOperation(operation));
485
- }
486
- })();
487
- this.writePromises.add(writePromise);
488
- try {
489
- await writePromise;
490
- }
491
- finally {
492
- this.writePromises.delete(writePromise);
493
- }
494
- const promises = [];
495
- this.listeners.forEach((listener) => {
496
- promises.push(listener(changes));
497
- });
498
- return Promise.all(promises).then(() => undefined);
499
- }
500
- async performOperation(operation) {
501
- const segment = operation.segment;
502
- const rawEntries = await this.persistence.get(segment);
503
- const entries = rawEntries === undefined ? {} : rawEntries;
504
- let ids = [];
505
- switch (operation.type) {
506
- case 'setEntries':
507
- ids = Object.keys(operation.entries);
508
- ids.forEach((id) => {
509
- entries[id] = clone(operation.entries[id]);
510
- });
511
- break;
512
- case 'evictEntries':
513
- ids = operation.ids;
514
- ids.forEach((id) => {
515
- delete entries[id];
516
- });
517
- }
518
- await this.persistence.set(operation.segment, entries);
519
- return { ids, segment, type: operation.type };
520
- }
521
- async flushPendingOperations() {
522
- // flush any pending read operations
523
- await this.persistence.flushPendingWork();
524
- // wait for any pending writes to finish
525
- while (this.writePromises.size > 0) {
526
- await waitForPromiseSet(this.writePromises);
527
- }
528
- }
529
- }
530
-
531
- // Use this util if you want to configure multiple network mocks in your test code.
532
- // If you're only mocking one, we suggest you use buildLuvioWithNetwork instead.
533
- function buildLuvioInstance(mockNetworkAdapter) {
534
- const store = new engine.InMemoryStore();
535
- const env = new engine.Environment(store, mockNetworkAdapter);
536
- return new engine.Luvio(env);
537
- }
538
- async function testAdapterCompletesRoundtrip(params) {
539
- const adapterSetup = setupAdapter(params);
540
- const result = await callAdapter(adapterSetup, params);
541
- expect(result.data).toBeDefined();
542
- expect(result).toEqualFulfilledSnapshotWithData(params.type === 'network'
543
- ? params.expectedData || params.network.response
544
- : params.expectedData, params.privateProperties);
545
- return result;
546
- }
547
- function setupAdapter(params) {
548
- const luvio = params.type === 'network' ? buildLuvioWithNetwork(params.network).luvio : params.luvio;
549
- return params.adapterFactory(luvio);
550
- }
551
- // Returns a tuple of the Luvio instance and network adapter so the test author may expect a number expected calls to the network.
552
- function buildLuvioWithNetwork(networkConfig) {
553
- // TODO: handle alternative expected network calls, errors and etc
554
- const expectedNetwork = [
555
- buildSuccessMockPayload(networkConfig.request, networkConfig.response),
556
- ];
557
- const mockNetworkAdapter = buildMockNetworkAdapter(expectedNetwork);
558
- return { luvio: buildLuvioInstance(mockNetworkAdapter), networkAdapter: mockNetworkAdapter };
559
- }
560
- async function callAdapter(adapter, params) {
561
- const snapshotOrPromise = adapter(params.adapterConfig);
562
- if (snapshotOrPromise === null) {
563
- throw new Error('Result of calling the adapter was null. Is the adapter config valid?');
564
- }
565
- // Cache Miss - Check that the network call was right.
566
- if (params.type === 'injectedLuvioNetwork' || params.type === 'network') {
567
- expect(snapshotOrPromise).toBeInstanceOf(Promise);
568
- return snapshotOrPromise;
569
- }
570
- else {
571
- // Cache hit
572
- expect(snapshotOrPromise).not.toBeInstanceOf(Promise); // Expect cache hit to be synchronous
573
- return Promise.resolve(snapshotOrPromise);
574
- }
575
- }
576
-
577
- exports.MemoryDurableStorePersistence = MemoryDurableStorePersistence;
578
- exports.MockDurableStore = MockDurableStore;
579
- exports.buildErrorMockPayload = buildErrorMockPayload;
580
- exports.buildFetchResponse = buildFetchResponse;
581
- exports.buildLuvioInstance = buildLuvioInstance;
582
- exports.buildLuvioWithNetwork = buildLuvioWithNetwork;
583
- exports.buildMockNetworkAdapter = buildMockNetworkAdapter;
584
- exports.buildSuccessMockPayload = buildSuccessMockPayload;
585
- exports.customMatchers = customMatchers;
586
- exports.flushPendingNetworkRequests = flushPendingNetworkRequests;
587
- exports.getMockFulfilledSnapshot = getMockFulfilledSnapshot;
588
- exports.getMockLuvioWithFulfilledSnapshot = getMockLuvioWithFulfilledSnapshot;
589
- exports.getMockNetworkAdapterCallCount = getMockNetworkAdapterCallCount;
590
- exports.isImmutable = isImmutable;
591
- exports.overrideMockNetworkResponses = overrideMockNetworkResponses;
592
- exports.resetMockNetworkAdapter = resetMockNetworkAdapter;
593
- exports.setMockNetworkPayloads = setMockNetworkPayloads;
594
- exports.setNetworkConnectivity = setNetworkConnectivity;
595
- exports.setupCustomLuvioMatchers = setupCustomLuvioMatchers;
596
- exports.stripProperties = stripProperties;
597
- exports.testAdapterCompletesRoundtrip = testAdapterCompletesRoundtrip;
598
- exports.verifyImmutable = verifyImmutable;
599
-
600
- }));
@@ -1,16 +0,0 @@
1
- import type { DurableStore, DurableStoreEntries, OnDurableStoreChangedListener, DurableStoreOperation } from '@luvio/environments';
2
- import type { DurableStorePersistence } from './durableStorePersistence';
3
- export declare class MockDurableStore implements DurableStore {
4
- private writePromises;
5
- listeners: Set<OnDurableStoreChangedListener>;
6
- persistence: DurableStorePersistence;
7
- constructor(persistence?: DurableStorePersistence);
8
- getEntries<T>(entryIds: string[], segment: string): Promise<DurableStoreEntries<T> | undefined>;
9
- getAllEntries<T>(segment: string): Promise<DurableStoreEntries<T> | undefined>;
10
- setEntries<T>(entries: DurableStoreEntries<T>, segment: string): Promise<void>;
11
- evictEntries(ids: string[], segment: string): Promise<void>;
12
- registerOnChangedListener(listener: OnDurableStoreChangedListener): () => Promise<void>;
13
- batchOperations<T>(operations: DurableStoreOperation<T>[]): Promise<void>;
14
- private performOperation;
15
- flushPendingOperations(): Promise<void>;
16
- }