unittests/tests/dataloader.js
branchpopcorn-port
changeset 64 b13359f9ce48
child 68 5469b2b9743f
equal deleted inserted replaced
63:acf10cf0ebd1 64:b13359f9ce48
       
     1 function test_dataloader() {
       
     2   module("Dataloader", { setup: function() {                                     
       
     3                                       IriSP.jQuery = jQuery;
       
     4                                 }
       
     5     });  
       
     6   
       
     7   test("should initialize dataloader", function() {
       
     8     var dt = new IriSP.DataLoader();    
       
     9     deepEqual(dt._cache, {}, "_cache empty");
       
    10   });
       
    11                
       
    12   test("should get an outside ressource", function() {
       
    13    
       
    14     var response_array = [{ id: 12, text: "Hey there" }];
       
    15     var response_string = JSON.stringify(response_array);
       
    16     
       
    17     var xhr = this.sandbox.useFakeXMLHttpRequest();
       
    18     var requests = this.requests = [];
       
    19     
       
    20     xhr.onCreate = function (request) {
       
    21         requests.push(request);
       
    22     };
       
    23 
       
    24     var spy_callback = this.spy();
       
    25     var dt = new IriSP.DataLoader();
       
    26     
       
    27     var resp = dt.get("/url", spy_callback);
       
    28     //IriSP.jQuery.get("/url", spy_callback);
       
    29     equals(xhr.requests.length, 1, "the mock ajax object should have received the request");
       
    30     
       
    31     xhr.requests[0].respond(200, { "Content-Type": "application/json" },
       
    32                              response_string);
       
    33     
       
    34     
       
    35     ok(spy_callback.calledOnce, "callback called");
       
    36     ok(spy_callback.calledWith(response_array), "callback called with correct string");
       
    37     deepEqual(dt._cache["/url"], response_array, "the response should be stored in the cache");
       
    38   });
       
    39     
       
    40 }