it ('should get a jQuery selector from the body', function (done) { var title = bulk.string (); var paragraph = bulk.string (); scipio.fromBody ('<html><body><h1>'+title+'</h1><p id="paragraph">'+paragraph+'</p></body></html>', function ($, body) { ($ !== null).should.be.true; body.should.equal ('<html><body><h1>'+title+'</h1><p id="paragraph">'+paragraph+'</p></body></html>'); $ ('h1').text ().should.equal (title); $ ('#paragraph').text ().should.equal (paragraph); done (); }); });
it ('should set the headers, property by property', function () { var foo = bulk.string (); scipio.setHeaders ('foo', foo); var bar = bulk.string (); scipio.setHeaders ('bar', bar); var baz = bulk.string (); scipio.setHeaders ('baz', baz); scipio.headers.should.containEql ({'foo': foo}); scipio.headers.should.containEql ({'bar': bar}); scipio.headers.should.containEql ({'baz': baz}); });
it ('should call the right methods to push data', function (done) { var route = thebulk.string (); var data = thebulk.obj (); data._route = route; emberbase.io = { sockets: { in: function (route_) { (route_ === route+'/VALUE' || route_ === route+'/CHILED_ADDED').should.be.true; return { emit: function (event, value) { (event === 'value_event' || event === 'child_added_event').should.be.true; if (event === 'child_added_event') value.should.equal (data); done (); } } } } }; emberbase.addRoute (route); socket.get = function (key, callback) { key.should.equal ('route'); callback (null, route); }; socket.emit = function (event) { event.should.not.eql ('UNKNOWN_ROUTE_ERR'); }; emberbase._onSetEvent (socket, data); });
it ('should call the right methods to set data', function (done) { var route = thebulk.string (); var data = thebulk.obj (); data._route = route; emberbase.io = { sockets: { in: function (route_) { route_.should.equal (route+'/VALUE'); return { emit: function (event, value) { event.should.equal ('value_event'); value.should.equal (data); done (); } } } } }; emberbase.addRoute (route); socket.get = function (key, callback) { key.should.equal ('route'); callback (null, route); }; socket.emit = function (event) { event.should.not.eql ('UNKNOWN_ROUTE_ERR'); }; emberbase._onSetEvent (socket, data); });
describe ('Scipio._parser ()', function () { var title = bulk.string (); var paragraph = bulk.string (); var body = '<html><body><h1>'+title+'</h1><p id="paragraph">'+paragraph+'</p></body></html>'; var data = scipio._parser (null, {statusCode: 200}, body); var $ = data[0], body_ = data[1], response_ = data[2]; it ('should return the body (default behavior)', function () { body_.should.equal (body); }); it ('should return a $ selector (default behavior)', function () { $ ('h1').text ().should.equal (title); $ ('#paragraph').text ().should.equal (paragraph); }); it ('should return a response object (default behavior)', function () { response_.should.have.property ('statusCode', 200); }); });
it ('should emit a error message when the route does not exist on the database', function (done) { var data = {route: thebulk.string ()+'456'}; socket.emit = function (msg) { msg.should.equal ('UNKNOWN_ROUTE_ERR'); done (); }; emberbase._onJoinRoute (socket, data); });
describe ('Scipio.use ()', function () { var error = bulk.string (); var body = bulk.string (); var response = bulk.string (); var thingy = bulk.string (); var stuff = bulk.string (); var shiny = bulk.string (); var muddy = bulk.string (); var fn = function (error_, response_, body_) { return [thingy, stuff, shiny, muddy]; }; var scipio = new Scipio (); scipio.use (fn); scipio.request = { get: function (url, callback) { callback (error, response, body); } }; it ('should return the values from the user defined parser, not the default one', function (done) { scipio.get ('http://example.org', function () { arguments[0].should.equal (thingy); arguments[1].should.equal (stuff); arguments[2].should.equal (shiny); arguments[3].should.equal (muddy); done (); }); }); });
it ('should get a jQuery selector from a URL', function (done) { var url = bulk.string (); var scipio = new Scipio (url); var title = bulk.string (); var paragraph = bulk.string (); var url = 'http://example.org'; scipio.request = { get: function (options, callback) { var body = '<html><body><h1>'+title+'</h1><p id="paragraph">'+paragraph+'</p></body></html>'; options.url.should.eql (url); callback (null, {statusCode: 200}, body); } }; scipio.fromUrl (url, function ($) { $ ('h1').text ().should.equal (title); $ ('#paragraph').text ().should.equal (paragraph); done (); }); });
it ('should get the children', function (done) { var emberbase = new Emberbase (); var route = thebulk.string (); var commit = { data: thebulk.obj () }; emberbase.addRoute (route); emberbase._pushData (route, commit, function (child, value) { child.val.should.eql (commit.data); emberbase._getChildren (route, function (data) { value.val[data.key].should.eql (data.val); }, emberbase); done (); }); });
it ('should emit an error while trying to save the route to the socket (no auth)', function (done) { var route = thebulk.string (); emberbase.addRoute (route); socket.set = function () { arguments[2].call (this, true); }; socket.join = function (room) {}; socket.emit = function (msg) { msg.should.equal ('UNKNOWN_ROUTE_ERR'); done (); }; var data = {_route: route}; emberbase._onJoinRoute (socket, data); });
it ('should get the value from the data', function (done) { var emberbase = new Emberbase (); var route = thebulk.string (); var commit = {data: thebulk.object (), path: ''}; emberbase.addRoute (route); emberbase._setData (route, commit, function (value) { emberbase._getValue (route, function (data) { value.should.eql (data); value.should.have.property ('val', commit.data); value.val.should.eql (commit.data); done (); }); }); });
it ('should call the right methods, join the right rooms and emit the right values', function (done) { var route = thebulk.string (); var data = thebulk.obj (); data._route = route; emberbase.addRoute (route); socket.join = function (room) { room.should.equal (route+'/CHILD_ADDED'); }; socket.emit = function (event, data_) { event.should.eql ('child_added_event'); done (); }; emberbase._onClientChildAdded (socket, data); });
it ('should call the right methods, join the right rooms and emit the right values', function (done) { var route = thebulk.string (); var data = thebulk.obj (); data._route = route; emberbase.addRoute (route); socket.get = function (key, callback) { key.should.equal ('route'); callback (null, route); }; socket.join = function (room) { room.should.equal (route+'/VALUE'); }; socket.emit = function (event, data_) { event.should.eql ('value_event'); done (); }; emberbase._onClientValue (socket, data); });
it ('should save the route to the socket, join the route room, and emit an ack (no error, no auth)', function (done) { var route = thebulk.string (); emberbase.addRoute (route); socket.set = function (key, value, callback) { key.should.equal ('route'); value.should.equal (route); (callback == undefined).should.be.false; callback.call (this); }; socket.join = function (room) { room.should.equal (route); }; socket.emit = function (msg) { msg.should.equal ('JOIN_ROUTE_ACK'); done (); }; var data = {route: route}; emberbase._onJoinRoute (socket, data); });
it ('should set the headers property as a whole', function () { var headers = {foo: bulk.string (), bar: bulk.string (), baz: bulk.string ()}; scipio.setHeaders (headers); scipio.should.have.property ('headers', headers); });
it ('should add a route to the database', function () { var route = thebulk.string (); emberbase.addRoute (route); emberbase.routes.should.include (route); });