-
Notifications
You must be signed in to change notification settings - Fork 74
/
onyxMultiMergeWebStorageTest.js
177 lines (147 loc) · 6.69 KB
/
onyxMultiMergeWebStorageTest.js
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
import OnyxCache from '../../lib/OnyxCache';
import waitForPromisesToResolve from '../utils/waitForPromisesToResolve';
import localforageMock from '../../__mocks__/localforage';
const ONYX_KEYS = {
COLLECTION: {
TEST_KEY: 'test_',
},
};
const initialTestObject = {a: 'a'};
const initialData = {
test_1: initialTestObject,
test_2: initialTestObject,
test_3: initialTestObject,
};
localforageMock.setInitialMockData(initialData);
describe('Onyx.mergeCollection() amd WebStorage', () => {
let Onyx;
beforeAll(() => {
// Force using WebStorage provider for these tests
jest.mock('../../lib/storage');
Onyx = require('../../lib').default;
jest.useRealTimers();
Onyx.init({
keys: ONYX_KEYS,
registerStorageEventListener: () => {},
initialKeyStates: {},
});
});
afterEach(() => Onyx.clear());
it('merges two sets of data consecutively', () => {
// Given initial data in storage
expect(localforageMock.storageMap.test_1).toEqual(initialTestObject);
expect(localforageMock.storageMap.test_2).toEqual(initialTestObject);
expect(localforageMock.storageMap.test_3).toEqual(initialTestObject);
// And an empty cache values for the collection keys
expect(OnyxCache.getValue('test_1')).not.toBeDefined();
expect(OnyxCache.getValue('test_2')).not.toBeDefined();
expect(OnyxCache.getValue('test_3')).not.toBeDefined();
// When we merge additional data
const additionalDataOne = {b: 'b', c: 'c', e: [1, 2]};
Onyx.mergeCollection(ONYX_KEYS.COLLECTION.TEST_KEY, {
test_1: additionalDataOne,
test_2: additionalDataOne,
test_3: additionalDataOne,
});
// And call again consecutively with different data
const additionalDataTwo = {d: 'd', e: [2]};
Onyx.mergeCollection(ONYX_KEYS.COLLECTION.TEST_KEY, {
test_1: additionalDataTwo,
test_2: additionalDataTwo,
test_3: additionalDataTwo,
});
return waitForPromisesToResolve()
.then(() => {
const finalObject = {
a: 'a', b: 'b', c: 'c', d: 'd', e: [2],
};
// Then our new data should merge with the existing data in the cache
expect(OnyxCache.getValue('test_1')).toEqual(finalObject);
expect(OnyxCache.getValue('test_2')).toEqual(finalObject);
expect(OnyxCache.getValue('test_3')).toEqual(finalObject);
// And the storage should reflect the same state
expect(localforageMock.storageMap.test_1).toEqual(finalObject);
expect(localforageMock.storageMap.test_2).toEqual(finalObject);
expect(localforageMock.storageMap.test_3).toEqual(finalObject);
});
});
it('cache updates correctly when accessed again if keys are removed or evicted', () => {
// Given empty storage
expect(localforageMock.storageMap.test_1).toBeFalsy();
expect(localforageMock.storageMap.test_2).toBeFalsy();
expect(localforageMock.storageMap.test_3).toBeFalsy();
// And an empty cache values for the collection keys
expect(OnyxCache.getValue('test_1')).toBeFalsy();
expect(OnyxCache.getValue('test_2')).toBeFalsy();
expect(OnyxCache.getValue('test_3')).toBeFalsy();
// When we merge additional data and wait for the change
const data = {a: 'a', b: 'b'};
Onyx.mergeCollection(ONYX_KEYS.COLLECTION.TEST_KEY, {
test_1: data,
test_2: data,
test_3: data,
});
return waitForPromisesToResolve()
.then(() => {
// Then the cache and storage should match
expect(OnyxCache.getValue('test_1')).toEqual(data);
expect(OnyxCache.getValue('test_2')).toEqual(data);
expect(OnyxCache.getValue('test_3')).toEqual(data);
expect(localforageMock.storageMap.test_1).toEqual(data);
expect(localforageMock.storageMap.test_2).toEqual(data);
expect(localforageMock.storageMap.test_3).toEqual(data);
// When we drop all the cache keys (but do not modify the underlying storage) and merge another object
OnyxCache.drop('test_1');
OnyxCache.drop('test_2');
OnyxCache.drop('test_3');
const additionalData = {c: 'c'};
Onyx.mergeCollection(ONYX_KEYS.COLLECTION.TEST_KEY, {
test_1: additionalData,
test_2: additionalData,
test_3: additionalData,
});
return waitForPromisesToResolve();
})
.then(() => {
const finalObject = {
a: 'a', b: 'b', c: 'c',
};
// Then our new data should merge with the existing data in the cache
expect(OnyxCache.getValue('test_1')).toEqual(finalObject);
expect(OnyxCache.getValue('test_2')).toEqual(finalObject);
expect(OnyxCache.getValue('test_3')).toEqual(finalObject);
// And the storage should reflect the same state
expect(localforageMock.storageMap.test_1).toEqual(finalObject);
expect(localforageMock.storageMap.test_2).toEqual(finalObject);
expect(localforageMock.storageMap.test_3).toEqual(finalObject);
});
});
it('setItem() and multiMerge()', () => {
// When Onyx is cleared, since it uses multiSet() to clear out all the values, the keys will remain with null for all the values
expect(localforageMock.storageMap).toEqual({
test_1: null,
test_2: null,
test_3: null,
});
// Given no previous data and several calls to setItem and call to mergeCollection to update a given key
// 1st call
Onyx.set('test_1', {a: 'a'});
// These merges will all queue together
Onyx.merge('test_1', {b: 'b'});
Onyx.merge('test_1', {c: 'c'});
// 2nd call
Onyx.mergeCollection(ONYX_KEYS.COLLECTION.TEST_KEY, {
test_1: {d: 'd', e: 'e'},
});
// Last call
Onyx.merge('test_1', {f: 'f'});
return waitForPromisesToResolve()
.then(() => {
const finalObject = {
a: 'a', b: 'b', c: 'c', f: 'f', d: 'd', e: 'e',
};
expect(OnyxCache.getValue('test_1')).toEqual(finalObject);
expect(localforageMock.storageMap.test_1).toEqual(finalObject);
});
});
});