|
1 $(document).ready(function() { |
|
2 |
|
3 module("ajax", { |
|
4 setup: function() { |
|
5 this.scriptsLength = $('head script[src]').length-1; |
|
6 this.cssLength = $('head link[rel=stylesheet]').length-1; |
|
7 // re-initialize cw loaded cache so that each tests run in a |
|
8 // clean environment, have a lookt at _loadAjaxHtmlHead implementation |
|
9 // in cubicweb.ajax.js for more information. |
|
10 cw.loaded_src = []; |
|
11 cw.loaded_href = []; |
|
12 }, |
|
13 teardown: function() { |
|
14 $('head script[src]:gt(' + this.scriptsLength + ')').remove(); |
|
15 $('head link[rel=stylesheet]:gt(' + this.cssLength + ')').remove(); |
|
16 } |
|
17 }); |
|
18 |
|
19 function jsSources() { |
|
20 return $.map($('head script[src]'), function(script) { |
|
21 return script.getAttribute('src'); |
|
22 }); |
|
23 } |
|
24 |
|
25 test('test simple h1 inclusion (ajax_url0.html)', function() { |
|
26 expect(3); |
|
27 equals(jQuery('#main').children().length, 0); |
|
28 stop(); |
|
29 jQuery('#main').loadxhtml('/../ajax_url0.html', { |
|
30 callback: function() { |
|
31 try { |
|
32 equals(jQuery('#main').children().length, 1); |
|
33 equals(jQuery('#main h1').html(), 'Hello'); |
|
34 } finally { |
|
35 start(); |
|
36 }; |
|
37 } |
|
38 }); |
|
39 }); |
|
40 |
|
41 test('test simple html head inclusion (ajax_url1.html)', function() { |
|
42 expect(6); |
|
43 var scriptsIncluded = jsSources(); |
|
44 equals(jQuery.inArray('http://foo.js', scriptsIncluded), - 1); |
|
45 stop(); |
|
46 jQuery('#main').loadxhtml('/../ajax_url1.html', { |
|
47 callback: function() { |
|
48 try { |
|
49 var origLength = scriptsIncluded.length; |
|
50 scriptsIncluded = jsSources(); |
|
51 // check that foo.js has been *appended* to <head> |
|
52 equals(scriptsIncluded.length, origLength + 1); |
|
53 equals(scriptsIncluded[origLength].indexOf('http://foo.js'), 0); |
|
54 // check that <div class="ajaxHtmlHead"> has been removed |
|
55 equals(jQuery('#main').children().length, 1); |
|
56 equals(jQuery('div.ajaxHtmlHead').length, 0); |
|
57 equals(jQuery('#main h1').html(), 'Hello'); |
|
58 } finally { |
|
59 start(); |
|
60 }; |
|
61 } |
|
62 }); |
|
63 }); |
|
64 |
|
65 test('test addCallback', function() { |
|
66 expect(3); |
|
67 equals(jQuery('#main').children().length, 0); |
|
68 stop(); |
|
69 var d = jQuery('#main').loadxhtml('/../ajax_url0.html'); |
|
70 d.addCallback(function() { |
|
71 try { |
|
72 equals(jQuery('#main').children().length, 1); |
|
73 equals(jQuery('#main h1').html(), 'Hello'); |
|
74 } finally { |
|
75 start(); |
|
76 }; |
|
77 }); |
|
78 }); |
|
79 |
|
80 test('test callback after synchronous request', function() { |
|
81 expect(1); |
|
82 var deferred = new Deferred(); |
|
83 var result = jQuery.ajax({ |
|
84 url: './ajax_url0.html', |
|
85 async: false, |
|
86 beforeSend: function(xhr) { |
|
87 deferred._req = xhr; |
|
88 }, |
|
89 success: function(data, status) { |
|
90 deferred.success(data); |
|
91 } |
|
92 }); |
|
93 stop(); |
|
94 deferred.addCallback(function() { |
|
95 try { |
|
96 // add an assertion to ensure the callback is executed |
|
97 ok(true, "callback is executed"); |
|
98 } finally { |
|
99 start(); |
|
100 }; |
|
101 }); |
|
102 }); |
|
103 |
|
104 test('test addCallback with parameters', function() { |
|
105 expect(3); |
|
106 equals(jQuery('#main').children().length, 0); |
|
107 stop(); |
|
108 var d = jQuery('#main').loadxhtml('/../ajax_url0.html'); |
|
109 d.addCallback(function(data, req, arg1, arg2) { |
|
110 try { |
|
111 equals(arg1, 'Hello'); |
|
112 equals(arg2, 'world'); |
|
113 } finally { |
|
114 start(); |
|
115 }; |
|
116 }, |
|
117 'Hello', 'world'); |
|
118 }); |
|
119 |
|
120 test('test callback after synchronous request with parameters', function() { |
|
121 expect(2); |
|
122 var deferred = new Deferred(); |
|
123 deferred.addCallback(function(data, req, arg1, arg2) { |
|
124 // add an assertion to ensure the callback is executed |
|
125 try { |
|
126 ok(true, "callback is executed"); |
|
127 equals(arg1, 'Hello'); |
|
128 equals(arg2, 'world'); |
|
129 } finally { |
|
130 start(); |
|
131 }; |
|
132 }, |
|
133 'Hello', 'world'); |
|
134 deferred.addErrback(function() { |
|
135 // throw an exception to start errback chain |
|
136 try { |
|
137 throw this._error; |
|
138 } finally { |
|
139 start(); |
|
140 }; |
|
141 }); |
|
142 stop(); |
|
143 var result = jQuery.ajax({ |
|
144 url: '/../ajax_url0.html', |
|
145 async: false, |
|
146 beforeSend: function(xhr) { |
|
147 deferred._req = xhr; |
|
148 }, |
|
149 success: function(data, status) { |
|
150 deferred.success(data); |
|
151 } |
|
152 }); |
|
153 }); |
|
154 |
|
155 test('test addErrback', function() { |
|
156 expect(1); |
|
157 stop(); |
|
158 var d = jQuery('#main').loadxhtml('/../ajax_url0.html'); |
|
159 d.addCallback(function() { |
|
160 // throw an exception to start errback chain |
|
161 try { |
|
162 throw new Error(); |
|
163 } finally { |
|
164 start(); |
|
165 }; |
|
166 }); |
|
167 d.addErrback(function() { |
|
168 try { |
|
169 ok(true, "errback is executed"); |
|
170 } finally { |
|
171 start(); |
|
172 }; |
|
173 }); |
|
174 }); |
|
175 |
|
176 test('test callback / errback execution order', function() { |
|
177 expect(4); |
|
178 var counter = 0; |
|
179 stop(); |
|
180 var d = jQuery('#main').loadxhtml('/../ajax_url0.html', { |
|
181 callback: function() { |
|
182 try { |
|
183 equals(++counter, 1); // should be executed first |
|
184 } finally { |
|
185 start(); |
|
186 }; |
|
187 } |
|
188 }); |
|
189 d.addCallback(function() { |
|
190 equals(++counter, 2); // should be executed and break callback chain |
|
191 throw new Error(); |
|
192 }); |
|
193 d.addCallback(function() { |
|
194 // should not be executed since second callback raised an error |
|
195 ok(false, "callback is executed"); |
|
196 }); |
|
197 d.addErrback(function() { |
|
198 // should be executed after the second callback |
|
199 equals(++counter, 3); |
|
200 }); |
|
201 d.addErrback(function() { |
|
202 // should be executed after the first errback |
|
203 equals(++counter, 4); |
|
204 }); |
|
205 }); |
|
206 |
|
207 test('test already included resources are ignored (ajax_url1.html)', function() { |
|
208 expect(10); |
|
209 var scriptsIncluded = jsSources(); |
|
210 // NOTE: |
|
211 equals(jQuery.inArray('http://foo.js', scriptsIncluded), -1); |
|
212 equals(jQuery('head link').length, 1); |
|
213 /* use endswith because in pytest context we have an absolute path */ |
|
214 ok(jQuery('head link').attr('href').endswith('/qunit.css')); |
|
215 stop(); |
|
216 jQuery('#main').loadxhtml('/../ajax_url1.html', { |
|
217 callback: function() { |
|
218 var origLength = scriptsIncluded.length; |
|
219 scriptsIncluded = jsSources(); |
|
220 try { |
|
221 // check that foo.js has been inserted in <head> |
|
222 equals(scriptsIncluded.length, origLength + 1); |
|
223 equals(scriptsIncluded[origLength].indexOf('http://foo.js'), 0); |
|
224 // check that <div class="ajaxHtmlHead"> has been removed |
|
225 equals(jQuery('#main').children().length, 1); |
|
226 equals(jQuery('div.ajaxHtmlHead').length, 0); |
|
227 equals(jQuery('#main h1').html(), 'Hello'); |
|
228 // qunit.css is not added twice |
|
229 equals(jQuery('head link').length, 1); |
|
230 /* use endswith because in pytest context we have an absolute path */ |
|
231 ok(jQuery('head link').attr('href').endswith('/qunit.css')); |
|
232 } finally { |
|
233 start(); |
|
234 } |
|
235 } |
|
236 }); |
|
237 }); |
|
238 |
|
239 test('test synchronous request loadRemote', function() { |
|
240 var res = loadRemote('/../ajaxresult.json', {}, |
|
241 'GET', true); |
|
242 same(res, ['foo', 'bar']); |
|
243 }); |
|
244 |
|
245 test('test event on CubicWeb', function() { |
|
246 expect(1); |
|
247 stop(); |
|
248 var events = null; |
|
249 jQuery(CubicWeb).bind('server-response', function() { |
|
250 // check that server-response event on CubicWeb is triggered |
|
251 events = 'CubicWeb'; |
|
252 }); |
|
253 jQuery('#main').loadxhtml('/../ajax_url0.html', { |
|
254 callback: function() { |
|
255 try { |
|
256 equals(events, 'CubicWeb'); |
|
257 } finally { |
|
258 start(); |
|
259 }; |
|
260 } |
|
261 }); |
|
262 }); |
|
263 |
|
264 test('test event on node', function() { |
|
265 expect(3); |
|
266 stop(); |
|
267 var nodes = []; |
|
268 jQuery('#main').bind('server-response', function() { |
|
269 nodes.push('node'); |
|
270 }); |
|
271 jQuery(CubicWeb).bind('server-response', function() { |
|
272 nodes.push('CubicWeb'); |
|
273 }); |
|
274 jQuery('#main').loadxhtml('/../ajax_url0.html', { |
|
275 callback: function() { |
|
276 try { |
|
277 equals(nodes.length, 2); |
|
278 // check that server-response event on CubicWeb is triggered |
|
279 // only once and event server-response on node is triggered |
|
280 equals(nodes[0], 'CubicWeb'); |
|
281 equals(nodes[1], 'node'); |
|
282 } finally { |
|
283 start(); |
|
284 }; |
|
285 } |
|
286 }); |
|
287 }); |
|
288 }); |
|
289 |