-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathonyxClearWebStorageTest.ts
More file actions
271 lines (240 loc) · 11.7 KB
/
onyxClearWebStorageTest.ts
File metadata and controls
271 lines (240 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
import waitForPromisesToResolve from '../utils/waitForPromisesToResolve';
import StorageMock from '../../lib/storage';
import Onyx from '../../lib/Onyx';
import type OnyxCache from '../../lib/OnyxCache';
import type GenericCollection from '../utils/GenericCollection';
import type {Connection} from '../../lib/OnyxConnectionManager';
const ONYX_KEYS = {
DEFAULT_KEY: 'defaultKey',
REGULAR_KEY: 'regularKey',
COLLECTION: {
TEST: 'test_',
},
};
const SET_VALUE = 'set';
const MERGED_VALUE = 'merged';
const DEFAULT_VALUE = 'default';
describe('Set data while storage is clearing', () => {
let connection: Connection | undefined;
let onyxValue: unknown;
/** @type OnyxCache */
let cache: typeof OnyxCache;
// Always use a "fresh" cache instance
beforeEach(() => {
onyxValue = null;
cache = require('../../lib/OnyxCache').default;
Onyx.init({
keys: ONYX_KEYS,
initialKeyStates: {
[ONYX_KEYS.DEFAULT_KEY]: DEFAULT_VALUE,
},
});
connection = Onyx.connect({
key: ONYX_KEYS.DEFAULT_KEY,
initWithStoredValues: false,
callback: (val) => (onyxValue = val),
});
return waitForPromisesToResolve();
});
afterEach(() => {
if (connection) {
Onyx.disconnect(connection);
}
return Onyx.clear();
});
it('should replace the value of Onyx.set with the default key state in the cache', () => {
expect.assertions(3);
// Given that Onyx is completely clear
// When set then clear is called on a key with a default key state
Onyx.set(ONYX_KEYS.DEFAULT_KEY, SET_VALUE);
Onyx.clear();
return waitForPromisesToResolve().then(() => {
// Then the value in Onyx, the cache, and Storage is the default key state
expect(onyxValue).toBe(DEFAULT_VALUE);
const cachedValue = cache.get(ONYX_KEYS.DEFAULT_KEY);
expect(cachedValue).toBe(DEFAULT_VALUE);
const storedValue = StorageMock.getItem(ONYX_KEYS.DEFAULT_KEY);
return expect(storedValue).resolves.toBe(DEFAULT_VALUE);
});
});
it('should replace the value of Onyx.merge with the default key state in the cache', () => {
expect.assertions(3);
// Given that Onyx is completely clear
// When merge then clear is called on a key with a default key state
Onyx.merge(ONYX_KEYS.DEFAULT_KEY, MERGED_VALUE);
Onyx.clear();
return waitForPromisesToResolve().then(() => {
// Then the value in Onyx, the cache, and Storage is the default key state
expect(onyxValue).toBe(DEFAULT_VALUE);
const cachedValue = cache.get(ONYX_KEYS.DEFAULT_KEY);
expect(cachedValue).toBe(DEFAULT_VALUE);
const storedValue = StorageMock.getItem(ONYX_KEYS.DEFAULT_KEY);
return expect(storedValue).resolves.toBe(DEFAULT_VALUE);
});
});
it('should preserve the value of any keysToPreserve passed in', () => {
expect.assertions(3);
// Given that Onyx has a value, and we have a variable listening to that value
let regularKeyOnyxValue: unknown;
Onyx.connect({
key: ONYX_KEYS.REGULAR_KEY,
initWithStoredValues: false,
callback: (val) => (regularKeyOnyxValue = val),
});
Onyx.set(ONYX_KEYS.REGULAR_KEY, SET_VALUE).then(() => {
// When clear is called with a key to preserve
Onyx.clear([ONYX_KEYS.REGULAR_KEY]);
});
return waitForPromisesToResolve().then(() => {
// Then the value of the preserved key is also still set in both the cache and storage
expect(regularKeyOnyxValue).toBe(SET_VALUE);
const regularKeyCachedValue = cache.get(ONYX_KEYS.REGULAR_KEY);
expect(regularKeyCachedValue).toBe(SET_VALUE);
const regularKeyStoredValue = StorageMock.getItem(ONYX_KEYS.REGULAR_KEY);
return expect(regularKeyStoredValue).resolves.toBe(SET_VALUE);
});
});
it('should preserve the value of any keysToPreserve over any default key states', () => {
expect.assertions(3);
// Given that Onyx has a value for a key with a default, and we have a variable listening to that value
Onyx.set(ONYX_KEYS.DEFAULT_KEY, SET_VALUE).then(() => {
// When clear is called with the default key to preserve
Onyx.clear([ONYX_KEYS.DEFAULT_KEY]);
});
return waitForPromisesToResolve().then(() => {
// Then the value in Onyx, the cache, and Storage is the set value
expect(onyxValue).toBe(SET_VALUE);
const cachedValue = cache.get(ONYX_KEYS.DEFAULT_KEY);
expect(cachedValue).toBe(SET_VALUE);
const storedValue = StorageMock.getItem(ONYX_KEYS.DEFAULT_KEY);
return expect(storedValue).resolves.toBe(SET_VALUE);
});
});
it('should preserve all collection members when a collection key is passed to keysToPreserve', () => {
expect.assertions(6);
const collectionItemKey1 = `${ONYX_KEYS.COLLECTION.TEST}1`;
const collectionItemKey2 = `${ONYX_KEYS.COLLECTION.TEST}2`;
// Given that Onyx has a collection with two items
return (
Onyx.mergeCollection(ONYX_KEYS.COLLECTION.TEST, {
[collectionItemKey1]: {id: 1, name: 'first'},
[collectionItemKey2]: {id: 2, name: 'second'},
} as GenericCollection)
// When clear is called with the collection prefix as a key to preserve
.then(() => Onyx.clear([ONYX_KEYS.COLLECTION.TEST]))
.then(() => waitForPromisesToResolve())
.then(() => {
// Then both collection members are preserved in the cache and storage
expect(cache.get(collectionItemKey1)).toEqual({id: 1, name: 'first'});
expect(cache.get(collectionItemKey2)).toEqual({id: 2, name: 'second'});
return Promise.all([StorageMock.getItem(collectionItemKey1), StorageMock.getItem(collectionItemKey2)]);
})
.then(([storedValue1, storedValue2]) => {
expect(storedValue1).toEqual({id: 1, name: 'first'});
expect(storedValue2).toEqual({id: 2, name: 'second'});
// And non-collection keys are still cleared (default key reset to default)
expect(cache.get(ONYX_KEYS.DEFAULT_KEY)).toBe(DEFAULT_VALUE);
return expect(StorageMock.getItem(ONYX_KEYS.DEFAULT_KEY)).resolves.toBe(DEFAULT_VALUE);
})
);
});
it('should preserve collection members and still clear regular keys not in keysToPreserve', () => {
expect.assertions(4);
const collectionItemKey1 = `${ONYX_KEYS.COLLECTION.TEST}1`;
// Given that Onyx has both a collection item and a regular key set
return (
Promise.all([Onyx.set(ONYX_KEYS.REGULAR_KEY, SET_VALUE), Onyx.mergeCollection(ONYX_KEYS.COLLECTION.TEST, {[collectionItemKey1]: 'value'} as GenericCollection)])
// When clear is called preserving only the collection
.then(() => Onyx.clear([ONYX_KEYS.COLLECTION.TEST]))
.then(() => waitForPromisesToResolve())
.then(() => {
// Then the collection member is preserved
expect(cache.get(collectionItemKey1)).toBe('value');
return expect(StorageMock.getItem(collectionItemKey1)).resolves.toBe('value');
})
.then(() => {
// And the regular key is cleared
expect(cache.get(ONYX_KEYS.REGULAR_KEY)).toBeUndefined();
return expect(StorageMock.getItem(ONYX_KEYS.REGULAR_KEY)).resolves.toBeNull();
})
);
});
it('should preserve both collection keys and individual keys when both are passed to keysToPreserve', () => {
expect.assertions(4);
const collectionItemKey1 = `${ONYX_KEYS.COLLECTION.TEST}1`;
// Given that Onyx has a collection item and a regular key set
return (
Promise.all([Onyx.set(ONYX_KEYS.REGULAR_KEY, SET_VALUE), Onyx.mergeCollection(ONYX_KEYS.COLLECTION.TEST, {[collectionItemKey1]: 'value'} as GenericCollection)])
// When clear is called preserving both the collection and the regular key
.then(() => Onyx.clear([ONYX_KEYS.COLLECTION.TEST, ONYX_KEYS.REGULAR_KEY]))
.then(() => waitForPromisesToResolve())
.then(() => {
// Then both the collection member and the regular key are preserved
expect(cache.get(collectionItemKey1)).toBe('value');
expect(cache.get(ONYX_KEYS.REGULAR_KEY)).toBe(SET_VALUE);
return Promise.all([StorageMock.getItem(collectionItemKey1), StorageMock.getItem(ONYX_KEYS.REGULAR_KEY)]);
})
.then(([storedCollectionValue, storedRegularValue]) => {
expect(storedCollectionValue).toBe('value');
expect(storedRegularValue).toBe(SET_VALUE);
})
);
});
it('should only trigger the connection callback once when using wait for collection callback', () => {
expect.assertions(4);
// Given a mocked callback function and a collection with four items in it
const collectionCallback = jest.fn();
const testConnection = Onyx.connect({
key: ONYX_KEYS.COLLECTION.TEST,
waitForCollectionCallback: true,
callback: collectionCallback,
});
return (
waitForPromisesToResolve()
.then(() =>
Onyx.mergeCollection(ONYX_KEYS.COLLECTION.TEST, {
[`${ONYX_KEYS.COLLECTION.TEST}1`]: 1,
[`${ONYX_KEYS.COLLECTION.TEST}2`]: 2,
[`${ONYX_KEYS.COLLECTION.TEST}3`]: 3,
[`${ONYX_KEYS.COLLECTION.TEST}4`]: 4,
} as GenericCollection),
)
// When onyx is cleared
.then(() => Onyx.clear())
.then(() => {
Onyx.disconnect(testConnection);
})
.then(() => {
// Then the collection callback should only have been called three times:
// 1. connect()
// 2. merge()
// 3. clear()
expect(collectionCallback).toHaveBeenCalledTimes(3);
// And it should be called with the expected parameters each time
expect(collectionCallback).toHaveBeenNthCalledWith(1, undefined, ONYX_KEYS.COLLECTION.TEST, undefined);
expect(collectionCallback).toHaveBeenNthCalledWith(
2,
{
test_1: 1,
test_2: 2,
test_3: 3,
test_4: 4,
},
ONYX_KEYS.COLLECTION.TEST,
{
test_1: 1,
test_2: 2,
test_3: 3,
test_4: 4,
},
);
expect(collectionCallback).toHaveBeenLastCalledWith({}, ONYX_KEYS.COLLECTION.TEST, {
test_1: undefined,
test_2: undefined,
test_3: undefined,
test_4: undefined,
});
})
);
});
});