unittests/tests/JSONSerializer.js
branchpopcorn-port
changeset 69 3f7a2e8a948f
parent 65 6a8cae20f190
child 75 f5a7299bd0ff
equal deleted inserted replaced
68:5469b2b9743f 69:3f7a2e8a948f
     1 function test_JSONSerializer() {
     1 function test_JSONSerializer() {
     2   module("JSON Serializer tests", 
     2   module("JSON Serializer tests", 
     3     { setup: function() {
     3     { setup: function() {      
     4       this.dt = new IriSP.DataLoader();
     4       this.dt = new IriSP.DataLoader();
     5       }
     5       }
     6     }
     6     }
     7     );    
     7     );    
     8     
     8     
    10       var arr = ["ab", {"de" : "fg"}, "lp"];
    10       var arr = ["ab", {"de" : "fg"}, "lp"];
    11       var serializer = new IriSP.JSONSerializer(this.dt);
    11       var serializer = new IriSP.JSONSerializer(this.dt);
    12 
    12 
    13       equal(serializer.serialize(arr), JSON.stringify(arr), "assert that the outputted json is correct");
    13       equal(serializer.serialize(arr), JSON.stringify(arr), "assert that the outputted json is correct");
    14     });
    14     });
       
    15     
       
    16     test("sync() - callback should get called", function() {
       
    17       this.xhr = this.sandbox.useFakeXMLHttpRequest();
       
    18       this.requests = [];
       
    19       this.xhr.onCreate = function (request) {
       
    20         this.requests.push(request);
       
    21       };
       
    22       
       
    23       var response_array = [{ media: 12, content: "Hey there" }];
       
    24       var response_string = JSON.stringify(response_array);
       
    25   
       
    26       var spy_callback = this.spy();
       
    27       var ser = new IriSP.JSONSerializer(this.dt, "/url");
       
    28       
       
    29       ser.sync(spy_callback);
       
    30       
       
    31       equals(this.xhr.requests.length, 1, "the mock ajax object should have received the request");
       
    32       this.xhr.requests[0].respond(200, { "Content-Type": "application/json" },
       
    33                              response_string);
       
    34         
       
    35       ok(spy_callback.calledOnce, "callback called");
       
    36       ok(spy_callback.calledWith(response_array), "callback called with correct value");
       
    37       deepEqual(ser._data, response_array, "the internal variable is initialized to the correct value");
       
    38     });
    15 
    39 
    16 };
    40 };