forked from balderdashy/sails
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
213 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,193 @@ | ||
/** | ||
* Test dependencies | ||
*/ | ||
var assert = require('assert'); | ||
var socketHelper = require('./helpers/socketHelper.js'); | ||
var appHelper = require('./helpers/appHelper'); | ||
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 (i.e. not the firehose)', function() { | ||
|
||
|
||
describe('when a model no default autosubscribe contexts ', function() { | ||
|
||
before(function(done) { | ||
this.timeout(5000); | ||
appHelper.build(appName, function(err) { | ||
if (err) {throw new Error(err);} | ||
socketHelper.writeModelConfig(); | ||
appHelper.liftWithTwoSockets({verbose: false}, function(err, sails, _socket1, _socket2) { | ||
if (err) {throw new Error(err);} | ||
sailsprocess = sails; | ||
socket1 = _socket1; | ||
socket2 = _socket2; | ||
|
||
socket2.get('/user/create?name=joe', function(){ | ||
socket2.get('/user/create?name=abby', function() { | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
after(function() { | ||
|
||
socket1.disconnect(); | ||
socket2.disconnect(); | ||
|
||
if (sailsprocess) { | ||
sailsprocess.kill(); | ||
} | ||
// console.log('before `chdir ../`' + ', cwd was :: ' + process.cwd()); | ||
process.chdir('../'); | ||
// console.log('after `chdir ../`' + ', cwd was :: ' + process.cwd()); | ||
appHelper.teardown(); | ||
}); | ||
|
||
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', function(){done();}); | ||
}); | ||
it('updating an instance via put should result in a correct socket messages being received', function(done) { | ||
socket2.on('user', function(message) { | ||
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}); | ||
}) | ||
}); | ||
}); | ||
|
||
}); | ||
}); |