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 pathParseRelation-test.js
282 lines (263 loc) · 8.37 KB
/
ParseRelation-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
jest.dontMock('../encode');
jest.dontMock('../ParseRelation');
jest.dontMock('../ParseOp');
jest.dontMock('../unique');
const mockStore = {};
const mockObject = function (className) {
this.className = className;
this.ops = {};
};
mockObject.registerSubclass = function () {};
mockObject.prototype = {
_getId() {
return this.id;
},
set(key, value) {
if (!mockStore[this.id]) {
mockStore[this.id] = {};
}
if (!mockStore[this.id][key]) {
mockStore[this.id][key] = [];
}
mockStore[this.id][key].push(value);
},
relation(key) {
if (mockStore[this.id][key]) {
return this.get(key);
}
return new ParseRelation(this, key);
},
get(key) {
return this.op(key).applyTo(null, { className: this.className, id: this.id }, key);
},
op(key) {
let finalOp = undefined;
for (let i = 0; i < mockStore[this.id][key].length; i++) {
finalOp = mockStore[this.id][key][i].mergeWith(finalOp);
}
return finalOp;
},
};
jest.setMock('../ParseObject', mockObject);
const mockQuery = function (className) {
this.className = className;
this.where = {};
this._extraOptions = {};
};
mockQuery.prototype = {
_addCondition(key, comparison, value) {
this.where[key] = this.where[key] || {};
this.where[key][comparison] = value;
},
};
jest.setMock('../ParseQuery', mockQuery);
const ParseObject = require('../ParseObject');
const ParseRelation = require('../ParseRelation').default;
describe('ParseRelation', () => {
it('can be constructed with a reference parent and key', () => {
const parent = new ParseObject('Item');
parent.id = 'I1';
const r = new ParseRelation(parent, 'shipments');
expect(r.parent).toBe(parent);
expect(r.key).toBe('shipments');
expect(r.targetClassName).toBe(null);
});
it('can add objects to a relation', () => {
const parent = new ParseObject('Item');
parent.id = 'I1';
const r = new ParseRelation(parent, 'shipments');
const o = new ParseObject('Delivery');
o.id = 'D1';
const p = r.add(o);
expect(p).toBeTruthy();
expect(r.toJSON()).toEqual({
__type: 'Relation',
className: 'Delivery',
});
expect(parent.op('shipments').toJSON()).toEqual({
__op: 'AddRelation',
objects: [{ __type: 'Pointer', objectId: 'D1', className: 'Delivery' }],
});
const o2 = new ParseObject('Delivery');
o2.id = 'D2';
const o3 = new ParseObject('Delivery');
o3.id = 'D3';
r.add([o2, o3]);
expect(r.toJSON()).toEqual({
__type: 'Relation',
className: 'Delivery',
});
expect(parent.op('shipments').toJSON()).toEqual({
__op: 'AddRelation',
objects: [
{ __type: 'Pointer', objectId: 'D1', className: 'Delivery' },
{ __type: 'Pointer', objectId: 'D2', className: 'Delivery' },
{ __type: 'Pointer', objectId: 'D3', className: 'Delivery' },
],
});
});
it('can add empty array to a relation', () => {
const parent = new ParseObject('Item');
parent.id = 'I1234';
const r = new ParseRelation(parent, 'shipments');
const o = new ParseObject('Delivery');
o.id = 'D1234';
const p = r.add(o);
expect(p).toBeTruthy();
expect(r.toJSON()).toEqual({
__type: 'Relation',
className: 'Delivery',
});
expect(parent.op('shipments').toJSON()).toEqual({
__op: 'AddRelation',
objects: [{ __type: 'Pointer', objectId: 'D1234', className: 'Delivery' }],
});
// Adding empty array shouldn't change the relation
r.add([]);
expect(r.toJSON()).toEqual({
__type: 'Relation',
className: 'Delivery',
});
expect(parent.op('shipments').toJSON()).toEqual({
__op: 'AddRelation',
objects: [{ __type: 'Pointer', objectId: 'D1234', className: 'Delivery' }],
});
});
it('cannot add to relation without parent', () => {
const relation = new ParseRelation();
expect(() => {
relation.add([]);
}).toThrow('Cannot add to a Relation without a parent');
});
it('cannot remove from relation without parent', () => {
const relation = new ParseRelation();
expect(() => {
relation.remove([]);
}).toThrow('Cannot remove from a Relation without a parent');
});
it('cannot construct query from relation without parent', () => {
const relation = new ParseRelation();
expect(() => {
relation.query();
}).toThrow('Cannot construct a query for a Relation without a parent');
});
it('can remove objects from a relation', () => {
const parent = new ParseObject('Item');
parent.id = 'I2';
const r = new ParseRelation(parent, 'shipments');
const o = new ParseObject('Delivery');
o.id = 'D1';
r.remove(o);
expect(r.toJSON()).toEqual({
__type: 'Relation',
className: 'Delivery',
});
expect(parent.op('shipments').toJSON()).toEqual({
__op: 'RemoveRelation',
objects: [{ __type: 'Pointer', objectId: 'D1', className: 'Delivery' }],
});
const o2 = new ParseObject('Delivery');
o2.id = 'D2';
const o3 = new ParseObject('Delivery');
o3.id = 'D3';
r.remove([o2, o3]);
expect(r.toJSON()).toEqual({
__type: 'Relation',
className: 'Delivery',
});
expect(parent.op('shipments').toJSON()).toEqual({
__op: 'RemoveRelation',
objects: [
{ __type: 'Pointer', objectId: 'D1', className: 'Delivery' },
{ __type: 'Pointer', objectId: 'D2', className: 'Delivery' },
{ __type: 'Pointer', objectId: 'D3', className: 'Delivery' },
],
});
});
it('can remove empty array from a relation', () => {
const parent = new ParseObject('Item');
parent.id = 'I5678';
const r = new ParseRelation(parent, 'shipments');
const o = new ParseObject('Delivery');
o.id = 'D5678';
r.remove(o);
expect(r.toJSON()).toEqual({
__type: 'Relation',
className: 'Delivery',
});
expect(parent.op('shipments').toJSON()).toEqual({
__op: 'RemoveRelation',
objects: [{ __type: 'Pointer', objectId: 'D5678', className: 'Delivery' }],
});
// Removing empty array shouldn't change the relation
r.remove([]);
expect(r.toJSON()).toEqual({
__type: 'Relation',
className: 'Delivery',
});
expect(parent.op('shipments').toJSON()).toEqual({
__op: 'RemoveRelation',
objects: [{ __type: 'Pointer', objectId: 'D5678', className: 'Delivery' }],
});
});
it('can generate a query for relation objects', () => {
const parent = new ParseObject('Item');
parent.id = 'I1';
let r = new ParseRelation(parent, 'shipments');
let q = r.query();
expect(q.className).toBe('Item');
expect(q._extraOptions).toEqual({
redirectClassNameForKey: 'shipments',
});
expect(q.where).toEqual({
$relatedTo: {
object: {
__type: 'Pointer',
objectId: 'I1',
className: 'Item',
},
key: 'shipments',
},
});
r = new ParseRelation(parent, 'shipments');
const o = new ParseObject('Delivery');
o.id = 'D1';
r.add(o);
q = r.query();
expect(q.className).toBe('Delivery');
expect(q.where).toEqual({
$relatedTo: {
object: {
__type: 'Pointer',
className: 'Item',
objectId: 'I1',
},
key: 'shipments',
},
});
});
it('can ensure it relates to the correct parent and key', () => {
const parent = new ParseObject('Item');
parent.id = 'I3';
const r = new ParseRelation(parent, 'shipments');
expect(r._ensureParentAndKey.bind(r, new ParseObject('Item'), 'shipments')).toThrow(
'Internal Error. Relation retrieved from two different Objects.'
);
expect(() => {
r._ensureParentAndKey(new ParseObject('TestObject'), 'shipments');
}).toThrow('Internal Error. Relation retrieved from two different Objects.');
expect(r._ensureParentAndKey.bind(r, parent, 'partners')).toThrow(
'Internal Error. Relation retrieved from two different keys.'
);
expect(r._ensureParentAndKey.bind(r, parent, 'shipments')).not.toThrow();
const noParent = new ParseRelation(null, null);
noParent._ensureParentAndKey(parent);
expect(noParent.parent).toEqual(parent);
const noIdParent = new ParseObject('Item');
const newParent = new ParseObject('Item');
newParent.id = 'newId';
const hasParent = new ParseRelation(noIdParent);
hasParent._ensureParentAndKey(newParent);
expect(hasParent.parent).toEqual(newParent);
});
});