forked from parse-community/Parse-SDK-JS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathObjectStateMutations-test.js
354 lines (325 loc) · 10.7 KB
/
ObjectStateMutations-test.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
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
jest.dontMock('../decode');
jest.dontMock('../encode');
jest.dontMock('../CoreManager');
jest.dontMock('../ObjectStateMutations');
jest.dontMock('../ParseFile');
jest.dontMock('../ParseGeoPoint');
jest.dontMock('../ParseOp');
jest.dontMock('../ParseRelation');
jest.dontMock('../TaskQueue');
const mockObject = function (className) {
this.className = className;
};
mockObject.registerSubclass = function () {};
jest.setMock('../ParseObject', mockObject);
const CoreManager = require('../CoreManager').default;
CoreManager.setParseObject(mockObject);
const ObjectStateMutations = require('../ObjectStateMutations');
const ParseOps = require('../ParseOp');
const TaskQueue = require('../TaskQueue').default;
describe('ObjectStateMutations', () => {
it('can apply server data', () => {
const serverData = {};
ObjectStateMutations.setServerData(serverData, { counter: 12 });
expect(serverData).toEqual({ counter: 12 });
ObjectStateMutations.setServerData(serverData, { counter: undefined });
expect(serverData).toEqual({});
});
it('can set a pending op', () => {
let pendingOps = [{}];
const op = new ParseOps.IncrementOp(1);
ObjectStateMutations.setPendingOp(pendingOps, 'counter', op);
expect(pendingOps).toEqual([{ counter: op }]);
pendingOps = [{}, {}];
ObjectStateMutations.setPendingOp(pendingOps, 'counter', op);
expect(pendingOps).toEqual([{}, { counter: op }]);
ObjectStateMutations.setPendingOp(pendingOps, 'counter', null);
expect(pendingOps).toEqual([{}, {}]);
});
it('can push a new pending state', () => {
const pendingOps = [{}];
ObjectStateMutations.pushPendingState(pendingOps);
expect(pendingOps).toEqual([{}, {}]);
ObjectStateMutations.pushPendingState(pendingOps);
expect(pendingOps).toEqual([{}, {}, {}]);
});
it('can pop a pending state', () => {
let pendingOps = [{}];
let first = pendingOps[0];
expect(ObjectStateMutations.popPendingState(pendingOps)).toBe(first);
expect(pendingOps).toEqual([{}]);
const op = new ParseOps.IncrementOp(1);
pendingOps = [{ counter: op }, {}, {}];
first = pendingOps[0];
expect(ObjectStateMutations.popPendingState(pendingOps)).toBe(first);
expect(pendingOps).toEqual([{}, {}]);
});
it('can merge the first op set into the next', () => {
let pendingOps = [{ counter: new ParseOps.SetOp(1), name: new ParseOps.SetOp('foo') }, {}];
ObjectStateMutations.mergeFirstPendingState(pendingOps);
expect(pendingOps).toEqual([
{ counter: new ParseOps.SetOp(1), name: new ParseOps.SetOp('foo') },
]);
pendingOps = [{ counter: new ParseOps.SetOp(1) }, { counter: new ParseOps.IncrementOp(1) }];
ObjectStateMutations.mergeFirstPendingState(pendingOps);
expect(pendingOps).toEqual([{ counter: new ParseOps.SetOp(2) }]);
});
it('can estimate an attribute value', () => {
const serverData = { counter: 12 };
const pendingOps = [{ counter: new ParseOps.IncrementOp(2), name: new ParseOps.SetOp('foo') }];
expect(
ObjectStateMutations.estimateAttribute(
serverData,
pendingOps,
{ className: 'someClass', id: 'someId' },
'counter'
)
).toBe(14);
expect(
ObjectStateMutations.estimateAttribute(
serverData,
pendingOps,
{ className: 'someClass', id: 'someId' },
'name'
)
).toBe('foo');
pendingOps.push({
counter: new ParseOps.IncrementOp(1),
name: new ParseOps.SetOp('override'),
});
expect(
ObjectStateMutations.estimateAttribute(
serverData,
pendingOps,
{ className: 'someClass', id: 'someId' },
'counter'
)
).toBe(15);
expect(
ObjectStateMutations.estimateAttribute(
serverData,
pendingOps,
{ className: 'someClass', id: 'someId' },
'name'
)
).toBe('override');
pendingOps.push({ likes: new ParseOps.RelationOp([], []) });
const relation = ObjectStateMutations.estimateAttribute(
serverData,
pendingOps,
{ className: 'someClass', id: 'someId' },
'likes'
);
expect(relation.parent.id).toBe('someId');
expect(relation.parent.className).toBe('someClass');
expect(relation.key).toBe('likes');
});
it('can estimate all attributes', () => {
const serverData = { counter: 12 };
const pendingOps = [{ counter: new ParseOps.IncrementOp(2), name: new ParseOps.SetOp('foo') }];
expect(
ObjectStateMutations.estimateAttributes(serverData, pendingOps, 'someClass', 'someId')
).toEqual({
counter: 14,
name: 'foo',
});
pendingOps.push({
counter: new ParseOps.IncrementOp(1),
name: new ParseOps.SetOp('override'),
});
expect(
ObjectStateMutations.estimateAttributes(serverData, pendingOps, 'someClass', 'someId')
).toEqual({
counter: 15,
name: 'override',
});
pendingOps.push({ likes: new ParseOps.RelationOp([], []) });
const attributes = ObjectStateMutations.estimateAttributes(serverData, pendingOps, {
className: 'someClass',
id: 'someId',
});
expect(attributes.likes.parent.id).toBe('someId');
expect(attributes.likes.parent.className).toBe('someClass');
expect(attributes.likes.key).toBe('likes');
});
it('can estimate attributes for nested documents', () => {
let serverData = { objectField: { counter: 10, letter: 'a' } };
let pendingOps = [{ 'objectField.counter': new ParseOps.IncrementOp(2) }];
expect(
ObjectStateMutations.estimateAttributes(serverData, pendingOps, 'someClass', 'someId')
).toEqual({
objectField: {
counter: 12,
letter: 'a',
},
});
pendingOps = [{ 'objectField.counter': new ParseOps.SetOp(20) }];
expect(
ObjectStateMutations.estimateAttributes(serverData, pendingOps, 'someClass', 'someId')
).toEqual({
objectField: {
counter: 20,
letter: 'a',
},
});
serverData = {};
pendingOps = [{ 'objectField.subField.subField.counter': new ParseOps.IncrementOp(20) }];
expect(
ObjectStateMutations.estimateAttributes(serverData, pendingOps, 'someClass', 'someId')
).toEqual({
objectField: {
subField: {
subField: {
counter: 20,
},
},
},
});
});
it('can estimate attributes for nested array documents', () => {
// Test without initial value
let serverData = { _id: 'someId', className: 'bug' };
let pendingOps = [{ 'items.0.count': new ParseOps.IncrementOp(1) }];
expect(
ObjectStateMutations.estimateAttributes(serverData, pendingOps, 'someClass', 'someId')
).toEqual({
_id: 'someId',
items: [{ count: 1 }],
className: 'bug',
});
// Test one level nested
serverData = {
_id: 'someId',
items: [
{ value: 'a', count: 5 },
{ value: 'b', count: 1 },
],
className: 'bug',
number: 2,
};
pendingOps = [{ 'items.0.count': new ParseOps.IncrementOp(1) }];
expect(
ObjectStateMutations.estimateAttributes(serverData, pendingOps, 'someClass', 'someId')
).toEqual({
_id: 'someId',
items: [
{ value: 'a', count: 6 },
{ value: 'b', count: 1 },
],
className: 'bug',
number: 2,
});
// Test multiple level nested fields
serverData = {
_id: 'someId',
items: [
{ value: { count: 54 }, count: 5 },
{ value: 'b', count: 1 },
],
className: 'bug',
number: 2,
};
pendingOps = [{ 'items.0.value.count': new ParseOps.IncrementOp(6) }];
expect(
ObjectStateMutations.estimateAttributes(serverData, pendingOps, 'someClass', 'someId')
).toEqual({
_id: 'someId',
items: [
{ value: { count: 60 }, count: 5 },
{ value: 'b', count: 1 },
],
className: 'bug',
number: 2,
});
});
it('can commit changes from the server', () => {
const serverData = {};
const objectCache = {};
ObjectStateMutations.commitServerChanges(serverData, objectCache, {
name: 'foo',
data: { count: 5 },
});
expect(serverData).toEqual({ name: 'foo', data: { count: 5 } });
expect(objectCache).toEqual({ data: '{"count":5}' });
});
it('can commit nested changes from the server', () => {
const serverData = {};
const objectCache = {};
ObjectStateMutations.commitServerChanges(serverData, objectCache, {
'name.foo': 'bar',
data: { count: 5 },
});
expect(serverData).toEqual({ name: { foo: 'bar' }, data: { count: 5 } });
expect(objectCache).toEqual({ data: '{"count":5}' });
});
it('can commit dot notation array changes from the server', () => {
const serverData = {
items: [
{ value: 'a', count: 5 },
{ value: 'b', count: 1 },
],
};
ObjectStateMutations.commitServerChanges(
serverData,
{},
{
'items.0.count': 15,
'items.1.count': 4,
}
);
expect(serverData).toEqual({
items: [
{ value: 'a', count: 15 },
{ value: 'b', count: 4 },
],
});
});
it('can commit dot notation array changes from the server to empty serverData', () => {
const serverData = {};
ObjectStateMutations.commitServerChanges(
serverData,
{},
{
'items.0.count': 15,
'items.1.count': 4,
}
);
expect(serverData).toEqual({ items: [{ count: 15 }, { count: 4 }] });
});
it('can commit nested json array changes from the server to empty serverData', () => {
const serverData = {};
const objectCache = {};
ObjectStateMutations.commitServerChanges(serverData, objectCache, {
items: { '0': { count: 20 }, '1': { count: 5 } },
});
// Should not transform
expect(serverData).toEqual({ items: { '0': { count: 20 }, '1': { count: 5 } } });
expect(objectCache).toEqual({ items: '{"0":{"count":20},"1":{"count":5}}' });
});
it('can commit json array with PushStatus offset fields', () => {
const serverData = {};
const objectCache = {};
ObjectStateMutations.commitServerChanges(serverData, objectCache, {
sentPerUTCOffset: { '1': { count: 20 } },
failedPerUTCOffset: { '5': { count: 25 } },
});
// Should not transform to an array
expect(serverData).toEqual({
sentPerUTCOffset: { '1': { count: 20 } },
failedPerUTCOffset: { '5': { count: 25 } },
});
expect(objectCache).toEqual({
sentPerUTCOffset: '{"1":{"count":20}}',
failedPerUTCOffset: '{"5":{"count":25}}',
});
});
it('can generate a default state for implementations', () => {
expect(ObjectStateMutations.defaultState()).toEqual({
serverData: {},
pendingOps: [{}],
objectCache: {},
tasks: new TaskQueue(),
existed: false,
});
});
});