Skip to content

Commit 44015c3

Browse files
dblythydplewis
andauthored
Before Connect + Before Subscribe help required (parse-community#6793)
* Before Connect + Before Subscribe #1 * Cleanup and Documentation * Add E2E tests * Bump parse to 2.15.0 Co-authored-by: Diamond Lewis <[email protected]>
1 parent 93a88c5 commit 44015c3

File tree

8 files changed

+527
-227
lines changed

8 files changed

+527
-227
lines changed

package-lock.json

Lines changed: 15 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
"lru-cache": "5.1.1",
4747
"mime": "2.4.6",
4848
"mongodb": "3.5.9",
49-
"parse": "2.14.0",
49+
"parse": "2.15.0",
5050
"pg-promise": "10.5.7",
5151
"pluralize": "8.0.0",
5252
"redis": "3.0.2",
@@ -104,6 +104,7 @@
104104
"posttest": "cross-env MONGODB_VERSION=${MONGODB_VERSION:=4.0.4} MONGODB_TOPOLOGY=${MONGODB_TOPOLOGY:=standalone} MONGODB_STORAGE_ENGINE=${MONGODB_STORAGE_ENGINE:=mmapv1} mongodb-runner stop",
105105
"coverage": "cross-env MONGODB_VERSION=${MONGODB_VERSION:=4.0.4} MONGODB_TOPOLOGY=${MONGODB_TOPOLOGY:=standalone} MONGODB_STORAGE_ENGINE=${MONGODB_STORAGE_ENGINE:=mmapv1} TESTING=1 nyc jasmine",
106106
"start": "node ./bin/parse-server",
107+
"prettier": "prettier --write {src,spec}/**/*.js",
107108
"prepare": "npm run build",
108109
"postinstall": "node -p 'require(\"./postinstall.js\")()'"
109110
},

spec/ParseLiveQuery.spec.js

Lines changed: 93 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
describe('ParseLiveQuery', function() {
3+
describe('ParseLiveQuery', function () {
44
it('can subscribe to query', async done => {
55
await reconfigureServer({
66
liveQuery: {
@@ -24,6 +24,97 @@ describe('ParseLiveQuery', function() {
2424
await object.save();
2525
});
2626

27+
it('can handle beforeConnect / beforeSubscribe hooks', async done => {
28+
await reconfigureServer({
29+
liveQuery: {
30+
classNames: ['TestObject'],
31+
},
32+
startLiveQueryServer: true,
33+
verbose: false,
34+
silent: true,
35+
});
36+
const object = new TestObject();
37+
await object.save();
38+
39+
Parse.Cloud.beforeSubscribe('TestObject', req => {
40+
expect(req.op).toBe('subscribe');
41+
expect(req.requestId).toBe(1);
42+
expect(req.query).toBeDefined();
43+
expect(req.user).toBeUndefined();
44+
});
45+
46+
Parse.Cloud.beforeConnect(req => {
47+
expect(req.event).toBe('connect');
48+
expect(req.clients).toBe(0);
49+
expect(req.subscriptions).toBe(0);
50+
expect(req.useMasterKey).toBe(false);
51+
expect(req.installationId).toBeDefined();
52+
expect(req.user).toBeUndefined();
53+
expect(req.sessionToken).toBeUndefined();
54+
expect(req.client).toBeDefined();
55+
});
56+
const query = new Parse.Query(TestObject);
57+
query.equalTo('objectId', object.id);
58+
const subscription = await query.subscribe();
59+
subscription.on('update', async object => {
60+
expect(object.get('foo')).toBe('bar');
61+
done();
62+
});
63+
object.set({ foo: 'bar' });
64+
await object.save();
65+
});
66+
67+
it('can handle beforeConnect error', async done => {
68+
await reconfigureServer({
69+
liveQuery: {
70+
classNames: ['TestObject'],
71+
},
72+
startLiveQueryServer: true,
73+
verbose: false,
74+
silent: true,
75+
});
76+
const object = new TestObject();
77+
await object.save();
78+
79+
Parse.Cloud.beforeConnect(() => {
80+
throw new Error('You shall not pass!');
81+
});
82+
Parse.LiveQuery.on('error', error => {
83+
expect(error).toBe('You shall not pass!');
84+
done();
85+
});
86+
const query = new Parse.Query(TestObject);
87+
query.equalTo('objectId', object.id);
88+
await query.subscribe();
89+
});
90+
91+
it('can handle beforeSubscribe error', async done => {
92+
await reconfigureServer({
93+
liveQuery: {
94+
classNames: ['TestObject'],
95+
},
96+
startLiveQueryServer: true,
97+
verbose: false,
98+
silent: true,
99+
});
100+
const object = new TestObject();
101+
await object.save();
102+
103+
Parse.Cloud.beforeSubscribe(TestObject, () => {
104+
throw new Error('You shall not subscribe!');
105+
});
106+
Parse.LiveQuery.on('error', error => {
107+
expect(error).toBe('You shall not subscribe!');
108+
});
109+
const query = new Parse.Query(TestObject);
110+
query.equalTo('objectId', object.id);
111+
const subscription = await query.subscribe();
112+
subscription.on('error', error => {
113+
expect(error).toBe('You shall not subscribe!');
114+
done();
115+
});
116+
});
117+
27118
it('handle invalid websocket payload length', async done => {
28119
await reconfigureServer({
29120
liveQuery: {
@@ -61,7 +152,7 @@ describe('ParseLiveQuery', function() {
61152
}, 1000);
62153
});
63154

64-
afterEach(async function(done) {
155+
afterEach(async function (done) {
65156
const client = await Parse.CoreManager.getLiveQueryController().getDefaultLiveQueryClient();
66157
client.close();
67158
// Wait for live query client to disconnect

0 commit comments

Comments
 (0)