Debian Codegraph @

old-tests/tests_onhold/3_ajax/ajax.js

raw
1 // DATA_TEMPLATE: empty_table
2 oTest.fnStart( "ajax" );
3
4
5 $(document).ready( function () {
6 var json;
7 var result;
8
9 //
10 // As a string
11 //
12 oTest.fnWaitTest(
13 "Basic request as a string - getting arrays",
14 function () {
15 $('#example').dataTable( {
16 "ajax": "../data_sources/arrays.php"
17 } );
18 },
19 function () {
20 return $('tbody td').eq(0).html() === '1';
21 }
22 );
23
24 oTest.fnWaitTest(
25 "Basic request as a string - getting objects",
26 function () {
27 oSession.fnRestore();
28 $('#example').dataTable( {
29 "ajax": "../data_sources/objects.php",
30 "columns": [
31 { data: 'engine' },
32 { data: 'browser' },
33 { data: 'platform' },
34 { data: 'version' },
35 { data: 'grade' }
36 ]
37 } );
38 },
39 function () {
40 return $('tbody td').eq(1).html() === '20';
41 }
42 );
43
44 oTest.fnWaitTest(
45 "Default request is GET - string based",
46 function () {
47 oSession.fnRestore();
48 json = {};
49
50 $('#example').dataTable( {
51 "ajax": "../data_sources/method.php?method=get"
52 } );
53 },
54 function () {
55 return $('tbody td').eq(0).html() === '1';
56 }
57 );
58
59 oTest.fnWaitTest(
60 "jQuery anti-cache parameter is sent by default - string based",
61 function () {
62 oSession.fnRestore();
63 json = {};
64
65 $('#example').dataTable( {
66 "ajax": "../data_sources/param.php"
67 } ).on('xhr', function (e, settings, o) {
68 json = o;
69 } );
70 },
71 function () {
72 return json.get && json.get._;
73 }
74 );
75
76 oTest.fnWaitTest(
77 "No other parameters sent",
78 null,
79 function () {
80 return json.get_length === 1;
81 }
82 );
83
84
85
86 //
87 // As an object
88 //
89 oTest.fnWaitTest(
90 "Get Ajax using url parameter only",
91 function () {
92 oSession.fnRestore();
93 $('#example').dataTable( {
94 "ajax": {
95 "url": "../data_sources/arrays.php"
96 }
97 } );
98 },
99 function () {
100 return $('tbody td').eq(0).html() === '1';
101 }
102 );
103
104
105 // props
106 oTest.fnWaitTest(
107 "Disable cache property",
108 function () {
109 oSession.fnRestore();
110 json = {};
111
112 $('#example').dataTable( {
113 "ajax": {
114 "url": "../data_sources/param.php",
115 "cache": true
116 }
117 } ).on('xhr', function (e, settings, o) {
118 json = o;
119 } );
120 },
121 function () {
122 return json.get && typeof json.get._ === 'undefined';
123 }
124 );
125
126 oTest.fnWaitTest(
127 "Set an error callback",
128 function () {
129 oSession.fnRestore();
130 result = false;
131
132 $('#example').dataTable( {
133 "ajax": {
134 "url": "../data_sources/rubbish",
135 "error": function () {
136 result = true;
137 }
138 }
139 } );
140 },
141 function () {
142 return result;
143 }
144 );
145
146 // type
147 oTest.fnWaitTest(
148 "type - Default request is GET",
149 function () {
150 oSession.fnRestore();
151
152 $('#example').dataTable( {
153 "ajax": {
154 "url": "../data_sources/method.php?method=get"
155 }
156 } );
157 },
158 function () {
159 return $('tbody td').eq(1).html() === '2';
160 }
161 );
162
163 oTest.fnWaitTest(
164 "type - Can use `type` to make a POST request",
165 function () {
166 oSession.fnRestore();
167
168 $('#example').dataTable( {
169 "ajax": {
170 "url": "../data_sources/method.php?method=post",
171 "type": "POST"
172 }
173 } );
174 },
175 function () {
176 return $('tbody td').eq(2).html() === '3';
177 }
178 );
179
180 oTest.fnWaitTest(
181 "type - Can use `type` to make a PUT request",
182 function () {
183 oSession.fnRestore();
184
185 $('#example').dataTable( {
186 "ajax": {
187 "url": "../data_sources/method.php?method=put",
188 "type": "PUT"
189 }
190 } );
191 },
192 function () {
193 return $('tbody td').eq(0).html() === '1';
194 }
195 );
196
197
198 // data
199 oTest.fnWaitTest(
200 "data - Function based data - has empty array as input",
201 function () {
202 oSession.fnRestore();
203 result = false;
204
205 $('#example').dataTable( {
206 "ajax": {
207 "url": "../data_sources/param.php",
208 "data": function ( d ) {
209 result = d.length === 0;
210 }
211 }
212 } );
213 },
214 function () {
215 return result;
216 }
217 );
218
219 oTest.fnWaitTest(
220 "data - Function based data - can return an object",
221 function () {
222 oSession.fnRestore();
223 json = {};
224
225 $('#example').dataTable( {
226 "ajax": {
227 "url": "../data_sources/param.php",
228 "data": function ( d ) {
229 return { 'tapestry': 'king' };
230 }
231 }
232 } ).on('xhr', function (e, settings, o) {
233 json = o;
234 } );
235 },
236 function () {
237 return json.get && json.get.tapestry === 'king';
238 }
239 );
240
241 oTest.fnWaitTest(
242 "data - Function based data - multiple properties",
243 function () {
244 oSession.fnRestore();
245 json = {};
246
247 $('#example').dataTable( {
248 "ajax": {
249 "url": "../data_sources/param.php",
250 "data": function ( d ) {
251 return { 'tapestry': 'king', 'move': 'earth' };
252 }
253 }
254 } ).on('xhr', function (e, settings, o) {
255 json = o;
256 } );
257 },
258 function () {
259 return json.get && json.get.tapestry === 'king' && json.get.move === 'earth';
260 }
261 );
262
263 oTest.fnWaitTest(
264 "data - Confirm no other parameters sent",
265 null,
266 function () {
267 return json.get_length === 3;
268 }
269 );
270
271
272 oTest.fnWaitTest(
273 "data - Function based data - can return an array of key/value object pairs",
274 function () {
275 oSession.fnRestore();
276 json = {};
277
278 $('#example').dataTable( {
279 "ajax": {
280 "url": "../data_sources/param.php",
281 "data": function ( d ) {
282 return [
283 { 'name': 'tapestry', 'value': 'carole' }
284 ];
285 }
286 }
287 } ).on('xhr', function (e, settings, o) {
288 json = o;
289 } );
290 },
291 function () {
292 return json.get && json.get.tapestry === 'carole';
293 }
294 );
295
296 oTest.fnWaitTest(
297 "data - Function based data - multiple properties",
298 function () {
299 oSession.fnRestore();
300 json = {};
301
302 $('#example').dataTable( {
303 "ajax": {
304 "url": "../data_sources/param.php",
305 "data": function ( d ) {
306 return [
307 { 'name': 'tapestry', 'value': 'carole' },
308 { 'name': 'feel', 'value': 'earth move' }
309 ];
310 }
311 }
312 } ).on('xhr', function (e, settings, o) {
313 json = o;
314 } );
315 },
316 function () {
317 return json.get && json.get.tapestry === 'carole' && json.get.feel === 'earth move';
318 }
319 );
320
321 oTest.fnWaitTest(
322 "data - Function based data - add parameters to passed in array",
323 function () {
324 oSession.fnRestore();
325 json = {};
326
327 $('#example').dataTable( {
328 "ajax": {
329 "url": "../data_sources/param.php",
330 "data": function ( d ) {
331 d.push( { 'name': 'tapestry', 'value': 'carole' } );
332 d.push( { 'name': 'rich', 'value': 'hue' } );
333 }
334 }
335 } ).on('xhr', function (e, settings, o) {
336 json = o;
337 } );
338 },
339 function () {
340 return json.get && json.get.tapestry === 'carole' && json.get.rich === 'hue';
341 }
342 );
343
344 oTest.fnWaitTest(
345 "data - Function based data - send parameters by POST",
346 function () {
347 oSession.fnRestore();
348 json = {};
349
350 $('#example').dataTable( {
351 "ajax": {
352 "url": "../data_sources/param.php",
353 "data": function ( d ) {
354 d.push( { 'name': 'tapestry', 'value': 'king' } );
355 d.push( { 'name': 'rich', 'value': 'hue' } );
356 },
357 "type": "POST"
358 }
359 } ).on('xhr', function (e, settings, o) {
360 json = o;
361 } );
362 },
363 function () {
364 return json.post && json.post.tapestry === 'king' && json.post.rich === 'hue';
365 }
366 );
367
368 oTest.fnWaitTest(
369 "data - Object based data - sends parameters defined",
370 function () {
371 oSession.fnRestore();
372 json = {};
373
374 $('#example').dataTable( {
375 "ajax": {
376 "url": "../data_sources/param.php",
377 "data": {
378 "too": "late",
379 "got": "friend"
380 }
381 }
382 } ).on('xhr', function (e, settings, o) {
383 json = o;
384 } );
385 },
386 function () {
387 return json.get && json.get.too === 'late' && json.get.got === 'friend';
388 }
389 );
390
391 oTest.fnWaitTest(
392 "data - Array based data - sends parameters defined",
393 function () {
394 oSession.fnRestore();
395 json = {};
396
397 $('#example').dataTable( {
398 "ajax": {
399 "url": "../data_sources/param.php",
400 "data": [
401 { 'name': 'tapestry', 'value': 'king' },
402 { 'name': 'far', 'value': 'away' }
403 ]
404 }
405 } ).on('xhr', function (e, settings, o) {
406 json = o;
407 } );
408 },
409 function () {
410 return json.get && json.get.tapestry === 'king' && json.get.far === 'away';
411 }
412 );
413
414
415 // dataSrc
416 oTest.fnWaitTest(
417 "dataSrc - Default data source is aaData",
418 function () {
419 oSession.fnRestore();
420
421 $('#example').dataTable( {
422 "ajax": {
423 "url": "../data_sources/arrays.php"
424 }
425 } );
426 },
427 function () {
428 return $('tbody td').eq(0).html() === '1';
429 }
430 );
431
432 oTest.fnWaitTest(
433 "dataSrc - as a string - read from `data`",
434 function () {
435 oSession.fnRestore();
436
437 $('#example').dataTable( {
438 "ajax": {
439 "url": "../data_sources/arrays.php?dataSrc=data",
440 "dataSrc": "data"
441 }
442 } );
443 },
444 function () {
445 return $('tbody td').eq(0).html() === '1';
446 }
447 );
448
449 oTest.fnWaitTest(
450 "dataSrc - as a string - read from nested property `data.inner`",
451 function () {
452 oSession.fnRestore();
453
454 $('#example').dataTable( {
455 "ajax": {
456 "url": "../data_sources/arrays.php?dataSrc=nested",
457 "dataSrc": "data.inner"
458 }
459 } );
460 },
461 function () {
462 return $('tbody td').eq(0).html() === '1';
463 }
464 );
465
466 oTest.fnWaitTest(
467 "dataSrc - as a string - read from plain array",
468 function () {
469 oSession.fnRestore();
470
471 $('#example').dataTable( {
472 "ajax": {
473 "url": "../data_sources/arrays.php?dataSrc=plain",
474 "dataSrc": ""
475 }
476 } );
477 },
478 function () {
479 return $('tbody td').eq(0).html() === '1';
480 }
481 );
482
483 oTest.fnWaitTest(
484 "dataSrc - as a function, return JSON property",
485 function () {
486 oSession.fnRestore();
487
488 $('#example').dataTable( {
489 "ajax": {
490 "url": "../data_sources/arrays.php?dataSrc=nested",
491 "dataSrc": function ( json ) {
492 return json.data.inner;
493 }
494 }
495 } );
496 },
497 function () {
498 return $('tbody td').eq(0).html() === '1';
499 }
500 );
501
502 oTest.fnWaitTest(
503 "dataSrc - as a function, can manipulate the data",
504 function () {
505 oSession.fnRestore();
506
507 $('#example').dataTable( {
508 "ajax": {
509 "url": "../data_sources/arrays.php?dataSrc=data",
510 "dataSrc": function ( json ) {
511 json.data[0][0] = "Tapestry";
512 return json.data;
513 }
514 }
515 } );
516 },
517 function () {
518 return $('tbody td').eq(0).html() === 'Tapestry' &&
519 $('tbody td').eq(1).html() === '2';
520 }
521 );
522
523
524
525 //
526 // As a function
527 //
528 oTest.fnTest(
529 "ajax as a function - first parameter is array of data",
530 function () {
531 oSession.fnRestore();
532 result = null;
533
534 $('#example').dataTable( {
535 "ajax": function ( data, callback, settings ) {
536 result = arguments;
537 callback( { aaData: [] } );
538 }
539 } );
540 },
541 function () {
542 return Array.isArray( result[0] ) && result[0].length === 0;
543 }
544 );
545
546 oTest.fnTest(
547 "ajax as a function - second parameter is callback function",
548 null,
549 function () {
550 return $.isFunction( result[1] );
551 }
552 );
553
554 oTest.fnTest(
555 "ajax as a function - third parameter is settings object",
556 null,
557 function () {
558 return result[2] === $('#example').dataTable().fnSettings();
559 }
560 );
561
562 oTest.fnTest(
563 "ajax as a function - only three parameters",
564 null,
565 function () {
566 return result.length === 3;
567 }
568 );
569
570 oTest.fnTest(
571 "ajax as a function - callback will insert data into the table",
572 function () {
573 oSession.fnRestore();
574
575 $('#example').dataTable( {
576 "ajax": function ( data, callback, settings ) {
577 callback( { aaData: [[1,2,3,4,5]] } );
578 }
579 } );
580 },
581 function () {
582 return $('tbody td').eq(0).html() === '1';
583 }
584 );
585
586
587
588 oTest.fnComplete();
589 } );