| author | hamidouk |
| Thu, 20 Oct 2011 15:50:04 +0200 | |
| branch | popcorn-port |
| changeset 103 | 2dfd89e91c3a |
| parent 75 | f5a7299bd0ff |
| child 147 | 955119f901b4 |
| permissions | -rw-r--r-- |
| 65 | 1 |
function test_JSONSerializer() { |
2 |
module("JSON Serializer tests", |
|
|
69
3f7a2e8a948f
added a .sync() method to the json serializer. Changed the tests accordingly.
hamidouk
parents:
65
diff
changeset
|
3 |
{ setup: function() { |
| 65 | 4 |
this.dt = new IriSP.DataLoader(); |
5 |
} |
|
6 |
} |
|
7 |
); |
|
8 |
|
|
9 |
test("should return the correct JSON", function() { |
|
10 |
var arr = ["ab", {"de" : "fg"}, "lp"]; |
|
11 |
var serializer = new IriSP.JSONSerializer(this.dt); |
|
12 |
||
13 |
equal(serializer.serialize(arr), JSON.stringify(arr), "assert that the outputted json is correct"); |
|
14 |
}); |
|
|
69
3f7a2e8a948f
added a .sync() method to the json serializer. Changed the tests accordingly.
hamidouk
parents:
65
diff
changeset
|
15 |
|
| 75 | 16 |
test("sync()", function() { |
|
69
3f7a2e8a948f
added a .sync() method to the json serializer. Changed the tests accordingly.
hamidouk
parents:
65
diff
changeset
|
17 |
this.xhr = this.sandbox.useFakeXMLHttpRequest(); |
|
3f7a2e8a948f
added a .sync() method to the json serializer. Changed the tests accordingly.
hamidouk
parents:
65
diff
changeset
|
18 |
this.requests = []; |
|
3f7a2e8a948f
added a .sync() method to the json serializer. Changed the tests accordingly.
hamidouk
parents:
65
diff
changeset
|
19 |
this.xhr.onCreate = function (request) { |
|
3f7a2e8a948f
added a .sync() method to the json serializer. Changed the tests accordingly.
hamidouk
parents:
65
diff
changeset
|
20 |
this.requests.push(request); |
|
3f7a2e8a948f
added a .sync() method to the json serializer. Changed the tests accordingly.
hamidouk
parents:
65
diff
changeset
|
21 |
}; |
|
3f7a2e8a948f
added a .sync() method to the json serializer. Changed the tests accordingly.
hamidouk
parents:
65
diff
changeset
|
22 |
|
|
103
2dfd89e91c3a
added a method to sort the json annotations. changed the tests accordingly.
hamidouk
parents:
75
diff
changeset
|
23 |
var response_array = { media: 12, content: "Hey there", |
|
2dfd89e91c3a
added a method to sort the json annotations. changed the tests accordingly.
hamidouk
parents:
75
diff
changeset
|
24 |
annotations: [{"begin": "32", "end" : 64}, {"begin": "08", "end" : 27},{"begin": "02", "end" : 61}] }; |
|
2dfd89e91c3a
added a method to sort the json annotations. changed the tests accordingly.
hamidouk
parents:
75
diff
changeset
|
25 |
|
|
2dfd89e91c3a
added a method to sort the json annotations. changed the tests accordingly.
hamidouk
parents:
75
diff
changeset
|
26 |
/* sorted array is our comparision array */ |
|
2dfd89e91c3a
added a method to sort the json annotations. changed the tests accordingly.
hamidouk
parents:
75
diff
changeset
|
27 |
var sorted_array = IriSP.jQuery.extend({}, response_array); |
|
2dfd89e91c3a
added a method to sort the json annotations. changed the tests accordingly.
hamidouk
parents:
75
diff
changeset
|
28 |
sorted_array.annotations.sort(function(a, b) |
|
2dfd89e91c3a
added a method to sort the json annotations. changed the tests accordingly.
hamidouk
parents:
75
diff
changeset
|
29 |
{ var a_begin = +a.begin; |
|
2dfd89e91c3a
added a method to sort the json annotations. changed the tests accordingly.
hamidouk
parents:
75
diff
changeset
|
30 |
var b_begin = +b.begin; |
|
2dfd89e91c3a
added a method to sort the json annotations. changed the tests accordingly.
hamidouk
parents:
75
diff
changeset
|
31 |
return a_begin - b_begin; |
|
2dfd89e91c3a
added a method to sort the json annotations. changed the tests accordingly.
hamidouk
parents:
75
diff
changeset
|
32 |
}); |
|
2dfd89e91c3a
added a method to sort the json annotations. changed the tests accordingly.
hamidouk
parents:
75
diff
changeset
|
33 |
|
|
69
3f7a2e8a948f
added a .sync() method to the json serializer. Changed the tests accordingly.
hamidouk
parents:
65
diff
changeset
|
34 |
var response_string = JSON.stringify(response_array); |
|
3f7a2e8a948f
added a .sync() method to the json serializer. Changed the tests accordingly.
hamidouk
parents:
65
diff
changeset
|
35 |
|
|
3f7a2e8a948f
added a .sync() method to the json serializer. Changed the tests accordingly.
hamidouk
parents:
65
diff
changeset
|
36 |
var spy_callback = this.spy(); |
|
3f7a2e8a948f
added a .sync() method to the json serializer. Changed the tests accordingly.
hamidouk
parents:
65
diff
changeset
|
37 |
var ser = new IriSP.JSONSerializer(this.dt, "/url"); |
|
3f7a2e8a948f
added a .sync() method to the json serializer. Changed the tests accordingly.
hamidouk
parents:
65
diff
changeset
|
38 |
|
|
3f7a2e8a948f
added a .sync() method to the json serializer. Changed the tests accordingly.
hamidouk
parents:
65
diff
changeset
|
39 |
ser.sync(spy_callback); |
|
3f7a2e8a948f
added a .sync() method to the json serializer. Changed the tests accordingly.
hamidouk
parents:
65
diff
changeset
|
40 |
|
|
3f7a2e8a948f
added a .sync() method to the json serializer. Changed the tests accordingly.
hamidouk
parents:
65
diff
changeset
|
41 |
equals(this.xhr.requests.length, 1, "the mock ajax object should have received the request"); |
|
3f7a2e8a948f
added a .sync() method to the json serializer. Changed the tests accordingly.
hamidouk
parents:
65
diff
changeset
|
42 |
this.xhr.requests[0].respond(200, { "Content-Type": "application/json" }, |
|
3f7a2e8a948f
added a .sync() method to the json serializer. Changed the tests accordingly.
hamidouk
parents:
65
diff
changeset
|
43 |
response_string); |
|
3f7a2e8a948f
added a .sync() method to the json serializer. Changed the tests accordingly.
hamidouk
parents:
65
diff
changeset
|
44 |
|
|
3f7a2e8a948f
added a .sync() method to the json serializer. Changed the tests accordingly.
hamidouk
parents:
65
diff
changeset
|
45 |
ok(spy_callback.calledOnce, "callback called"); |
|
3f7a2e8a948f
added a .sync() method to the json serializer. Changed the tests accordingly.
hamidouk
parents:
65
diff
changeset
|
46 |
ok(spy_callback.calledWith(response_array), "callback called with correct value"); |
|
3f7a2e8a948f
added a .sync() method to the json serializer. Changed the tests accordingly.
hamidouk
parents:
65
diff
changeset
|
47 |
deepEqual(ser._data, response_array, "the internal variable is initialized to the correct value"); |
|
103
2dfd89e91c3a
added a method to sort the json annotations. changed the tests accordingly.
hamidouk
parents:
75
diff
changeset
|
48 |
|
|
2dfd89e91c3a
added a method to sort the json annotations. changed the tests accordingly.
hamidouk
parents:
75
diff
changeset
|
49 |
var order_preserved = true; |
|
2dfd89e91c3a
added a method to sort the json annotations. changed the tests accordingly.
hamidouk
parents:
75
diff
changeset
|
50 |
|
|
2dfd89e91c3a
added a method to sort the json annotations. changed the tests accordingly.
hamidouk
parents:
75
diff
changeset
|
51 |
var i = 0; |
|
2dfd89e91c3a
added a method to sort the json annotations. changed the tests accordingly.
hamidouk
parents:
75
diff
changeset
|
52 |
for(i = 0; i < ser._data.length - 1; i++) { |
|
2dfd89e91c3a
added a method to sort the json annotations. changed the tests accordingly.
hamidouk
parents:
75
diff
changeset
|
53 |
if (ser._data.annotations[i].begin > ser._data.annotations[i+1].begin) { |
|
2dfd89e91c3a
added a method to sort the json annotations. changed the tests accordingly.
hamidouk
parents:
75
diff
changeset
|
54 |
order_preserved = false; |
|
2dfd89e91c3a
added a method to sort the json annotations. changed the tests accordingly.
hamidouk
parents:
75
diff
changeset
|
55 |
break; |
|
2dfd89e91c3a
added a method to sort the json annotations. changed the tests accordingly.
hamidouk
parents:
75
diff
changeset
|
56 |
} |
|
2dfd89e91c3a
added a method to sort the json annotations. changed the tests accordingly.
hamidouk
parents:
75
diff
changeset
|
57 |
} |
|
2dfd89e91c3a
added a method to sort the json annotations. changed the tests accordingly.
hamidouk
parents:
75
diff
changeset
|
58 |
|
|
2dfd89e91c3a
added a method to sort the json annotations. changed the tests accordingly.
hamidouk
parents:
75
diff
changeset
|
59 |
ok(order_preserved, "the annotation sub-array is sorted by begin time"); |
|
69
3f7a2e8a948f
added a .sync() method to the json serializer. Changed the tests accordingly.
hamidouk
parents:
65
diff
changeset
|
60 |
}); |
| 65 | 61 |
|
| 75 | 62 |
test("currentMedia should return the current media", function() { |
63 |
var ser = new IriSP.JSONSerializer(this.dt, "/url"); |
|
64 |
/* FIXME: actually get something instead of monkey-patching the struct */ |
|
65 |
ser._data = {} |
|
66 |
ser._data.medias = [0]; |
|
67 |
equal(ser.currentMedia(), 0, "currentMedia() returns the correct value"); |
|
|
103
2dfd89e91c3a
added a method to sort the json annotations. changed the tests accordingly.
hamidouk
parents:
75
diff
changeset
|
68 |
}); |
| 65 | 69 |
}; |