unittests/tests/dataloader.js
author hamidouk
Thu, 13 Oct 2011 17:13:44 +0200
branchpopcorn-port
changeset 75 f5a7299bd0ff
parent 68 5469b2b9743f
child 247 69bc26f879e6
permissions -rw-r--r--
changes to the unit tests.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
64
b13359f9ce48 unit tests for the dataloader.
hamidouk
parents:
diff changeset
     1
function test_dataloader() {
b13359f9ce48 unit tests for the dataloader.
hamidouk
parents:
diff changeset
     2
  module("Dataloader", { setup: function() {                                     
b13359f9ce48 unit tests for the dataloader.
hamidouk
parents:
diff changeset
     3
                                      IriSP.jQuery = jQuery;
b13359f9ce48 unit tests for the dataloader.
hamidouk
parents:
diff changeset
     4
                                }
b13359f9ce48 unit tests for the dataloader.
hamidouk
parents:
diff changeset
     5
    });  
b13359f9ce48 unit tests for the dataloader.
hamidouk
parents:
diff changeset
     6
  
b13359f9ce48 unit tests for the dataloader.
hamidouk
parents:
diff changeset
     7
  test("should initialize dataloader", function() {
b13359f9ce48 unit tests for the dataloader.
hamidouk
parents:
diff changeset
     8
    var dt = new IriSP.DataLoader();    
b13359f9ce48 unit tests for the dataloader.
hamidouk
parents:
diff changeset
     9
    deepEqual(dt._cache, {}, "_cache empty");
b13359f9ce48 unit tests for the dataloader.
hamidouk
parents:
diff changeset
    10
  });
b13359f9ce48 unit tests for the dataloader.
hamidouk
parents:
diff changeset
    11
               
b13359f9ce48 unit tests for the dataloader.
hamidouk
parents:
diff changeset
    12
  test("should get an outside ressource", function() {
b13359f9ce48 unit tests for the dataloader.
hamidouk
parents:
diff changeset
    13
   
b13359f9ce48 unit tests for the dataloader.
hamidouk
parents:
diff changeset
    14
    var response_array = [{ id: 12, text: "Hey there" }];
b13359f9ce48 unit tests for the dataloader.
hamidouk
parents:
diff changeset
    15
    var response_string = JSON.stringify(response_array);
b13359f9ce48 unit tests for the dataloader.
hamidouk
parents:
diff changeset
    16
    
b13359f9ce48 unit tests for the dataloader.
hamidouk
parents:
diff changeset
    17
    var xhr = this.sandbox.useFakeXMLHttpRequest();
b13359f9ce48 unit tests for the dataloader.
hamidouk
parents:
diff changeset
    18
    var requests = this.requests = [];
b13359f9ce48 unit tests for the dataloader.
hamidouk
parents:
diff changeset
    19
    
b13359f9ce48 unit tests for the dataloader.
hamidouk
parents:
diff changeset
    20
    xhr.onCreate = function (request) {
b13359f9ce48 unit tests for the dataloader.
hamidouk
parents:
diff changeset
    21
        requests.push(request);
b13359f9ce48 unit tests for the dataloader.
hamidouk
parents:
diff changeset
    22
    };
b13359f9ce48 unit tests for the dataloader.
hamidouk
parents:
diff changeset
    23
b13359f9ce48 unit tests for the dataloader.
hamidouk
parents:
diff changeset
    24
    var spy_callback = this.spy();
b13359f9ce48 unit tests for the dataloader.
hamidouk
parents:
diff changeset
    25
    var dt = new IriSP.DataLoader();
b13359f9ce48 unit tests for the dataloader.
hamidouk
parents:
diff changeset
    26
    
b13359f9ce48 unit tests for the dataloader.
hamidouk
parents:
diff changeset
    27
    var resp = dt.get("/url", spy_callback);
68
5469b2b9743f fixed an error in dataloader.js test.
hamidouk
parents: 64
diff changeset
    28
64
b13359f9ce48 unit tests for the dataloader.
hamidouk
parents:
diff changeset
    29
    equals(xhr.requests.length, 1, "the mock ajax object should have received the request");
b13359f9ce48 unit tests for the dataloader.
hamidouk
parents:
diff changeset
    30
    
b13359f9ce48 unit tests for the dataloader.
hamidouk
parents:
diff changeset
    31
    xhr.requests[0].respond(200, { "Content-Type": "application/json" },
b13359f9ce48 unit tests for the dataloader.
hamidouk
parents:
diff changeset
    32
                             response_string);
b13359f9ce48 unit tests for the dataloader.
hamidouk
parents:
diff changeset
    33
    
b13359f9ce48 unit tests for the dataloader.
hamidouk
parents:
diff changeset
    34
    
b13359f9ce48 unit tests for the dataloader.
hamidouk
parents:
diff changeset
    35
    ok(spy_callback.calledOnce, "callback called");
b13359f9ce48 unit tests for the dataloader.
hamidouk
parents:
diff changeset
    36
    ok(spy_callback.calledWith(response_array), "callback called with correct string");
75
f5a7299bd0ff changes to the unit tests.
hamidouk
parents: 68
diff changeset
    37
    
f5a7299bd0ff changes to the unit tests.
hamidouk
parents: 68
diff changeset
    38
    // FIXME : remove or activate this test.
f5a7299bd0ff changes to the unit tests.
hamidouk
parents: 68
diff changeset
    39
    //deepEqual(dt._cache["/url"], response_array, "the response should be stored in the cache");
64
b13359f9ce48 unit tests for the dataloader.
hamidouk
parents:
diff changeset
    40
  });
b13359f9ce48 unit tests for the dataloader.
hamidouk
parents:
diff changeset
    41
    
b13359f9ce48 unit tests for the dataloader.
hamidouk
parents:
diff changeset
    42
}