forked from parse-community/Parse-SDK-JS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParseLiveQueryTest.js
259 lines (229 loc) · 8.06 KB
/
ParseLiveQueryTest.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
'use strict';
const assert = require('assert');
const Parse = require('../../node');
const sleep = require('./sleep');
describe('Parse LiveQuery', () => {
beforeEach(() => {
Parse.User.enableUnsafeCurrentUser();
});
it('can subscribe to query', async done => {
const object = new TestObject();
await object.save();
const installationId = await Parse.CoreManager.getInstallationController().currentInstallationId();
const query = new Parse.Query(TestObject);
query.equalTo('objectId', object.id);
const subscription = await query.subscribe();
subscription.on('update', (object, original, response) => {
assert.equal(object.get('foo'), 'bar');
assert.equal(response.installationId, installationId);
done();
});
object.set({ foo: 'bar' });
await object.save();
});
it('can subscribe to query with client', async done => {
const object = new TestObject();
await object.save();
const installationId = await Parse.CoreManager.getInstallationController().currentInstallationId();
const query = new Parse.Query(TestObject);
query.equalTo('objectId', object.id);
const client = await Parse.CoreManager.getLiveQueryController().getDefaultLiveQueryClient();
if (client.shouldOpen()) {
client.open();
}
const subscription = client.subscribe(query);
subscription.on('update', (object, original, response) => {
assert.equal(object.get('foo'), 'bar');
assert.equal(response.installationId, installationId);
done();
});
await subscription.subscribePromise;
object.set({ foo: 'bar' });
await object.save();
});
it('can subscribe to query with null connect fields', async done => {
const client = new Parse.LiveQueryClient({
applicationId: 'integration',
serverURL: 'ws://localhost:1337',
javascriptKey: null,
masterKey: null,
sessionToken: null,
installationId: null,
});
client.open();
const object = new TestObject();
await object.save();
const query = new Parse.Query(TestObject);
query.equalTo('objectId', object.id);
const subscription = await client.subscribe(query);
subscription.on('update', async object => {
assert.equal(object.get('foo'), 'bar');
client.close();
done();
});
await subscription.subscribePromise;
object.set({ foo: 'bar' });
await object.save();
});
it('can subscribe to multiple queries', async () => {
const objectA = new TestObject();
const objectB = new TestObject();
await Parse.Object.saveAll([objectA, objectB]);
const queryA = new Parse.Query(TestObject);
const queryB = new Parse.Query(TestObject);
queryA.equalTo('objectId', objectA.id);
queryB.equalTo('objectId', objectB.id);
const subscriptionA = await queryA.subscribe();
const subscriptionB = await queryB.subscribe();
let count = 0;
subscriptionA.on('update', object => {
count++;
assert.equal(object.get('foo'), 'bar');
});
subscriptionB.on('update', object => {
count++;
assert.equal(object.get('foo'), 'baz');
});
await objectA.save({ foo: 'bar' });
await objectB.save({ foo: 'baz' });
await sleep(1000);
assert.equal(count, 2);
});
it('can subscribe to multiple queries different class', async () => {
const objectA = new TestObject();
const objectB = new DiffObject();
await Parse.Object.saveAll([objectA, objectB]);
const queryA = new Parse.Query(TestObject);
const queryB = new Parse.Query(DiffObject);
queryA.equalTo('objectId', objectA.id);
queryB.equalTo('objectId', objectB.id);
const subscriptionA = await queryA.subscribe();
const subscriptionB = await queryB.subscribe();
let count = 0;
subscriptionA.on('update', object => {
count++;
assert.equal(object.get('foo'), 'bar');
});
subscriptionB.on('update', object => {
count++;
assert.equal(object.get('foo'), 'baz');
});
await objectA.save({ foo: 'bar' });
await objectB.save({ foo: 'baz' });
await sleep(1000);
assert.equal(count, 2);
});
it('can unsubscribe to multiple queries different class', async () => {
const objectA = new TestObject();
const objectB = new DiffObject();
await Parse.Object.saveAll([objectA, objectB]);
const queryA = new Parse.Query(TestObject);
const queryB = new Parse.Query(DiffObject);
queryA.equalTo('objectId', objectA.id);
queryB.equalTo('objectId', objectB.id);
const subscriptionA = await queryA.subscribe();
const subscriptionB = await queryB.subscribe();
let count = 0;
subscriptionA.on('update', () => {
count++;
});
subscriptionB.on('update', object => {
count++;
assert.equal(object.get('foo'), 'baz');
});
subscriptionA.unsubscribe();
await objectA.save({ foo: 'bar' });
await objectB.save({ foo: 'baz' });
await sleep(1000);
assert.equal(count, 1);
});
it('can unsubscribe with await to multiple queries different class', async () => {
const objectA = new TestObject();
const objectB = new DiffObject();
await Parse.Object.saveAll([objectA, objectB]);
const queryA = new Parse.Query(TestObject);
const queryB = new Parse.Query(DiffObject);
queryA.equalTo('objectId', objectA.id);
queryB.equalTo('objectId', objectB.id);
const subscriptionA = await queryA.subscribe();
const subscriptionB = await queryB.subscribe();
let count = 0;
subscriptionA.on('update', () => {
count++;
});
subscriptionB.on('update', object => {
count++;
assert.equal(object.get('foo'), 'baz');
});
await subscriptionA.unsubscribe();
await objectA.save({ foo: 'bar' });
await objectB.save({ foo: 'baz' });
await sleep(1000);
assert.equal(count, 1);
});
it('can subscribe to ACL', async done => {
const user = await Parse.User.signUp('ooo', 'password');
const ACL = new Parse.ACL(user);
const object = new TestObject();
object.setACL(ACL);
await object.save();
const query = new Parse.Query(TestObject);
query.equalTo('objectId', object.id);
const subscription = await query.subscribe(user.getSessionToken());
subscription.on('update', async object => {
assert.equal(object.get('foo'), 'bar');
await Parse.User.logOut();
done();
});
await object.save({ foo: 'bar' });
});
it('can subscribe to null sessionToken', async done => {
const user = await Parse.User.signUp('oooooo', 'password');
const readOnly = Parse.User.readOnlyAttributes();
Parse.User.readOnlyAttributes = null;
user.set('sessionToken', null);
assert.equal(user.getSessionToken(), null);
const object = new TestObject();
await object.save();
const query = new Parse.Query(TestObject);
query.equalTo('objectId', object.id);
const subscription = await query.subscribe();
subscription.on('update', async object => {
assert.equal(object.get('foo'), 'bar');
Parse.User.readOnlyAttributes = function () {
return readOnly;
};
await Parse.User.logOut();
done();
});
await object.save({ foo: 'bar' });
});
it('can subscribe with open event', async done => {
const object = new TestObject();
await object.save();
const query = new Parse.Query(TestObject);
query.equalTo('objectId', object.id);
const subscription = await query.subscribe();
subscription.on('open', response => {
assert(response.clientId);
assert(response.installationId);
done();
});
});
it('can subscribe to query with fields', async done => {
const object = new TestObject();
await object.save({ name: 'hello', age: 21 });
const query = new Parse.Query(TestObject);
query.equalTo('objectId', object.id);
query.select(['name']);
const subscription = await query.subscribe();
subscription.on('update', object => {
assert.equal(object.get('name'), 'hello');
assert.equal(object.get('age'), undefined);
assert.equal(object.get('foo'), undefined);
done();
});
object.set({ foo: 'bar' });
await object.save();
});
});