describe("Transistor.Control", function () { var control, recorder; beforeEach(function () { recorder = (function() { var me = {}; me.callback = function(value) { me.called_with = value; }; return me; }()); window.JSONP = function() { return FAKE_JSONP; }; window.FAKE_JSONP = (function () { var me, get, setNextResponse, clearResponse; me = {}; called = function(url, data, callback) { me.last_call = { url: url, data: data, callback: callback } }; get = function (url, data, callback) { called(url, data, callback); callback(me.next_response); clearResponse(); }; setNextResponse = function (response) { me.next_response = response; } clearResponse = function () { me.next_response = undefined }; me.setNextResponse = setNextResponse; me.get = get; return me; }()); control = Transistor.Control("broadcaster_url", "station_uid", "token"); }); describe("set", function () { it("builds channel_url", function() { control.set("a/channel", [], function(){}); assertEqual("broadcaster_url/command/set/station_uid/token/a/channel", FAKE_JSONP.last_call.url); }); it("sends arguments", function() { control.set("a/channel", [3,0], function(){}); assertEqual({data: '{"collection":[3,0]}'}, FAKE_JSONP.last_call.data); }); it("calls callback with the result from FAKE_JSONP", function(){ FAKE_JSONP.setNextResponse({"test": 123}); control.set("a/channel", [3,0], recorder.callback); assertEqual({"test": 123}, recorder.called_with); }); }); describe("insert", function () { it("builds channel_url", function() { control.insert("a/channel", {}, function(){}); assertEqual("broadcaster_url/command/insert/station_uid/token/a/channel", FAKE_JSONP.last_call.url); }); it("sends arguments", function() { control.insert("a/channel", {a:2}, function(){}); assertEqual({data: '{"entry":{"a":2}}'}, FAKE_JSONP.last_call.data); }); it("calls callback with the result from FAKE_JSONP", function(){ FAKE_JSONP.setNextResponse({"test": 123}); control.insert("a/channel", {}, recorder.callback); assertEqual({"test": 123}, recorder.called_with); }); }); describe("update", function () { it("builds channel_url", function() { control.update("a/channel", 12, {}, function(){}); assertEqual("broadcaster_url/command/update/station_uid/token/a/channel", FAKE_JSONP.last_call.url); }); it("sends arguments", function() { control.update("a/channel", 12, {a:2}, function(){}); assertEqual({data: '{"id":12,"entry":{"a":2}}'}, FAKE_JSONP.last_call.data); }); it("calls callback with the result from FAKE_JSONP", function(){ FAKE_JSONP.setNextResponse({"test": 123}); control.update("a/channel", 12, {}, recorder.callback); assertEqual({"test": 123}, recorder.called_with); }); }); describe("remove", function () { it("builds channel_url", function() { control.remove("a/channel", 12, function(){}); assertEqual("broadcaster_url/command/remove/station_uid/token/a/channel", FAKE_JSONP.last_call.url); }); it("sends arguments", function() { control.remove("a/channel", 12, function(){}); assertEqual({data: '{"id":12}'}, FAKE_JSONP.last_call.data); }); it("calls callback with the result from FAKE_JSONP", function(){ FAKE_JSONP.setNextResponse({"test": 123}); control.remove("a/channel", 23, recorder.callback); assertEqual({"test": 123}, recorder.called_with); }); }); });