@amedia/user 0.14.18 → 0.14.20

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 (85) hide show
  1. package/package.json +2 -3
  2. package/src/actions/authentication.spec.ts +0 -51
  3. package/src/actions/authentication.ts +0 -49
  4. package/src/actions/authorization.spec.ts +0 -377
  5. package/src/actions/authorization.ts +0 -120
  6. package/src/actions/preferences.spec.ts +0 -41
  7. package/src/actions/preferences.ts +0 -41
  8. package/src/actions/storage.ts +0 -22
  9. package/src/actions/userData.spec.ts +0 -133
  10. package/src/actions/userData.ts +0 -68
  11. package/src/autologin/index.spec.ts +0 -264
  12. package/src/autologin/index.ts +0 -113
  13. package/src/autologin/thirdPartyCookie.spec.ts +0 -95
  14. package/src/autologin/thirdPartyCookie.ts +0 -55
  15. package/src/cache/engines/global.spec.ts +0 -62
  16. package/src/cache/engines/global.ts +0 -49
  17. package/src/cache/index.spec.ts +0 -132
  18. package/src/cache/index.ts +0 -48
  19. package/src/cache/types.ts +0 -12
  20. package/src/clients/europa.spec.ts +0 -209
  21. package/src/clients/europa.ts +0 -183
  22. package/src/clients/jupiter.spec.ts +0 -178
  23. package/src/clients/jupiter.ts +0 -119
  24. package/src/clients/mercury.spec.ts +0 -126
  25. package/src/clients/mercury.ts +0 -105
  26. package/src/clients/mercuryLog.spec.ts +0 -20
  27. package/src/clients/mercuryLog.ts +0 -67
  28. package/src/clients/nebula.spec.ts +0 -70
  29. package/src/clients/nebula.ts +0 -83
  30. package/src/clients/pluto.spec.ts +0 -262
  31. package/src/clients/pluto.ts +0 -126
  32. package/src/eventHandlers/activation.ts +0 -40
  33. package/src/eventHandlers/authentication.ts +0 -34
  34. package/src/eventHandlers/eventMap.ts +0 -40
  35. package/src/eventHandlers/navigation.spec.ts +0 -189
  36. package/src/eventHandlers/navigation.ts +0 -58
  37. package/src/eventHandlers/state.spec.ts +0 -48
  38. package/src/eventHandlers/state.ts +0 -23
  39. package/src/eventHandlers/storage.spec.ts +0 -114
  40. package/src/eventHandlers/storage.ts +0 -45
  41. package/src/eventHandlers/userAttributes.spec.ts +0 -93
  42. package/src/eventHandlers/userAttributes.ts +0 -31
  43. package/src/exceptions/FetchTimeoutError.ts +0 -8
  44. package/src/exceptions/PaywallError.ts +0 -16
  45. package/src/index-authentcation.spec.ts +0 -26
  46. package/src/index-storage.spec.ts +0 -144
  47. package/src/index-subscription.spec.ts +0 -213
  48. package/src/index.ts +0 -46
  49. package/src/janitor/index.spec.ts +0 -18
  50. package/src/janitor/index.ts +0 -60
  51. package/src/janitor/jobs/accessCookieRefresher.spec.ts +0 -213
  52. package/src/janitor/jobs/accessCookieRefresher.ts +0 -96
  53. package/src/janitor/jobs/daxsubCookie.spec.ts +0 -109
  54. package/src/janitor/jobs/daxsubCookie.ts +0 -38
  55. package/src/janitor/jobs/removeUserStorage.spec.ts +0 -34
  56. package/src/janitor/jobs/removeUserStorage.ts +0 -12
  57. package/src/lib/PaywallUnlockRequest.spec.ts +0 -446
  58. package/src/lib/PaywallUnlockRequest.ts +0 -161
  59. package/src/lib/UserDataRequest.spec.ts +0 -231
  60. package/src/lib/UserDataRequest.ts +0 -267
  61. package/src/lib/cookie.ts +0 -121
  62. package/src/lib/rpc.ts +0 -19
  63. package/src/store/index.spec.ts +0 -49
  64. package/src/store/index.ts +0 -66
  65. package/src/types.ts +0 -22
  66. package/src/utils/customEventWrapper.spec.ts +0 -237
  67. package/src/utils/customEventWrapper.ts +0 -77
  68. package/src/utils/debouncer.spec.ts +0 -252
  69. package/src/utils/debouncer.ts +0 -123
  70. package/src/utils/deepEquals.ts +0 -42
  71. package/src/utils/expectBootLogs.ts +0 -9
  72. package/src/utils/getGlobalThis.ts +0 -17
  73. package/src/utils/getOrUpdate.spec.ts +0 -166
  74. package/src/utils/getOrUpdate.ts +0 -66
  75. package/src/utils/localhostCheck.ts +0 -10
  76. package/src/utils/location.spec.ts +0 -61
  77. package/src/utils/location.ts +0 -16
  78. package/src/utils/log.spec.ts +0 -177
  79. package/src/utils/log.ts +0 -112
  80. package/src/utils/loginState.ts +0 -24
  81. package/src/utils/networkError.ts +0 -11
  82. package/src/utils/queue.ts +0 -32
  83. package/src/utils/type.ts +0 -19
  84. package/src/utils/url.spec.ts +0 -33
  85. package/src/utils/url.ts +0 -37
@@ -1,66 +0,0 @@
1
- import deepmerge from 'ts-deepmerge';
2
- import { State, UserAttributes } from '../types';
3
-
4
- type StoreDataMap = {
5
- userAttributes: UserAttributes;
6
- state: State;
7
- storage: { [namespace: string]: { [index: string]: unknown } };
8
- isPoisoned: boolean;
9
- };
10
-
11
- type Listener<T> = (data: T) => void;
12
- type Listeners<T = StoreDataMap> = {
13
- [key in keyof T]?: Array<Listener<T[key]>>;
14
- };
15
-
16
- const store: Partial<StoreDataMap> = {};
17
- const listeners: Listeners = {};
18
-
19
- const emit = (key: keyof StoreDataMap): void => {
20
- // @ts-ignore
21
- listeners[key]?.forEach((listener) => listener(store[key]));
22
- };
23
-
24
- const on = <K extends keyof StoreDataMap, T = StoreDataMap[K]>(
25
- key: K,
26
- listener: Listener<T>
27
- ) => {
28
- if (!listeners[key]) {
29
- listeners[key] = [];
30
- }
31
-
32
- // @ts-ignore todo How can this possibly be undefined given the statement above?
33
- listeners[key].push(listener);
34
- };
35
-
36
- const set = (key: keyof StoreDataMap, value: StoreDataMap[typeof key]) => {
37
- // @ts-ignore todo Not sure why this type fails.
38
- store[key] = value;
39
- emit(key);
40
- };
41
-
42
- const merge = <K extends keyof StoreDataMap>(
43
- key: K,
44
- value: Partial<StoreDataMap[K]>
45
- ) => {
46
- // @ts-ignore
47
- set(key, deepmerge(store[key] || {}, value));
48
- };
49
-
50
- const get = <K extends keyof StoreDataMap, T = StoreDataMap[K]>(
51
- key: K
52
- ): T | undefined =>
53
- // @ts-ignore Not sure why this fails
54
- store[key];
55
- const clear = () => {
56
- store.state = undefined;
57
- store.userAttributes = undefined;
58
- store.storage = undefined;
59
- store.isPoisoned = undefined;
60
- Object.keys(listeners).forEach((key) => {
61
- // @ts-ignore
62
- listeners[key] = [];
63
- });
64
- };
65
-
66
- export default { merge, set, get, on, clear };
package/src/types.ts DELETED
@@ -1,22 +0,0 @@
1
- export type UserAttributes = {
2
- uuid?: string | null;
3
- name?: string | null;
4
- email?: string | null;
5
- phone?: string | null;
6
- trackingKey?: string | null;
7
- extraData?: { [index: string]: unknown };
8
- access?: string[];
9
- privacyPreferences?: PrivacyPreferences | null;
10
- };
11
-
12
- export type State = {
13
- isLoggedIn: boolean;
14
- emergencyMode?: string[];
15
- };
16
-
17
- export type PrivacyPreferences = {
18
- allowResearchUsage: boolean;
19
- connectWithSurveys: boolean;
20
- personalizedAds: boolean;
21
- personalizedContent: boolean;
22
- };
@@ -1,237 +0,0 @@
1
- import store from '../store';
2
- import {
3
- dispatch,
4
- listenForAllEvents,
5
- listenWithExclusivity,
6
- setRootElement,
7
- } from './customEventWrapper';
8
- import log from './log';
9
- import { AmediaUserEvents } from '../eventHandlers/eventMap';
10
-
11
- const itShouldNotLogError = () => {
12
- it('should not log error', () => {
13
- expect(log.error).not.toHaveBeenCalled();
14
- });
15
- };
16
-
17
- let rootElement: Element;
18
- beforeEach(() => {
19
- spyOn(log, 'error').and.stub();
20
-
21
- store.clear();
22
-
23
- /**
24
- * This is already set in test bootstrapping, but we need access to the
25
- * rootElement for some tests. Cleanest to override root element a second time
26
- */
27
- rootElement = new Document().createElement('div');
28
- spyOn(rootElement, 'dispatchEvent').and.callThrough();
29
- setRootElement(rootElement);
30
- });
31
-
32
- describe('when calling listenWithExclusivity', () => {
33
- describe('and we are missing root element', () => {
34
- beforeEach(() => {
35
- // @ts-ignore
36
- setRootElement(null);
37
- listenWithExclusivity(AmediaUserEvents.REQUEST_NAVIGATION, () => {});
38
- });
39
-
40
- it('should log error', () => {
41
- expect(log.error).toHaveBeenCalledWith(
42
- 'No root element to bind event to!'
43
- );
44
- });
45
- });
46
-
47
- describe('and we are poisoned', () => {
48
- let listener: jest.Mock;
49
- beforeEach(() => {
50
- listener = jest.fn();
51
- listenWithExclusivity(AmediaUserEvents.REQUEST_NAVIGATION, listener);
52
- store.set('isPoisoned', true);
53
- rootElement.dispatchEvent(
54
- new CustomEvent(AmediaUserEvents.REQUEST_NAVIGATION, {
55
- detail: { type: 'reload' },
56
- })
57
- );
58
- });
59
-
60
- it('should not call listener', () => {
61
- expect(listener).not.toHaveBeenCalled();
62
- });
63
-
64
- itShouldNotLogError();
65
- });
66
-
67
- describe('and a single listener have been added', () => {
68
- let listener: jest.Mock;
69
- let event: CustomEvent;
70
- beforeEach(() => {
71
- listener = jest.fn();
72
- event = new CustomEvent(AmediaUserEvents.REQUEST_NAVIGATION);
73
- listenWithExclusivity(AmediaUserEvents.REQUEST_NAVIGATION, listener);
74
- dispatch(event);
75
- });
76
-
77
- it('should call the first listener', () => {
78
- expect(listener).toHaveBeenCalledWith(event);
79
- });
80
-
81
- itShouldNotLogError();
82
- });
83
-
84
- describe('and multiple listeners have been added', () => {
85
- let first: jest.Mock;
86
- let second: jest.Mock;
87
- let event: CustomEvent;
88
- beforeEach(() => {
89
- first = jest.fn();
90
- second = jest.fn();
91
- event = new CustomEvent(AmediaUserEvents.REQUEST_NAVIGATION);
92
- listenWithExclusivity(AmediaUserEvents.REQUEST_NAVIGATION, first);
93
- listenWithExclusivity(AmediaUserEvents.REQUEST_NAVIGATION, second);
94
- rootElement.dispatchEvent(event);
95
- });
96
-
97
- it('should call the first listener', () => {
98
- expect(first).toHaveBeenCalledWith(event);
99
- });
100
-
101
- it('should not call the second listener', () => {
102
- expect(second).not.toHaveBeenCalled();
103
- });
104
-
105
- itShouldNotLogError();
106
- });
107
- });
108
-
109
- describe('when calling listenForAllEvents', () => {
110
- describe('and we are missing root element', () => {
111
- beforeEach(() => {
112
- // @ts-ignore
113
- setRootElement(null);
114
- listenForAllEvents(AmediaUserEvents.REQUEST_NAVIGATION, () => {});
115
- });
116
-
117
- it('should log error', () => {
118
- expect(log.error).toHaveBeenCalledWith(
119
- 'No root element to bind event to!'
120
- );
121
- });
122
- });
123
-
124
- describe('and we are poisoned', () => {
125
- let listener: jest.Mock;
126
- let event: CustomEvent;
127
- beforeEach(() => {
128
- listener = jest.fn();
129
- event = new CustomEvent(AmediaUserEvents.REQUEST_NAVIGATION, {
130
- detail: { type: 'reload' },
131
- });
132
- listenForAllEvents(AmediaUserEvents.REQUEST_NAVIGATION, listener);
133
- store.set('isPoisoned', true);
134
- rootElement.dispatchEvent(event);
135
- });
136
-
137
- it('should ignore any poison and call listener', () => {
138
- /**
139
- * The reason we ignore poison here is that we almost certanly
140
- * won't have the store to identify it anyway
141
- */
142
- expect(listener).toHaveBeenCalledWith(event);
143
- });
144
-
145
- itShouldNotLogError();
146
- });
147
-
148
- describe('and a single listener have been added', () => {
149
- let listener: jest.Mock;
150
- let event: CustomEvent;
151
- beforeEach(() => {
152
- listener = jest.fn();
153
- event = new CustomEvent(AmediaUserEvents.REQUEST_NAVIGATION);
154
- listenForAllEvents(AmediaUserEvents.REQUEST_NAVIGATION, listener);
155
- dispatch(event);
156
- });
157
-
158
- it('should call the first listener', () => {
159
- expect(listener).toHaveBeenCalledWith(event);
160
- });
161
-
162
- itShouldNotLogError();
163
- });
164
-
165
- describe('and multiple listeners have been added', () => {
166
- let first: jest.Mock;
167
- let second: jest.Mock;
168
- let event: CustomEvent;
169
- beforeEach(() => {
170
- first = jest.fn();
171
- second = jest.fn();
172
- event = new CustomEvent(AmediaUserEvents.REQUEST_NAVIGATION);
173
- listenForAllEvents(AmediaUserEvents.REQUEST_NAVIGATION, first);
174
- listenForAllEvents(AmediaUserEvents.REQUEST_NAVIGATION, second);
175
- rootElement.dispatchEvent(event);
176
- });
177
-
178
- it('should call the first listener', () => {
179
- expect(first).toHaveBeenCalledWith(event);
180
- });
181
-
182
- it('should call the second listener', () => {
183
- expect(second).toHaveBeenCalledWith(event);
184
- });
185
-
186
- itShouldNotLogError();
187
- });
188
- });
189
-
190
- describe('when calling dispatch', () => {
191
- describe('and we are missing root element', () => {
192
- beforeEach(() => {
193
- // @ts-ignore
194
- setRootElement(null);
195
- dispatch(
196
- new CustomEvent(AmediaUserEvents.REQUEST_NAVIGATION, {
197
- detail: { reload: true },
198
- })
199
- );
200
- });
201
-
202
- it('should log error', () => {
203
- expect(log.error).toHaveBeenCalledWith(
204
- 'No root element to dispatch event on!'
205
- );
206
- });
207
- });
208
-
209
- describe('and we are poisoned', () => {
210
- beforeEach(() => {
211
- store.set('isPoisoned', true);
212
- dispatch(
213
- new CustomEvent(AmediaUserEvents.REQUEST_NAVIGATION, {
214
- detail: { reload: true },
215
- })
216
- );
217
- });
218
-
219
- it('should not dispatch event', () => {
220
- expect(rootElement.dispatchEvent).not.toHaveBeenCalled();
221
- });
222
- });
223
-
224
- describe('and all requirements are met', () => {
225
- let event: CustomEvent;
226
- beforeEach(() => {
227
- event = new CustomEvent(AmediaUserEvents.REQUEST_NAVIGATION, {
228
- detail: { reload: true },
229
- });
230
- dispatch(event);
231
- });
232
-
233
- it('should dispatch event', () => {
234
- expect(rootElement.dispatchEvent).toHaveBeenCalledWith(event);
235
- });
236
- });
237
- });
@@ -1,77 +0,0 @@
1
- import log from './log';
2
- import getGlobalThis from './getGlobalThis';
3
- import store from '../store';
4
-
5
- let rootElement: Window | Element | typeof globalThis;
6
-
7
- export const setRootElement = (element: typeof rootElement) => {
8
- rootElement = element;
9
- };
10
-
11
- setRootElement(getGlobalThis());
12
-
13
- /**
14
- * This listener MUST be used by all listeners in ../eventHandlers
15
- * We want to ensure that only _one_ instance of this package actually
16
- * orchestrates data and performs requests against backends.
17
- * @param type
18
- * @param listener
19
- */
20
- export const listenWithExclusivity = <T extends keyof WindowEventMap>(
21
- type: T,
22
- listener: (this: typeof rootElement, ev: WindowEventMap[T]) => void
23
- ) => {
24
- if (!rootElement) {
25
- log.error('No root element to bind event to!');
26
- return;
27
- }
28
- rootElement.addEventListener(type, (evt) => {
29
- // Since this lib might me loaded multiple times, we need to prevent all other processing of this event.
30
- evt.stopImmediatePropagation();
31
-
32
- /**
33
- * This will prevent any listeners from being called if we're poisoned,
34
- * thus preventing any external calls from being started when we're
35
- * being navigated away
36
- */
37
- if (store.get('isPoisoned')) {
38
- return;
39
- }
40
- listener.apply(rootElement, [evt as WindowEventMap[T]]);
41
- });
42
- };
43
-
44
- /**
45
- * Use this for response listeners. (../index.ts) where we want
46
- * all updates with data - no matter who sent the actual request
47
- * @param type
48
- * @param listener
49
- */
50
- export const listenForAllEvents = <T extends keyof WindowEventMap>(
51
- type: T,
52
- listener: (this: typeof rootElement, ev: WindowEventMap[T]) => void
53
- ) => {
54
- if (!rootElement) {
55
- log.error('No root element to bind event to!');
56
- return;
57
- }
58
- rootElement.addEventListener(type, (evt) => {
59
- listener.apply(rootElement, [evt as WindowEventMap[T]]);
60
- });
61
- };
62
-
63
- export const dispatch = <T extends Event>(event: T) => {
64
- if (!rootElement) {
65
- log.error('No root element to dispatch event on!');
66
- return;
67
- }
68
- /**
69
- * This will prevent any further event dispatching.
70
- * Effectively having requesters of data wait "forever" (the few
71
- * milliseconds) until we navigate away
72
- */
73
- if (store.get('isPoisoned')) {
74
- return;
75
- }
76
- rootElement.dispatchEvent(event);
77
- };
@@ -1,252 +0,0 @@
1
- import Debouncer from './debouncer';
2
-
3
- describe('Debouncer', () => {
4
- describe('when params is list of arrays and response is key value object', () => {
5
- let debouncer: Debouncer<string[], Record<string, string>>;
6
- const params1 = ['foo', 'bar'];
7
- const params2 = ['baz', '1337'];
8
- describe('when workload succeeds', () => {
9
- beforeEach(() => {
10
- debouncer = new Debouncer<string[], Record<string, string>>(
11
- 10,
12
- [],
13
- Debouncer.stringListParamsAccumulator,
14
- (params) =>
15
- Promise.resolve(
16
- params.reduce(
17
- (map, param): Record<string, string> => ({
18
- ...map,
19
- [param]: param.toUpperCase(),
20
- }),
21
- {} as Record<string, string>
22
- )
23
- )
24
- );
25
- });
26
- describe('when called in quick succession', () => {
27
- let result1: Record<string, string>;
28
- let result2: Record<string, string>;
29
- const expectedResult = {
30
- foo: 'FOO',
31
- bar: 'BAR',
32
- baz: 'BAZ',
33
- 1337: '1337',
34
- };
35
- beforeEach(async () => {
36
- const promise1 = debouncer.perform(params1);
37
- const promise2 = debouncer.perform(params2);
38
- result1 = await promise1;
39
- result2 = await promise2;
40
- });
41
- it('Resolution to first promise is complete set', () => {
42
- expect(result1).toEqual(expectedResult);
43
- });
44
- it('Resolution to second promise is complete set', () => {
45
- expect(result2).toEqual(expectedResult);
46
- });
47
- });
48
- describe('when waiting for first resolution before second call', () => {
49
- let result1: Record<string, string>;
50
- let result2: Record<string, string>;
51
- const expectedResult1 = {
52
- foo: 'FOO',
53
- bar: 'BAR',
54
- };
55
- const expectedResult2 = {
56
- baz: 'BAZ',
57
- 1337: '1337',
58
- };
59
- beforeEach(async () => {
60
- result1 = await debouncer.perform(params1);
61
- result2 = await debouncer.perform(params2);
62
- });
63
- it('Resolution to first promise is only result of first set', () => {
64
- expect(result1).toEqual(expectedResult1);
65
- });
66
- it('Resolution to second promise is only result of second set', () => {
67
- expect(result2).toEqual(expectedResult2);
68
- });
69
- });
70
- });
71
- describe('when workload fails', () => {
72
- beforeEach(() => {
73
- debouncer = new Debouncer<string[], Record<string, string>>(
74
- 10,
75
- [],
76
- Debouncer.stringListParamsAccumulator,
77
- (params) =>
78
- Promise.reject(new Error(`Cannot handle ${params.join(',')}`))
79
- );
80
- });
81
- describe('when called in quick succession', () => {
82
- let result1: string;
83
- let result2: string;
84
- beforeEach(async () => {
85
- const promise1 = debouncer.perform(params1);
86
- const promise2 = debouncer.perform(params2);
87
- try {
88
- await promise1;
89
- } catch (e) {
90
- result1 = e.message;
91
- }
92
- try {
93
- await promise2;
94
- } catch (e) {
95
- result2 = e.message;
96
- }
97
- });
98
- it('Resolution to first promise is error handling all params', () => {
99
- expect(result1).toEqual('Cannot handle foo,bar,baz,1337');
100
- });
101
- it('Resolution to second promise is error handling all params', async () => {
102
- expect(result2).toEqual('Cannot handle foo,bar,baz,1337');
103
- });
104
- });
105
- describe('when waiting for first resolution before second call', () => {
106
- let result1: string;
107
- let result2: string;
108
- beforeEach(async () => {
109
- try {
110
- await debouncer.perform(params1);
111
- } catch (e) {
112
- result1 = e.message;
113
- }
114
- try {
115
- await debouncer.perform(params2);
116
- } catch (e) {
117
- result2 = e.message;
118
- }
119
- });
120
- it('Resolution to first promise is error handling first set', () => {
121
- expect(result1).toEqual('Cannot handle foo,bar');
122
- });
123
- it('Resolution to second promise is error handling second set', () => {
124
- expect(result2).toEqual('Cannot handle baz,1337');
125
- });
126
- });
127
- });
128
- });
129
- describe('when params is key value object response is list of strings', () => {
130
- let debouncer: Debouncer<Record<string, string>, string[]>;
131
- const params1 = {
132
- foo: 'FOO',
133
- bar: 'BAR',
134
- };
135
- const params2 = {
136
- baz: 'BAZ',
137
- 1337: '1337',
138
- };
139
- describe('when workload succeeds', () => {
140
- beforeEach(() => {
141
- debouncer = new Debouncer<Record<string, string>, string[]>(
142
- 10,
143
- {},
144
- Debouncer.stringMapParamsAccumulator,
145
- (params) =>
146
- Promise.resolve(Object.entries(params).map(([k, v]) => `${k}=${v}`))
147
- );
148
- });
149
-
150
- describe('when called in quick succession', () => {
151
- let result1: string[];
152
- let result2: string[];
153
- const expectedResult = ['1337=1337', 'foo=FOO', 'bar=BAR', 'baz=BAZ'];
154
- beforeEach(async () => {
155
- const promise1 = debouncer.perform(params1);
156
- const promise2 = debouncer.perform(params2);
157
- result1 = await promise1;
158
- result2 = await promise2;
159
- });
160
- it('Resolution to first promise is complete set', () => {
161
- expect(result1).toEqual(expectedResult);
162
- });
163
- it('Resolution to second promise is complete set', () => {
164
- expect(result2).toEqual(expectedResult);
165
- });
166
- });
167
- describe('when waiting for first resolution before second call', () => {
168
- let result1: string[];
169
- let result2: string[];
170
- const expectedResult1 = ['foo=FOO', 'bar=BAR'];
171
- const expectedResult2 = ['1337=1337', 'baz=BAZ'];
172
- beforeEach(async () => {
173
- result1 = await debouncer.perform(params1);
174
- result2 = await debouncer.perform(params2);
175
- });
176
- it('Resolution to first promise is only result of first set', () => {
177
- expect(result1).toEqual(expectedResult1);
178
- });
179
- it('Resolution to second promise is only result of second set', () => {
180
- expect(result2).toEqual(expectedResult2);
181
- });
182
- });
183
- });
184
- describe('when workload fails', () => {
185
- beforeEach(() => {
186
- debouncer = new Debouncer<Record<string, string>, string[]>(
187
- 10,
188
- {},
189
- Debouncer.stringMapParamsAccumulator,
190
- (params) =>
191
- Promise.reject(
192
- new Error(
193
- `Cannot handle ${Object.entries(params)
194
- .map(([k, v]) => `${k}=${v}`)
195
- .join(',')}`
196
- )
197
- )
198
- );
199
- });
200
- describe('when called in quick succession', () => {
201
- let result1: string;
202
- let result2: string;
203
- beforeEach(async () => {
204
- const promise1 = debouncer.perform(params1);
205
- const promise2 = debouncer.perform(params2);
206
- try {
207
- await promise1;
208
- } catch (e) {
209
- result1 = e.message;
210
- }
211
- try {
212
- await promise2;
213
- } catch (e) {
214
- result2 = e.message;
215
- }
216
- });
217
- it('Resolution to first promise is error handling all params', () => {
218
- expect(result1).toEqual(
219
- 'Cannot handle 1337=1337,foo=FOO,bar=BAR,baz=BAZ'
220
- );
221
- });
222
- it('Resolution to second promise is error handling all params', async () => {
223
- expect(result2).toEqual(
224
- 'Cannot handle 1337=1337,foo=FOO,bar=BAR,baz=BAZ'
225
- );
226
- });
227
- });
228
- describe('when waiting for first resolution before second call', () => {
229
- let result1: string;
230
- let result2: string;
231
- beforeEach(async () => {
232
- try {
233
- await debouncer.perform(params1);
234
- } catch (e) {
235
- result1 = e.message;
236
- }
237
- try {
238
- await debouncer.perform(params2);
239
- } catch (e) {
240
- result2 = e.message;
241
- }
242
- });
243
- it('Resolution to first promise is error handling first set', () => {
244
- expect(result1).toEqual('Cannot handle foo=FOO,bar=BAR');
245
- });
246
- it('Resolution to second promise is error handling second set', () => {
247
- expect(result2).toEqual('Cannot handle 1337=1337,baz=BAZ');
248
- });
249
- });
250
- });
251
- });
252
- });