unittests/tests/dataloader.js
author hamidouk
Mon, 19 Dec 2011 15:25:22 +0100
branchpopcorn-port
changeset 481 a46cfeee6d77
parent 452 35495f156c41
permissions -rw-r--r--
using jquery ui draggable changes the state of an element from absolute to relative positioning, which breaks the way our seek button expands itself, so we need to force absolute positioning, quite uglily, using jquery.
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
  });
452
35495f156c41 automatically determine if we should use JSONP or something else.
hamidouk
parents: 291
diff changeset
    44
 
35495f156c41 automatically determine if we should use JSONP or something else.
hamidouk
parents: 291
diff changeset
    45
  test("should default to JSONP for foreign domains", function() {
35495f156c41 automatically determine if we should use JSONP or something else.
hamidouk
parents: 291
diff changeset
    46
    /* we can't simulate jsonp so we just verify that the function is called */
35495f156c41 automatically determine if we should use JSONP or something else.
hamidouk
parents: 291
diff changeset
    47
    var stub = this.stub(IriSP.jQuery, "ajax");
35495f156c41 automatically determine if we should use JSONP or something else.
hamidouk
parents: 291
diff changeset
    48
    var dt = new IriSP.DataLoader();
64
b13359f9ce48 unit tests for the dataloader.
hamidouk
parents:
diff changeset
    49
    
452
35495f156c41 automatically determine if we should use JSONP or something else.
hamidouk
parents: 291
diff changeset
    50
    var resp = dt.get("http://example.com/url&a=1", stub);
35495f156c41 automatically determine if we should use JSONP or something else.
hamidouk
parents: 291
diff changeset
    51
35495f156c41 automatically determine if we should use JSONP or something else.
hamidouk
parents: 291
diff changeset
    52
    ok(stub.calledOnce, "ajax request actually made");
35495f156c41 automatically determine if we should use JSONP or something else.
hamidouk
parents: 291
diff changeset
    53
  });
35495f156c41 automatically determine if we should use JSONP or something else.
hamidouk
parents: 291
diff changeset
    54
   
35495f156c41 automatically determine if we should use JSONP or something else.
hamidouk
parents: 291
diff changeset
    55
}