# HG changeset patch # User hamidouk # Date 1318420814 -7200 # Node ID 3f7a2e8a948f8bdc6f19878f9e990f7ee3a87bcf # Parent 5469b2b9743f1db30d44b01a5feafea8a5cdfc73 added a .sync() method to the json serializer. Changed the tests accordingly. diff -r 5469b2b9743f -r 3f7a2e8a948f src/js/data.js --- a/src/js/data.js Wed Oct 12 13:59:38 2011 +0200 +++ b/src/js/data.js Wed Oct 12 14:00:14 2011 +0200 @@ -42,6 +42,18 @@ return JSON.parse(data); }; +IriSP.JSONSerializer.prototype.sync = function(callback) { + /* we don't have to do much because jQuery handles json for us */ + var wrapper = function(obj) { + return function(data) { + obj._data = data; + callback(data); + } + }; + + this._DataLoader.get(this._url, wrapper(this)); +}; + IriSP.SerializerFactory = function(DataLoader) { this._dataloader = DataLoader; }; diff -r 5469b2b9743f -r 3f7a2e8a948f unittests/tests/JSONSerializer.js --- a/unittests/tests/JSONSerializer.js Wed Oct 12 13:59:38 2011 +0200 +++ b/unittests/tests/JSONSerializer.js Wed Oct 12 14:00:14 2011 +0200 @@ -1,6 +1,6 @@ function test_JSONSerializer() { module("JSON Serializer tests", - { setup: function() { + { setup: function() { this.dt = new IriSP.DataLoader(); } } @@ -12,5 +12,29 @@ equal(serializer.serialize(arr), JSON.stringify(arr), "assert that the outputted json is correct"); }); + + test("sync() - callback should get called", function() { + this.xhr = this.sandbox.useFakeXMLHttpRequest(); + this.requests = []; + this.xhr.onCreate = function (request) { + this.requests.push(request); + }; + + var response_array = [{ media: 12, content: "Hey there" }]; + var response_string = JSON.stringify(response_array); + + var spy_callback = this.spy(); + var ser = new IriSP.JSONSerializer(this.dt, "/url"); + + ser.sync(spy_callback); + + equals(this.xhr.requests.length, 1, "the mock ajax object should have received the request"); + this.xhr.requests[0].respond(200, { "Content-Type": "application/json" }, + response_string); + + ok(spy_callback.calledOnce, "callback called"); + ok(spy_callback.calledWith(response_array), "callback called with correct value"); + deepEqual(ser._data, response_array, "the internal variable is initialized to the correct value"); + }); }; \ No newline at end of file