Debian Codegraph @

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