unit tests for the dataloader. popcorn-port
authorhamidouk
Tue, 11 Oct 2011 17:15:37 +0200
branchpopcorn-port
changeset 64 b13359f9ce48
parent 63 acf10cf0ebd1
child 65 6a8cae20f190
unit tests for the dataloader.
unittests/tests/dataloader.js
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/unittests/tests/dataloader.js	Tue Oct 11 17:15:37 2011 +0200
@@ -0,0 +1,40 @@
+function test_dataloader() {
+  module("Dataloader", { setup: function() {                                     
+                                      IriSP.jQuery = jQuery;
+                                }
+    });  
+  
+  test("should initialize dataloader", function() {
+    var dt = new IriSP.DataLoader();    
+    deepEqual(dt._cache, {}, "_cache empty");
+  });
+               
+  test("should get an outside ressource", function() {
+   
+    var response_array = [{ id: 12, text: "Hey there" }];
+    var response_string = JSON.stringify(response_array);
+    
+    var xhr = this.sandbox.useFakeXMLHttpRequest();
+    var requests = this.requests = [];
+    
+    xhr.onCreate = function (request) {
+        requests.push(request);
+    };
+
+    var spy_callback = this.spy();
+    var dt = new IriSP.DataLoader();
+    
+    var resp = dt.get("/url", spy_callback);
+    //IriSP.jQuery.get("/url", spy_callback);
+    equals(xhr.requests.length, 1, "the mock ajax object should have received the request");
+    
+    xhr.requests[0].respond(200, { "Content-Type": "application/json" },
+                             response_string);
+    
+    
+    ok(spy_callback.calledOnce, "callback called");
+    ok(spy_callback.calledWith(response_array), "callback called with correct string");
+    deepEqual(dt._cache["/url"], response_array, "the response should be stored in the cache");
+  });
+    
+}
\ No newline at end of file