| author | hamidouk |
| Mon, 21 Nov 2011 14:14:37 +0100 | |
| branch | popcorn-port |
| changeset 291 | e942a49240f4 |
| parent 247 | 69bc26f879e6 |
| child 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 |
}); |
44 |
|
|
45 |
} |