unittests/tests/dataloader.js
author hamidouk
Tue, 29 Nov 2011 11:09:08 +0100
branchpopcorn-port
changeset 345 8a088f7daa66
parent 291 e942a49240f4
child 452 35495f156c41
permissions -rw-r--r--
rollover over the interface buttons now works as expected. Also changed the width of the buttons to the correct size. Resized the width and height of the sprites to be the same as the boxes we display them in.
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
    
247
69bc26f879e6 activated url caching.
hamidouk
parents: 75
diff changeset
    27
    var resp = dt.get("/url&a=1", 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
    
247
69bc26f879e6 activated url caching.
hamidouk
parents: 75
diff changeset
    38
    deepEqual(dt._cache["/url"], response_array, "the response should be stored in the cache");
69bc26f879e6 activated url caching.
hamidouk
parents: 75
diff changeset
    39
    
69bc26f879e6 activated url caching.
hamidouk
parents: 75
diff changeset
    40
    var resp2 = dt.get("/url&a=2", spy_callback);
291
e942a49240f4 caching for asynchronous request now works correctly.
hamidouk
parents: 247
diff changeset
    41
    ok(spy_callback.calledTwice && xhr.requests.length === 1, "callback called twice but request made only once.");
247
69bc26f879e6 activated url caching.
hamidouk
parents: 75
diff changeset
    42
    
64
b13359f9ce48 unit tests for the dataloader.
hamidouk
parents:
diff changeset
    43
  });
b13359f9ce48 unit tests for the dataloader.
hamidouk
parents:
diff changeset
    44
    
b13359f9ce48 unit tests for the dataloader.
hamidouk
parents:
diff changeset
    45
}