forked from parse-community/Parse-SDK-JS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathObjectStateMutations-test.js
140 lines (119 loc) · 5.54 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
/**
* Copyright (c) 2015-present, Parse, LLC.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
jest.dontMock('../decode');
jest.dontMock('../encode');
jest.dontMock('../ObjectStateMutations');
jest.dontMock('../ParseFile');
jest.dontMock('../ParseGeoPoint');
jest.dontMock('../ParseOp');
jest.dontMock('../ParsePromise');
jest.dontMock('../ParseRelation');
jest.dontMock('../TaskQueue');
const mockObject = function(className) {
this.className = className;
};
mockObject.registerSubclass = function() {};
jest.setMock('../ParseObject', mockObject);
const ObjectStateMutations = require('../ObjectStateMutations');
const ParseOps = require('../ParseOp');
const TaskQueue = require('../TaskQueue');
describe('ObjectStateMutations', () => {
it('can apply server data', () => {
let 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 = [{}];
let 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', () => {
let 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([{}]);
let 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', () => {
let serverData = { counter: 12 };
let pendingOps = [{ counter: new ParseOps.IncrementOp(2), name: new ParseOps.SetOp('foo') }];
expect(ObjectStateMutations.estimateAttribute(serverData, pendingOps, 'someClass', 'someId', 'counter')).toBe(14);
expect(ObjectStateMutations.estimateAttribute(serverData, pendingOps, 'someClass', 'someId', 'name')).toBe('foo');
pendingOps.push({ counter: new ParseOps.IncrementOp(1), name: new ParseOps.SetOp('override') });
expect(ObjectStateMutations.estimateAttribute(serverData, pendingOps, 'someClass', 'someId', 'counter')).toBe(15);
expect(ObjectStateMutations.estimateAttribute(serverData, pendingOps, 'someClass', 'someId', 'name')).toBe('override');
pendingOps.push({ likes: new ParseOps.RelationOp([], []) });
let relation = ObjectStateMutations.estimateAttribute(serverData, pendingOps, 'someClass', 'someId', 'likes');
expect(relation.parent.id).toBe('someId');
expect(relation.parent.className).toBe('someClass');
expect(relation.key).toBe('likes');
});
it('can estimate all attributes', () => {
let serverData = { counter: 12 };
let 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([], []) });
let attributes = ObjectStateMutations.estimateAttributes(serverData, pendingOps, 'someClass', 'someId');
expect(attributes.likes.parent.id).toBe('someId');
expect(attributes.likes.parent.className).toBe('someClass');
expect(attributes.likes.key).toBe('likes');
});
it('can commit changes from the server', () => {
let serverData = {};
let 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 generate a default state for implementations', () => {
expect(ObjectStateMutations.defaultState()).toEqual({
serverData: {},
pendingOps: [{}],
objectCache: {},
tasks: new TaskQueue(),
existed: false
});
});
});