| author | hamidouk |
| Tue, 27 Dec 2011 10:06:05 +0100 | |
| branch | popcorn-port |
| changeset 530 | 3cc4a789dae7 |
| parent 452 | 35495f156c41 |
| permissions | -rw-r--r-- |
| 64 | 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 |
|
|
| 247 | 27 |
var resp = dt.get("/url&a=1", spy_callback); |
| 68 | 28 |
|
| 64 | 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"); |
|
| 75 | 37 |
|
| 247 | 38 |
deepEqual(dt._cache["/url"], response_array, "the response should be stored in the cache"); |
39 |
|
|
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 | 42 |
|
| 64 | 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 | 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 |
} |