@amedia/user 0.6.1 → 0.7.1

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.
@@ -6,6 +6,7 @@ import { subscribeToUserAttributes } from './index';
6
6
  import * as userData from './actions/userData';
7
7
  import { setRootElement } from './utils/customEventWrapper';
8
8
  import { listenForUserAttributeRequests } from './eventHandlers/userAttributes';
9
+ import store from './store';
9
10
 
10
11
  beforeEach(() => {
11
12
  jest.resetAllMocks();
@@ -14,6 +15,9 @@ beforeEach(() => {
14
15
  // since tests are executed in parallel.
15
16
  setRootElement(new Document());
16
17
 
18
+ // Store must be cleared before we re-listen for user attribute requests
19
+ store.clear();
20
+
17
21
  // Also, since we keep creating new root elements, we also
18
22
  // need to attach to these new roots.
19
23
  listenForUserAttributeRequests();
@@ -21,13 +25,9 @@ beforeEach(() => {
21
25
 
22
26
  describe('Public endpoint - userdata', () => {
23
27
  it('should alert about user data', () => {
24
- jest
25
- .spyOn(userData, 'fetchDataBasedOnAttributes')
26
- .mockImplementation((_, callback: Function) => {
27
- callback({
28
- name: 'Donald Duck',
29
- });
30
- });
28
+ jest.spyOn(userData, 'requestAttributes').mockImplementation(() => {
29
+ store.set('userAttributes', { name: 'Donald Duck' });
30
+ });
31
31
 
32
32
  const onUserUpdate = jest.fn();
33
33
  subscribeToUserAttributes(['name'], onUserUpdate);
@@ -38,13 +38,9 @@ describe('Public endpoint - userdata', () => {
38
38
  });
39
39
 
40
40
  it('should not alert on same data', () => {
41
- jest
42
- .spyOn(userData, 'fetchDataBasedOnAttributes')
43
- .mockImplementation((_, callback: Function) => {
44
- callback({
45
- name: 'Donald Duck',
46
- });
47
- });
41
+ jest.spyOn(userData, 'requestAttributes').mockImplementation(() => {
42
+ store.set('userAttributes', { name: 'Donald Duck' });
43
+ });
48
44
 
49
45
  const onUserUpdate1 = jest.fn();
50
46
  subscribeToUserAttributes(['name'], onUserUpdate1);
@@ -64,17 +60,13 @@ describe('Public endpoint - userdata', () => {
64
60
 
65
61
  it('should alert when data changes', () => {
66
62
  jest
67
- .spyOn(userData, 'fetchDataBasedOnAttributes')
68
- .mockImplementationOnce((_, callback: Function) => {
69
- callback({
70
- name: undefined,
71
- });
72
- })
73
- .mockImplementationOnce((_, callback: Function) => {
74
- callback({
75
- name: 'Donald Duck',
76
- });
77
- });
63
+ .spyOn(userData, 'requestAttributes')
64
+ .mockImplementationOnce(() =>
65
+ store.set('userAttributes', { name: undefined })
66
+ )
67
+ .mockImplementationOnce(() =>
68
+ store.set('userAttributes', { name: 'Donald Duck' })
69
+ );
78
70
 
79
71
  const firstOnUserUpdate = jest.fn();
80
72
  subscribeToUserAttributes(['name'], firstOnUserUpdate);
@@ -94,15 +86,13 @@ describe('Public endpoint - userdata', () => {
94
86
  });
95
87
  });
96
88
 
97
- it('should not return more data that requested (except for login status)', () => {
98
- jest
99
- .spyOn(userData, 'fetchDataBasedOnAttributes')
100
- .mockImplementationOnce((_, callback: Function) => {
101
- callback({
102
- name: 'Donald Duck',
103
- email: 'donald.duck@andeby.no',
104
- });
89
+ it('should not return more data that requested', () => {
90
+ jest.spyOn(userData, 'requestAttributes').mockImplementation(() => {
91
+ store.set('userAttributes', {
92
+ name: 'Donald Duck',
93
+ email: 'donald.duck@andeby.no',
105
94
  });
95
+ });
106
96
 
107
97
  const onUserUpdate = jest.fn();
108
98
  subscribeToUserAttributes(['name'], onUserUpdate);
@@ -113,15 +103,12 @@ describe('Public endpoint - userdata', () => {
113
103
  });
114
104
 
115
105
  it('should support returning multiple requested attributes', () => {
116
- jest
117
- .spyOn(userData, 'fetchDataBasedOnAttributes')
118
-
119
- .mockImplementationOnce((_, callback: Function) => {
120
- callback({
121
- name: 'Donald Duck',
122
- email: 'donald.duck@andeby.no',
123
- });
106
+ jest.spyOn(userData, 'requestAttributes').mockImplementation(() => {
107
+ store.set('userAttributes', {
108
+ name: 'Donald Duck',
109
+ email: 'donald.duck@andeby.no',
124
110
  });
111
+ });
125
112
 
126
113
  const onUserUpdate = jest.fn();
127
114
  subscribeToUserAttributes(['name', 'email'], onUserUpdate);
@@ -133,15 +120,6 @@ describe('Public endpoint - userdata', () => {
133
120
  });
134
121
 
135
122
  it('should require at least one attribute', () => {
136
- jest
137
- .spyOn(userData, 'fetchDataBasedOnAttributes')
138
- .mockImplementationOnce((_, callback: Function) => {
139
- callback({
140
- name: 'Donald Duck',
141
- email: 'donald.duck@andeby.no',
142
- });
143
- });
144
-
145
123
  const onUserUpdate = jest.fn();
146
124
  expect(() => subscribeToUserAttributes([], onUserUpdate)).toThrowError(
147
125
  new Error('You must subscribe to at least one attribute')
@@ -149,14 +127,12 @@ describe('Public endpoint - userdata', () => {
149
127
  });
150
128
 
151
129
  it('should support requesting different attributes', () => {
152
- jest
153
- .spyOn(userData, 'fetchDataBasedOnAttributes')
154
- .mockImplementation((_, callback: Function) => {
155
- callback({
156
- name: 'Donald Duck',
157
- email: 'donald.duck@andeby.no',
158
- });
130
+ jest.spyOn(userData, 'requestAttributes').mockImplementation(() => {
131
+ store.set('userAttributes', {
132
+ name: 'Donald Duck',
133
+ email: 'donald.duck@andeby.no',
159
134
  });
135
+ });
160
136
 
161
137
  const firstOnUserUpdate = jest.fn();
162
138
  subscribeToUserAttributes(['name', 'email'], firstOnUserUpdate);
@@ -175,4 +151,20 @@ describe('Public endpoint - userdata', () => {
175
151
  email: 'donald.duck@andeby.no',
176
152
  });
177
153
  });
154
+
155
+ it('should also report state changes', () => {
156
+ jest.spyOn(userData, 'requestAttributes').mockImplementation(() => {
157
+ store.set('userAttributes', {
158
+ name: 'Donald Duck',
159
+ });
160
+ store.set('state', { isLoggedIn: true });
161
+ });
162
+
163
+ const onUserUpdate = jest.fn();
164
+ const onStateUpdate = jest.fn();
165
+ subscribeToUserAttributes(['name'], onUserUpdate, onStateUpdate);
166
+
167
+ expect(onUserUpdate).toHaveBeenCalledWith({ name: 'Donald Duck' });
168
+ expect(onStateUpdate).toHaveBeenCalledWith({ isLoggedIn: true });
169
+ });
178
170
  });
@@ -1,4 +1,4 @@
1
- import store from './store';
1
+ import store from '.';
2
2
 
3
3
  beforeEach(() => {
4
4
  store.clear();
@@ -17,15 +17,15 @@ const emit = (key: keyof StoreDataMap): void => {
17
17
  listeners[key]?.forEach((listener) => listener(store[key]));
18
18
  };
19
19
 
20
- const on = (
21
- key: keyof StoreDataMap,
22
- listener: Listener<StoreDataMap[typeof key]>
20
+ const on = <K extends keyof StoreDataMap, T = StoreDataMap[K]>(
21
+ key: K,
22
+ listener: Listener<T>
23
23
  ) => {
24
24
  if (!listeners[key]) {
25
25
  listeners[key] = [];
26
26
  }
27
27
 
28
- // @ts-ignore todo How can this possibly be undefined?
28
+ // @ts-ignore todo How can this possibly be undefined given the statement above?
29
29
  listeners[key].push(listener);
30
30
  };
31
31
 
@@ -35,12 +35,17 @@ const set = (key: keyof StoreDataMap, value: StoreDataMap[typeof key]) => {
35
35
  emit(key);
36
36
  };
37
37
 
38
- const get = (key: keyof StoreDataMap): StoreDataMap[typeof key] | undefined =>
39
- store[key];
38
+ const get = <K extends keyof StoreDataMap, T = StoreDataMap[K]>(
39
+ key: K
40
+ // @ts-ignore Not sure why this fails
41
+ ): T | undefined => store[key];
40
42
 
41
43
  const clear = () => {
42
44
  store.state = undefined;
43
45
  store.userAttributes = undefined;
46
+ Object.keys(listeners).forEach((key) => {
47
+ listeners[key] = [];
48
+ });
44
49
  };
45
50
 
46
51
  export default { set, get, on, clear };
package/src/types.ts CHANGED
@@ -3,7 +3,7 @@ export type UserAttributes = {
3
3
  email?: string;
4
4
  phone?: string;
5
5
  trackingKey?: string;
6
- extraData?: [];
6
+ extraData?: { [index: string]: unknown };
7
7
  access?: string[];
8
8
  privacyPreferences?: PrivacyPreferences;
9
9
  };
@@ -2,15 +2,17 @@ import { Runtype, Static } from 'runtypes';
2
2
  import queue from './queue';
3
3
  import cache from '../cache';
4
4
 
5
- const DEFAULT_TTL_10_SECONDS = 10000;
6
- const cacheTTLMap: { [index: string]: number } = {};
5
+ export type DataFetcherWithCacheTTL<T extends Runtype> = Promise<{
6
+ ttl: number;
7
+ data: Static<T>;
8
+ }>;
7
9
 
8
- const getOrUpdate = <T extends Runtype>(
10
+ export default function getOrUpdate<T extends Runtype>(
9
11
  key: string,
10
12
  schema: T,
11
- action: () => Promise<Static<T>>
12
- ): Promise<Static<T>> =>
13
- new Promise((resolve, reject) => {
13
+ fetch: () => DataFetcherWithCacheTTL<T>
14
+ ): Promise<Static<T>> {
15
+ return new Promise((resolve, reject) => {
14
16
  queue.add(key, { resolve, reject });
15
17
  if (queue.peekQueueSize(key) > 1) {
16
18
  return;
@@ -23,11 +25,11 @@ const getOrUpdate = <T extends Runtype>(
23
25
  });
24
26
  return;
25
27
  }
26
- action()
28
+ fetch()
27
29
  .then((res) => {
28
- cache.set(key, res, cacheTTLMap[key] || DEFAULT_TTL_10_SECONDS);
30
+ cache.set(key, res.data, res.ttl);
29
31
  queue.getAll(key).forEach((promise) => {
30
- promise.resolve(res);
32
+ promise.resolve(res.data);
31
33
  });
32
34
  })
33
35
  .catch(() => {
@@ -37,5 +39,4 @@ const getOrUpdate = <T extends Runtype>(
37
39
  });
38
40
  });
39
41
  });
40
-
41
- export default getOrUpdate;
42
+ }