-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Expand file tree
/
Copy pathParseLiveQueryQuery.spec.js
More file actions
287 lines (236 loc) · 7.99 KB
/
ParseLiveQueryQuery.spec.js
File metadata and controls
287 lines (236 loc) · 7.99 KB
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
283
284
285
286
287
'use strict';
describe('ParseLiveQuery query operation', function () {
beforeEach(() => {
Parse.CoreManager.getLiveQueryController().setDefaultLiveQueryClient(null);
});
afterEach(async () => {
const client = await Parse.CoreManager.getLiveQueryController().getDefaultLiveQueryClient();
if (client) {
await client.close();
}
});
it('can execute query on existing subscription and receive results', async () => {
await reconfigureServer({
liveQuery: {
classNames: ['TestObject'],
},
startLiveQueryServer: true,
verbose: false,
silent: true,
});
// Create test objects
const obj1 = new TestObject();
obj1.set('name', 'object1');
await obj1.save();
const obj2 = new TestObject();
obj2.set('name', 'object2');
await obj2.save();
// Subscribe to query
const query = new Parse.Query(TestObject);
const subscription = await query.subscribe();
// Wait for subscription to be ready
await new Promise(resolve => subscription.on('open', resolve));
// Set up result listener
const resultPromise = new Promise((resolve, reject) => {
const timeout = setTimeout(() => reject(new Error('Timeout waiting for result')), 5000);
subscription.on('result', results => {
clearTimeout(timeout);
resolve(results);
});
});
// Get the LiveQuery client and send query message
const client = await Parse.CoreManager.getLiveQueryController().getDefaultLiveQueryClient();
const message = {
op: 'query',
requestId: subscription.id,
};
client.socket.send(JSON.stringify(message));
// Wait for and verify results
const results = await resultPromise;
expect(Array.isArray(results)).toBe(true);
expect(results.length).toBe(2);
expect(results.some(r => r.name === 'object1')).toBe(true);
expect(results.some(r => r.name === 'object2')).toBe(true);
await subscription.unsubscribe();
});
it('respects field filtering (keys) when executing query', async () => {
await reconfigureServer({
liveQuery: {
classNames: ['TestObject'],
},
startLiveQueryServer: true,
verbose: false,
silent: true,
});
// Create test object with multiple fields
const obj = new TestObject();
obj.set('name', 'test');
obj.set('secret', 'confidential');
obj.set('public', 'visible');
await obj.save();
// Subscribe with field selection
const query = new Parse.Query(TestObject);
query.select('name', 'public'); // Only select these fields
const subscription = await query.subscribe();
// Wait for subscription to be ready
await new Promise(resolve => subscription.on('open', resolve));
// Set up result listener
const resultPromise = new Promise((resolve, reject) => {
const timeout = setTimeout(() => reject(new Error('Timeout')), 5000);
subscription.on('result', results => {
clearTimeout(timeout);
resolve(results);
});
});
// Send query message
const client = await Parse.CoreManager.getLiveQueryController().getDefaultLiveQueryClient();
const message = {
op: 'query',
requestId: subscription.id,
};
client.socket.send(JSON.stringify(message));
// Wait for and verify results
const results = await resultPromise;
expect(results.length).toBe(1);
const result = results[0];
expect(result.name).toBe('test');
expect(result.public).toBe('visible');
expect(result.secret).toBeUndefined(); // Should be filtered out
await subscription.unsubscribe();
});
it('runs beforeFind and afterFind triggers', async () => {
let beforeFindCalled = false;
let afterFindCalled = false;
Parse.Cloud.beforeFind('TestObject', () => {
beforeFindCalled = true;
});
Parse.Cloud.afterFind('TestObject', req => {
afterFindCalled = true;
return req.objects;
});
await reconfigureServer({
liveQuery: {
classNames: ['TestObject'],
},
startLiveQueryServer: true,
verbose: false,
silent: true,
});
// Create test object
const obj = new TestObject();
obj.set('name', 'test');
await obj.save();
// Subscribe
const query = new Parse.Query(TestObject);
const subscription = await query.subscribe();
// Wait for subscription to be ready
await new Promise(resolve => subscription.on('open', resolve));
// Set up result listener
const resultPromise = new Promise((resolve, reject) => {
const timeout = setTimeout(() => reject(new Error('Timeout')), 5000);
subscription.on('result', results => {
clearTimeout(timeout);
resolve(results);
});
});
// Send query message
const client = await Parse.CoreManager.getLiveQueryController().getDefaultLiveQueryClient();
const message = {
op: 'query',
requestId: subscription.id,
};
client.socket.send(JSON.stringify(message));
// Wait for results
await resultPromise;
// Verify triggers were called
expect(beforeFindCalled).toBe(true);
expect(afterFindCalled).toBe(true);
await subscription.unsubscribe();
});
it('handles query with where constraints', async () => {
await reconfigureServer({
liveQuery: {
classNames: ['TestObject'],
},
startLiveQueryServer: true,
verbose: false,
silent: true,
});
// Create multiple test objects
const obj1 = new TestObject();
obj1.set('name', 'apple');
await obj1.save();
const obj2 = new TestObject();
obj2.set('name', 'banana');
await obj2.save();
const obj3 = new TestObject();
obj3.set('name', 'cherry');
await obj3.save();
// Subscribe with where constraint
const query = new Parse.Query(TestObject);
query.equalTo('name', 'banana');
const subscription = await query.subscribe();
// Wait for subscription to be ready
await new Promise(resolve => subscription.on('open', resolve));
// Set up result listener
const resultPromise = new Promise((resolve, reject) => {
const timeout = setTimeout(() => reject(new Error('Timeout')), 5000);
subscription.on('result', results => {
clearTimeout(timeout);
resolve(results);
});
});
// Send query message
const client = await Parse.CoreManager.getLiveQueryController().getDefaultLiveQueryClient();
const message = {
op: 'query',
requestId: subscription.id,
};
client.socket.send(JSON.stringify(message));
// Wait for and verify results - should only get banana
const results = await resultPromise;
expect(results.length).toBe(1);
expect(results[0].name).toBe('banana');
await subscription.unsubscribe();
});
it('handles errors gracefully', async () => {
await reconfigureServer({
liveQuery: {
classNames: ['TestObject'],
},
startLiveQueryServer: true,
verbose: false,
silent: true,
});
// Create an object
const obj = new TestObject();
obj.set('name', 'test');
await obj.save();
// Subscribe
const query = new Parse.Query(TestObject);
const subscription = await query.subscribe();
await new Promise(resolve => subscription.on('open', resolve));
// Set up listeners for both result and error
let resultReceived = false;
let errorReceived = false;
subscription.on('result', () => {
resultReceived = true;
});
subscription.on('error', () => {
errorReceived = true;
});
// Send query message
const client = await Parse.CoreManager.getLiveQueryController().getDefaultLiveQueryClient();
const message = {
op: 'query',
requestId: subscription.id,
};
client.socket.send(JSON.stringify(message));
// Wait a bit for the response
await new Promise(resolve => setTimeout(resolve, 500));
// Should have received result (not error) since query is valid
expect(resultReceived).toBe(true);
expect(errorReceived).toBe(false);
await subscription.unsubscribe();
});
});