|
1 $(document).ready(function() { |
|
2 |
|
3 QUnit.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_scripts = []; |
|
11 cw.loaded_links = []; |
|
12 }, |
|
13 teardown: function() { |
|
14 $('head script[src]:lt(' + ($('head script[src]').length - 1 - 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 QUnit.test('test simple h1 inclusion (ajax_url0.html)', function (assert) { |
|
26 assert.expect(3); |
|
27 assert.equal($('#qunit-fixture').children().length, 0); |
|
28 var done = assert.async(); |
|
29 $('#qunit-fixture').loadxhtml('static/jstests/ajax_url0.html', null, 'GET') |
|
30 .addCallback(function() { |
|
31 try { |
|
32 assert.equal($('#qunit-fixture').children().length, 1); |
|
33 assert.equal($('#qunit-fixture h1').html(), 'Hello'); |
|
34 } finally { |
|
35 done(); |
|
36 }; |
|
37 } |
|
38 ); |
|
39 }); |
|
40 |
|
41 QUnit.test('test simple html head inclusion (ajax_url1.html)', function (assert) { |
|
42 assert.expect(6); |
|
43 var scriptsIncluded = jsSources(); |
|
44 assert.equal(jQuery.inArray('http://foo.js', scriptsIncluded), - 1); |
|
45 var done = assert.async(); |
|
46 $('#qunit-fixture').loadxhtml('static/jstests/ajax_url1.html', null, 'GET') |
|
47 .addCallback(function() { |
|
48 try { |
|
49 var origLength = scriptsIncluded.length; |
|
50 scriptsIncluded = jsSources(); |
|
51 // check that foo.js has been prepended to <head> |
|
52 assert.equal(scriptsIncluded.length, origLength + 1); |
|
53 assert.equal(scriptsIncluded.indexOf('http://foo.js'), 0); |
|
54 // check that <div class="ajaxHtmlHead"> has been removed |
|
55 assert.equal($('#qunit-fixture').children().length, 1); |
|
56 assert.equal($('div.ajaxHtmlHead').length, 0); |
|
57 assert.equal($('#qunit-fixture h1').html(), 'Hello'); |
|
58 } finally { |
|
59 done(); |
|
60 }; |
|
61 } |
|
62 ); |
|
63 }); |
|
64 |
|
65 QUnit.test('test addCallback', function (assert) { |
|
66 assert.expect(3); |
|
67 assert.equal($('#qunit-fixture').children().length, 0); |
|
68 var done = assert.async(); |
|
69 var d = $('#qunit-fixture').loadxhtml('static/jstests/ajax_url0.html', null, 'GET'); |
|
70 d.addCallback(function() { |
|
71 try { |
|
72 assert.equal($('#qunit-fixture').children().length, 1); |
|
73 assert.equal($('#qunit-fixture h1').html(), 'Hello'); |
|
74 } finally { |
|
75 done(); |
|
76 }; |
|
77 }); |
|
78 }); |
|
79 |
|
80 QUnit.test('test callback after synchronous request', function (assert) { |
|
81 assert.expect(1); |
|
82 var deferred = new Deferred(); |
|
83 var result = jQuery.ajax({ |
|
84 url: 'static/jstests/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 var done = assert.async(); |
|
94 deferred.addCallback(function() { |
|
95 try { |
|
96 // add an assertion to ensure the callback is executed |
|
97 assert.ok(true, "callback is executed"); |
|
98 } finally { |
|
99 done(); |
|
100 }; |
|
101 }); |
|
102 }); |
|
103 |
|
104 QUnit.test('test addCallback with parameters', function (assert) { |
|
105 assert.expect(3); |
|
106 assert.equal($('#qunit-fixture').children().length, 0); |
|
107 var done = assert.async(); |
|
108 var d = $('#qunit-fixture').loadxhtml('static/jstests/ajax_url0.html', null, 'GET'); |
|
109 d.addCallback(function(data, req, arg1, arg2) { |
|
110 try { |
|
111 assert.equal(arg1, 'Hello'); |
|
112 assert.equal(arg2, 'world'); |
|
113 } finally { |
|
114 done(); |
|
115 }; |
|
116 }, |
|
117 'Hello', 'world'); |
|
118 }); |
|
119 |
|
120 QUnit.test('test callback after synchronous request with parameters', function (assert) { |
|
121 assert.expect(3); |
|
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 assert.ok(true, "callback is executed"); |
|
127 assert.equal(arg1, 'Hello'); |
|
128 assert.equal(arg2, 'world'); |
|
129 } finally { |
|
130 done(); |
|
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 done(); |
|
140 }; |
|
141 }); |
|
142 var done = assert.async(); |
|
143 var result = jQuery.ajax({ |
|
144 url: 'static/jstests/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 QUnit.test('test addErrback', function (assert) { |
|
156 assert.expect(1); |
|
157 var done = assert.async(); |
|
158 var d = $('#qunit-fixture').loadxhtml('static/jstests/nonexistent.html', null, 'GET'); |
|
159 d.addCallback(function() { |
|
160 // should not be executed |
|
161 assert.ok(false, "callback is executed"); |
|
162 }); |
|
163 d.addErrback(function() { |
|
164 try { |
|
165 assert.ok(true, "errback is executed"); |
|
166 } finally { |
|
167 done(); |
|
168 }; |
|
169 }); |
|
170 }); |
|
171 |
|
172 QUnit.test('test callback execution order', function (assert) { |
|
173 assert.expect(3); |
|
174 var counter = 0; |
|
175 var done = assert.async(); |
|
176 var d = $('#qunit-fixture').loadxhtml('static/jstests/ajax_url0.html', null, 'GET'); |
|
177 d.addCallback(function() { |
|
178 assert.equal(++counter, 1); // should be executed first |
|
179 }); |
|
180 d.addCallback(function() { |
|
181 assert.equal(++counter, 2); |
|
182 }); |
|
183 d.addCallback(function() { |
|
184 try { |
|
185 assert.equal(++counter, 3); |
|
186 } finally { |
|
187 done(); |
|
188 } |
|
189 }); |
|
190 }); |
|
191 |
|
192 QUnit.test('test already included resources are ignored (ajax_url1.html)', function (assert) { |
|
193 assert.expect(10); |
|
194 var scriptsIncluded = jsSources(); |
|
195 // NOTE: |
|
196 assert.equal(jQuery.inArray('http://foo.js', scriptsIncluded), -1); |
|
197 assert.equal($('head link').length, 1); |
|
198 /* use endswith because in pytest context we have an absolute path */ |
|
199 assert.ok($('head link').attr('href').endswith('/qunit.css'), 'qunit.css is loaded'); |
|
200 var done = assert.async(); |
|
201 $('#qunit-fixture').loadxhtml('static/jstests/ajax_url1.html', null, 'GET') |
|
202 .addCallback(function() { |
|
203 var origLength = scriptsIncluded.length; |
|
204 scriptsIncluded = jsSources(); |
|
205 try { |
|
206 // check that foo.js has been inserted in <head> |
|
207 assert.equal(scriptsIncluded.length, origLength + 1); |
|
208 assert.equal(scriptsIncluded.indexOf('http://foo.js'), 0); |
|
209 // check that <div class="ajaxHtmlHead"> has been removed |
|
210 assert.equal($('#qunit-fixture').children().length, 1); |
|
211 assert.equal($('div.ajaxHtmlHead').length, 0); |
|
212 assert.equal($('#qunit-fixture h1').html(), 'Hello'); |
|
213 // qunit.css is not added twice |
|
214 assert.equal($('head link').length, 1); |
|
215 /* use endswith because in pytest context we have an absolute path */ |
|
216 assert.ok($('head link').attr('href').endswith('/qunit.css'), 'qunit.css is loaded'); |
|
217 } finally { |
|
218 done(); |
|
219 } |
|
220 } |
|
221 ); |
|
222 }); |
|
223 |
|
224 QUnit.test('test synchronous request loadRemote', function (assert) { |
|
225 var res = loadRemote('static/jstests/ajaxresult.json', {}, |
|
226 'GET', true); |
|
227 assert.deepEqual(res, ['foo', 'bar']); |
|
228 }); |
|
229 |
|
230 QUnit.test('test event on CubicWeb', function (assert) { |
|
231 assert.expect(1); |
|
232 var done = assert.async(); |
|
233 var events = null; |
|
234 $(CubicWeb).bind('server-response', function() { |
|
235 // check that server-response event on CubicWeb is triggered |
|
236 events = 'CubicWeb'; |
|
237 }); |
|
238 $('#qunit-fixture').loadxhtml('static/jstests/ajax_url0.html', null, 'GET') |
|
239 .addCallback(function() { |
|
240 try { |
|
241 assert.equal(events, 'CubicWeb'); |
|
242 } finally { |
|
243 done(); |
|
244 }; |
|
245 } |
|
246 ); |
|
247 }); |
|
248 |
|
249 QUnit.test('test event on node', function (assert) { |
|
250 assert.expect(3); |
|
251 var done = assert.async(); |
|
252 var nodes = []; |
|
253 $('#qunit-fixture').bind('server-response', function() { |
|
254 nodes.push('node'); |
|
255 }); |
|
256 $(CubicWeb).bind('server-response', function() { |
|
257 nodes.push('CubicWeb'); |
|
258 }); |
|
259 $('#qunit-fixture').loadxhtml('static/jstests/ajax_url0.html', null, 'GET') |
|
260 .addCallback(function() { |
|
261 try { |
|
262 assert.equal(nodes.length, 2); |
|
263 // check that server-response event on CubicWeb is triggered |
|
264 // only once and event server-response on node is triggered |
|
265 assert.equal(nodes[0], 'CubicWeb'); |
|
266 assert.equal(nodes[1], 'node'); |
|
267 } finally { |
|
268 done(); |
|
269 }; |
|
270 } |
|
271 ); |
|
272 }); |
|
273 }); |
|
274 |