forked from balderdashy/sails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhook.pubsub.modelEvents.context.test.js
235 lines (202 loc) · 7.23 KB
/
hook.pubsub.modelEvents.context.test.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
/**
* Test dependencies
*/
var assert = require('assert');
var socketHelper = require('./helpers/socketHelper.js');
var appHelper = require('./helpers/appHelper');
var httpHelper = require('./helpers/httpHelper');
var util = require('util');
/**
* Errors
*/
var Err = {
badResponse: function(response) {
return 'Wrong server response! Response :::\n' + util.inspect(response);
}
};
describe('pubsub :: ', function() {
var sailsprocess;
var socket1;
var socket2;
var appName = 'testApp';
describe('Model events', function() {
before(function(done) {
this.timeout(5000);
appHelper.build(appName, function(err) {
if (err) {
throw new Error(err);
}
socketHelper.writeModelConfig();
appHelper.liftWithTwoSockets({
silly: false
}, function(err, sails, _socket1, _socket2) {
if (err) {
throw new Error(err);
}
sailsprocess = sails;
socket1 = _socket1;
socket2 = _socket2;
httpHelper.testRoute('get', 'user/create?name=joe', function(err) {
if (err) {return done(err);}
httpHelper.testRoute('get', 'user/create?name=abby', function(err) {
if (err) {return done(err);}
done();
});
});
});
});
});
after(function() {
if (sailsprocess) {
sailsprocess.kill();
}
// console.log('before `chdir ../`' + ', cwd was :: ' + process.cwd());
process.chdir('../');
// console.log('after `chdir ../`' + ', cwd was :: ' + process.cwd());
appHelper.teardown();
});
describe('when a model no default autosubscribe contexts ', function() {
after(function(done) {
socket1.disconnect();
socket2.disconnect();
done();
});
afterEach(function(done) {
socket1.removeAllListeners();
socket2.removeAllListeners();
done();
});
it('updating an instance via put should not result in any socket messages being received', function(done) {
this.slow(3000);
socket2.on('user', function(message) {
assert(false, 'User event received by socket 2 when it should not have been!');
});
socket1.put('/user/1', {
name: 'scott'
});
setTimeout(done, 1000);
});
describe('after subscribing to the update context', function() {
before(function(done) {
socket2.get('/user/subscribe?id=1&context=update', done);
});
it('updating an instance via put should result in the correct socket messages being received', function(done) {
var TIME_TO_WAIT = 1500;
var timer = setTimeout(function() {
done(new Error('`update` event was not fired in a timely manner (probably was never going to happen.) Waited ' + TIME_TO_WAIT + 'ms.'));
}, TIME_TO_WAIT);
socket2.on('user', function(message) {
clearTimeout(timer);
assert(message.id == 1 && message.verb == 'updated' && message.data.name === 'bob', Err.badResponse(message));
done();
});
socket1.put('/user/1', {
name: 'bob'
});
});
});
it('sending a message should not result in any socket messages being received', function(done) {
this.slow(3000);
socket2.on('user', function(message) {
assert(false, 'User event received by socket 2 when it should not have been!');
});
socket1.get('/user/message');
setTimeout(done, 1000);
});
describe('after subscribing to the message context', function() {
before(function(done) {
socket2.get('/user/subscribe?id=1&context=message', function() {
done();
});
});
it('sending a message should result in a correct socket messages being received', function(done) {
socket2.on('user', function(message) {
assert(message.id == 1 && message.verb == 'messaged' && message.data.greeting == 'hello', Err.badResponse(message));
done();
})
socket1.get('/user/message');
})
});
it('adding a pet to the user should not result in any socket messages being received', function(done) {
this.slow(3000);
socket2.on('user', function(message) {
assert(false, 'User event received by socket 2 when it should not have been!');
})
socket1.post('/pet', {
name: 'rex',
owner: 1
});
setTimeout(done, 1000);
});
describe('after subscribing to the add:pets context', function() {
before(function(done) {
socket2.get('/user/subscribe?id=1&context=add:pets', function() {
done();
});
});
it('adding a pet should result in a correct socket messages being received', function(done) {
socket2.on('user', function(message) {
assert(message.id == 1 && message.verb == 'addedTo' && message.attribute == 'pets' && message.addedId == 2, Err.badResponse(message));
done();
})
socket1.post('/pet', {
name: 'alice',
owner: 1
});
})
});
it('removing a pet from the user should not result in any socket messages being received', function(done) {
this.slow(3000);
socket2.on('user', function(message) {
assert(false, 'User event received by socket 2 when it should not have been!');
})
socket1.delete('/pet', {
id: 1
});
setTimeout(done, 1000);
});
describe('after subscribing to the remove:pets context', function() {
before(function(done) {
socket2.get('/user/subscribe?id=1&context=remove:pets', function() {
done();
});
});
it('removing a pet should result in a correct socket messages being received', function(done) {
socket2.on('user', function(message) {
assert(message.id == 1 && message.verb == 'removedFrom' && message.attribute == 'pets' && message.removedId == 2, Err.badResponse(message));
done();
})
socket1.delete('/pet', {
id: 2
});
})
});
it('deleting a user should not result in any socket messages being received', function(done) {
this.slow(3000);
socket2.on('user', function(message) {
assert(false, 'User event received by socket 2 when it should not have been!');
})
socket1.delete('/user', {
id: 2
});
setTimeout(done, 1000);
});
describe('after subscribing to the destroy context', function() {
before(function(done) {
socket2.get('/user/subscribe?id=1&context=destroy', function() {
done();
});
});
it('deleting a user should result in a correct socket messages being received', function(done) {
socket2.on('user', function(message) {
assert(message.id == 1 && message.verb == 'destroyed', Err.badResponse(message));
done();
})
socket1.delete('/user', {
id: 1
});
})
});
});
});
});