This repository has been archived by the owner on Nov 27, 2023. It is now read-only.
forked from parse-community/Parse-SDK-JS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathObjectStateMutations-test.js
211 lines (190 loc) · 6.55 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
jest.dontMock('../decode');
jest.dontMock('../encode');
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 ObjectStateMutations = require('../ObjectStateMutations');
const ParseOps = require('../ParseOp');
const TaskQueue = require('../TaskQueue');
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,
'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([], []) });
const 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', () => {
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,
'someClass',
'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 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 generate a default state for implementations', () => {
expect(ObjectStateMutations.defaultState()).toEqual({
serverData: {},
pendingOps: [{}],
objectCache: {},
tasks: new TaskQueue(),
existed: false,
});
});
});