forked from parse-community/Parse-SDK-JS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParseSubclassTest.js
216 lines (193 loc) · 5.84 KB
/
ParseSubclassTest.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
'use strict';
const assert = require('assert');
const Parse = require('../../node');
describe('Parse Object Subclasses', () => {
it('uses subclasses when doing query find', done => {
const Subclass = Parse.Object.extend('Subclass', {
initialize(_attributes, _options, number) {
this.number = number || -1;
},
});
const object = new Subclass({}, {}, 57);
assert.equal(object.number, 57);
object
.save()
.then(() => {
const query = new Parse.Query(Subclass);
return query.find();
})
.then(results => {
assert.equal(results.length, 1);
assert(results[0] instanceof Subclass);
assert.equal(results[0].number, -1);
done();
});
});
it('uses subclasses when doing query get', done => {
const Subclass = Parse.Object.extend('Subclass', {
initialize(_attributes, _options, number) {
this.number = number || -1;
},
});
const object = new Subclass({}, {}, 57);
assert.equal(object.number, 57);
object
.save()
.then(() => {
const query = new Parse.Query(Subclass);
return query.get(object.id);
})
.then(result => {
assert(result instanceof Subclass);
assert.equal(result.number, -1);
done();
});
});
it('uses subclasses with array results', done => {
const Container = Parse.Object.extend('Container');
const Item = Parse.Object.extend('Item');
const ItemChild = Parse.Object.extend('Item');
const ItemGrandchild = ItemChild.extend();
const item = new Item();
item
.save({ foo: 'bar' })
.then(() => {
const container = new Container();
return container.save({ items: [item] });
})
.then(container => {
const query = new Parse.Query(Container);
return query.get(container.id);
})
.then(container => {
assert(container instanceof Container);
assert.equal(container.get('items').length, 1);
const item = container.get('items')[0];
assert(item instanceof Item);
assert(item instanceof ItemChild);
assert(item instanceof ItemGrandchild);
done();
});
});
it('can subclass multiple levels explicitly', done => {
const Parent = Parse.Object.extend('MyClass', {
initialize() {
Parent.__super__.initialize.apply(this, arguments);
this.parent = true;
},
});
const Child = Parent.extend({
initialize() {
Child.__super__.initialize.apply(this, arguments);
this.child = true;
},
});
const Grandchild = Child.extend({
initialize() {
Grandchild.__super__.initialize.apply(this, arguments);
this.grandchild = true;
},
});
const object = new Parent();
object
.save()
.then(() => {
const query = new Parse.Query(Grandchild);
return query.get(object.id);
})
.then(result => {
assert(result instanceof Parent);
assert(result instanceof Child);
assert(result instanceof Grandchild);
assert(result.parent);
assert(result.child);
assert(result.grandchild);
done();
});
});
it('can subclass multiple levels implicitly', done => {
const Parent = Parse.Object.extend('MyClass', {
initialize() {
Parent.__super__.initialize.apply(this, arguments);
this.parent = true;
},
});
const Child = Parse.Object.extend('MyClass', {
initialize() {
Child.__super__.initialize.apply(this, arguments);
this.child = true;
},
});
const Grandchild = Parse.Object.extend('MyClass', {
initialize() {
Grandchild.__super__.initialize.apply(this, arguments);
this.grandchild = true;
},
});
const object = new Parent();
object
.save()
.then(() => {
const query = new Parse.Query(Grandchild);
return query.get(object.id);
})
.then(result => {
assert(result instanceof Parent);
assert(result instanceof Child);
assert(result instanceof Grandchild);
assert(result.parent);
assert(result.child);
assert(result.grandchild);
done();
});
});
it('can subclass multiple levels explicitly with different names', done => {
const Parent = Parse.Object.extend('MyClass');
const Child = Parent.extend();
const Grandchild = Child.extend('NewClass');
const object = new Parent();
object
.save()
.then(() => {
const query = new Parse.Query(Child);
return query.get(object.id);
})
.then(result => {
assert(result instanceof Parent);
assert(result instanceof Child);
const query = new Parse.Query(Grandchild);
return query.get(object.id);
})
.then(null, () => {
// No object found
done();
});
});
it('propagates instance properties', () => {
const Squirtle = Parse.Object.extend('Squirtle', {
water: true,
});
const Wartortle = Squirtle.extend('Wartortle');
const wartortle = new Wartortle();
assert(wartortle.water);
});
it('registerSubclass with unknown className', async () => {
Parse.Object.unregisterSubclass('TestObject');
let outerClassName = '';
class TestObject extends Parse.Object {
constructor(className) {
super(className);
outerClassName = className;
}
}
Parse.Object.registerSubclass('TestObject', TestObject);
const o = new Parse.Object('TestObject');
await o.save();
const query = new Parse.Query('TestObject');
const first = await query.first();
expect(first instanceof TestObject).toBe(true);
expect(first.className).toBe('TestObject');
expect(outerClassName).toBe('TestObject');
Parse.Object.unregisterSubclass('TestObject');
});
});