|
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 equals(jQuery('#main').children().length, 1); |
|
32 equals(jQuery('#main h1').html(), 'Hello'); |
|
33 start(); |
|
34 } |
|
35 }); |
|
36 }); |
|
37 |
|
38 test('test simple html head inclusion (ajax_url1.html)', function() { |
|
39 expect(6); |
|
40 var scriptsIncluded = jsSources(); |
|
41 equals(jQuery.inArray('http://foo.js', scriptsIncluded), - 1); |
|
42 stop(); |
|
43 jQuery('#main').loadxhtml('/../ajax_url1.html', { |
|
44 callback: function() { |
|
45 var origLength = scriptsIncluded.length; |
|
46 scriptsIncluded = jsSources(); |
|
47 // check that foo.js has been inserted in <head> |
|
48 equals(scriptsIncluded.length, origLength + 1); |
|
49 equals(scriptsIncluded[origLength].indexOf('http://foo.js'), 0); |
|
50 // check that <div class="ajaxHtmlHead"> has been removed |
|
51 equals(jQuery('#main').children().length, 1); |
|
52 equals(jQuery('div.ajaxHtmlHead').length, 0); |
|
53 equals(jQuery('#main h1').html(), 'Hello'); |
|
54 start(); |
|
55 } |
|
56 }); |
|
57 }); |
|
58 |
|
59 test('test addCallback', function() { |
|
60 expect(3); |
|
61 equals(jQuery('#main').children().length, 0); |
|
62 stop(); |
|
63 var d = jQuery('#main').loadxhtml('/../ajax_url0.html'); |
|
64 d.addCallback(function() { |
|
65 equals(jQuery('#main').children().length, 1); |
|
66 equals(jQuery('#main h1').html(), 'Hello'); |
|
67 start(); |
|
68 }); |
|
69 }); |
|
70 |
|
71 test('test callback after synchronous request', function() { |
|
72 expect(1); |
|
73 var deferred = new Deferred(); |
|
74 var result = jQuery.ajax({ |
|
75 url: './ajax_url0.html', |
|
76 async: false, |
|
77 beforeSend: function(xhr) { |
|
78 deferred._req = xhr; |
|
79 }, |
|
80 success: function(data, status) { |
|
81 deferred.success(data); |
|
82 } |
|
83 }); |
|
84 stop(); |
|
85 deferred.addCallback(function() { |
|
86 // add an assertion to ensure the callback is executed |
|
87 ok(true, "callback is executed"); |
|
88 start(); |
|
89 }); |
|
90 }); |
|
91 |
|
92 test('test addCallback with parameters', function() { |
|
93 expect(3); |
|
94 equals(jQuery('#main').children().length, 0); |
|
95 stop(); |
|
96 var d = jQuery('#main').loadxhtml('/../ajax_url0.html'); |
|
97 d.addCallback(function(data, req, arg1, arg2) { |
|
98 equals(arg1, 'Hello'); |
|
99 equals(arg2, 'world'); |
|
100 start(); |
|
101 }, |
|
102 'Hello', 'world'); |
|
103 }); |
|
104 |
|
105 test('test callback after synchronous request with parameters', function() { |
|
106 var deferred = new Deferred(); |
|
107 var result = jQuery.ajax({ |
|
108 url: './ajax_url0.html', |
|
109 async: false, |
|
110 beforeSend: function(xhr) { |
|
111 deferred._req = xhr; |
|
112 }, |
|
113 success: function(data, status) { |
|
114 deferred.success(data); |
|
115 } |
|
116 }); |
|
117 deferred.addCallback(function(data, req, arg1, arg2) { |
|
118 // add an assertion to ensure the callback is executed |
|
119 ok(true, "callback is executed"); |
|
120 equals(arg1, 'Hello'); |
|
121 equals(arg2, 'world'); |
|
122 }, |
|
123 'Hello', 'world'); |
|
124 }); |
|
125 |
|
126 test('test addErrback', function() { |
|
127 expect(1); |
|
128 stop(); |
|
129 var d = jQuery('#main').loadxhtml('/../ajax_url0.html'); |
|
130 d.addCallback(function() { |
|
131 // throw an exception to start errback chain |
|
132 throw new Error(); |
|
133 }); |
|
134 d.addErrback(function() { |
|
135 ok(true, "errback is executed"); |
|
136 start(); |
|
137 }); |
|
138 }); |
|
139 |
|
140 test('test callback / errback execution order', function() { |
|
141 expect(4); |
|
142 var counter = 0; |
|
143 stop(); |
|
144 var d = jQuery('#main').loadxhtml('/../ajax_url0.html', { |
|
145 callback: function() { |
|
146 equals(++counter, 1); // should be executed first |
|
147 start(); |
|
148 } |
|
149 }); |
|
150 d.addCallback(function() { |
|
151 equals(++counter, 2); // should be executed and break callback chain |
|
152 throw new Error(); |
|
153 }); |
|
154 d.addCallback(function() { |
|
155 // should not be executed since second callback raised an error |
|
156 ok(false, "callback is executed"); |
|
157 }); |
|
158 d.addErrback(function() { |
|
159 // should be executed after the second callback |
|
160 equals(++counter, 3); |
|
161 }); |
|
162 d.addErrback(function() { |
|
163 // should be executed after the first errback |
|
164 equals(++counter, 4); |
|
165 }); |
|
166 }); |
|
167 |
|
168 test('test already included resources are ignored (ajax_url1.html)', function() { |
|
169 expect(10); |
|
170 var scriptsIncluded = jsSources(); |
|
171 // NOTE: |
|
172 equals(jQuery.inArray('http://foo.js', scriptsIncluded), -1); |
|
173 equals(jQuery('head link').length, 1); |
|
174 /* use endswith because in pytest context we have an absolute path */ |
|
175 ok(jQuery('head link').attr('href').endswith('/qunit.css')); |
|
176 stop(); |
|
177 jQuery('#main').loadxhtml('/../ajax_url1.html', { |
|
178 callback: function() { |
|
179 var origLength = scriptsIncluded.length; |
|
180 scriptsIncluded = jsSources(); |
|
181 try { |
|
182 // check that foo.js has been inserted in <head> |
|
183 equals(scriptsIncluded.length, origLength + 1); |
|
184 equals(scriptsIncluded[origLength].indexOf('http://foo.js'), 0); |
|
185 // check that <div class="ajaxHtmlHead"> has been removed |
|
186 equals(jQuery('#main').children().length, 1); |
|
187 equals(jQuery('div.ajaxHtmlHead').length, 0); |
|
188 equals(jQuery('#main h1').html(), 'Hello'); |
|
189 // qunit.css is not added twice |
|
190 equals(jQuery('head link').length, 1); |
|
191 /* use endswith because in pytest context we have an absolute path */ |
|
192 ok(jQuery('head link').attr('href').endswith('/qunit.css')); |
|
193 } finally { |
|
194 start(); |
|
195 } |
|
196 } |
|
197 }); |
|
198 }); |
|
199 |
|
200 test('test synchronous request loadRemote', function() { |
|
201 var res = loadRemote('/../ajaxresult.json', {}, |
|
202 'GET', true); |
|
203 same(res, ['foo', 'bar']); |
|
204 }); |
|
205 |
|
206 test('test event on CubicWeb', function() { |
|
207 expect(1); |
|
208 stop(); |
|
209 var events = null; |
|
210 jQuery(CubicWeb).bind('server-response', function() { |
|
211 // check that server-response event on CubicWeb is triggered |
|
212 events = 'CubicWeb'; |
|
213 }); |
|
214 jQuery('#main').loadxhtml('/../ajax_url0.html', { |
|
215 callback: function() { |
|
216 equals(events, 'CubicWeb'); |
|
217 start(); |
|
218 } |
|
219 }); |
|
220 }); |
|
221 |
|
222 test('test event on node', function() { |
|
223 expect(3); |
|
224 stop(); |
|
225 var nodes = []; |
|
226 jQuery('#main').bind('server-response', function() { |
|
227 nodes.push('node'); |
|
228 }); |
|
229 jQuery(CubicWeb).bind('server-response', function() { |
|
230 nodes.push('CubicWeb'); |
|
231 }); |
|
232 jQuery('#main').loadxhtml('/../ajax_url0.html', { |
|
233 callback: function() { |
|
234 equals(nodes.length, 2); |
|
235 // check that server-response event on CubicWeb is triggered |
|
236 // only once and event server-response on node is triggered |
|
237 equals(nodes[0], 'CubicWeb'); |
|
238 equals(nodes[1], 'node'); |
|
239 start(); |
|
240 } |
|
241 }); |
|
242 }); |
|
243 }); |
|
244 |