-
-
Notifications
You must be signed in to change notification settings - Fork 595
/
Copy pathDirtyTest.js
233 lines (216 loc) · 6.25 KB
/
DirtyTest.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
'use strict';
const assert = require('assert');
const Parse = require('../../node');
describe('Dirty Objects', () => {
it('tracks dirty arrays', done => {
const array = [1];
const object = new TestObject();
object.set('scores', array);
assert.equal(object.get('scores').length, 1);
object
.save()
.then(() => {
array.push(2);
assert.equal(object.get('scores').length, 2);
return object.save();
})
.then(() => {
const query = new Parse.Query(TestObject);
return query.get(object.id);
})
.then(o => {
assert.equal(o.get('scores').length, 2);
done();
})
.catch(done.fail);
});
it('tracks dirty arrays after fetch', done => {
const object = new TestObject();
object.set('scores', [1]);
object
.save()
.then(() => {
const query = new Parse.Query(TestObject);
return query.get(object.id);
})
.then(o => {
const array = o.get('scores');
array.push(2);
return o.save();
})
.then(() => {
const query = new Parse.Query(TestObject);
return query.get(object.id);
})
.then(o => {
assert.equal(o.get('scores').length, 2);
done();
});
});
it('tracks dirty objects', done => {
const dict = { player1: 1 };
const object = new TestObject();
object.set('scoreMap', dict);
object
.save()
.then(() => {
dict.player2 = 2;
return object.save();
})
.then(() => {
const query = new Parse.Query(TestObject);
return query.get(object.id);
})
.then(o => {
assert.equal(Object.keys(o.get('scoreMap')).length, 2);
done();
});
});
it('tracks dirty objects after fetch', done => {
const dict = { player1: 1 };
const object = new TestObject();
object.set('scoreMap', dict);
object
.save()
.then(() => {
const query = new Parse.Query(TestObject);
return query.get(object.id);
})
.then(o => {
const dictAgain = o.get('scoreMap');
dictAgain.player2 = 2;
return o.save();
})
.then(() => {
const query = new Parse.Query(TestObject);
return query.get(object.id);
})
.then(o => {
assert.equal(Object.keys(o.get('scoreMap')).length, 2);
done();
});
});
it('tracks dirty geo points', done => {
const geo = new Parse.GeoPoint(5, 5);
const object = new TestObject();
object.set('location', geo);
object
.save()
.then(() => {
geo.latitude = 10;
return object.save();
})
.then(() => {
const query = new Parse.Query(TestObject);
return query.get(object.id);
})
.then(o => {
assert.equal(o.get('location').latitude, 10);
done();
});
});
it('tracks dirty geo points on fresh objects', done => {
const geo = new Parse.GeoPoint(1.0, 1.0);
const object = new TestObject();
object.set('location', geo);
geo.latitude = 2.0;
object
.save()
.then(() => {
const query = new Parse.Query(TestObject);
return query.get(object.id);
})
.then(o => {
assert.equal(o.get('location').latitude, 2);
done();
});
});
it('does not resave relations with dirty children', done => {
const parent = new Parent();
const child = new Child();
let newChild;
let parentAgain;
child.set('ghostbuster', 'peter');
parent.set('child', child);
parent
.save()
.then(() => {
assert.equal(parent.get('child'), child);
assert.equal(child.get('ghostbuster'), 'peter');
const query = new Parse.Query(Parent);
return query.get(parent.id);
})
.then(again => {
parentAgain = again;
newChild = new Child();
newChild.set('ghostbuster', 'ray');
parentAgain.set('child', newChild);
return parentAgain.save();
})
.then(() => {
assert.equal(parent.get('child'), child);
assert.equal(parentAgain.get('child'), newChild);
assert.equal(child.get('ghostbuster'), 'peter');
assert.equal(newChild.get('ghostbuster'), 'ray');
// Now parent's child is newChild. If we change the original
// child, it shouldn't affect parent.
child.set('ghostbuster', 'egon');
assert.equal(parent.get('child').get('ghostbuster'), 'egon');
return parent.save();
})
.then(() => {
assert.equal(parent.get('child'), child);
assert.equal(parentAgain.get('child'), newChild);
assert.equal(child.get('ghostbuster'), 'egon');
assert.equal(newChild.get('ghostbuster'), 'ray');
const query = new Parse.Query(Parent);
return query.get(parent.id);
})
.then(yetAgain => {
assert.equal(parent.get('child'), child);
assert.equal(parentAgain.get('child'), newChild);
assert.equal(yetAgain.get('child').id, newChild.id);
assert.equal(child.get('ghostbuster'), 'egon');
assert.equal(newChild.get('ghostbuster'), 'ray');
const newChildAgain = yetAgain.get('child');
assert.equal(newChildAgain.id, newChild.id);
return newChildAgain.fetch();
})
.then(c => {
assert.equal(c.get('ghostbuster'), 'ray');
done();
});
});
it('does not dirty two-way pointers on saveAll', done => {
const parent = new Parent();
const child = new Child();
child
.save()
.then(() => {
child.set('property', 'x');
parent.set('children', [child]);
child.set('parent', parent);
return Parse.Object.saveAll([parent, child]);
})
.then(results => {
assert.equal(results[0].dirty(), false);
assert.equal(results[1].dirty(), false);
done();
});
});
it('unset fields should not stay dirty', done => {
const object = new TestObject();
object
.save({ foo: 'bar' })
.then(() => {
assert.equal(object.dirty(), false);
object.unset('foo');
assert.equal(object.dirty(), true);
return object.save();
})
.then(() => {
assert.equal(object.dirty(), false);
done();
});
});
});