File: stateLoadCallback.js

package info (click to toggle)
datatables.js 1.10.13%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 6,232 kB
  • ctags: 1,329
  • sloc: xml: 10,249; php: 4,387; sh: 492; makefile: 21
file content (136 lines) | stat: -rw-r--r-- 3,048 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
describe( "stateLoadCallback Option", function() {
	dt.libs( {
		js:  [ 'jquery', 'datatables' ],
		css: [ 'datatables' ]
	} );

	it( 'Default should be a function', function () {
		expect( typeof $.fn.dataTable.defaults.fnStateLoadCallback ).toBe( 'function' );
	} );


	dt.html( 'basic' );

	it( 'Two arguments passed', function () {
		test = -1;

		$('#example').DataTable( {
			stateSave: true,
			stateLoadCallback: function ( settings, callback ){
				test = arguments;
			}
		} );

		expect(test.length === 2).toBe(true);
	} );

	it( 'First is the settings object', function () {
		expect(test[0] == $('#example').DataTable().settings()[0]).toBe(true);
	} );

	it( 'Second is a callback function', function () {
		expect(typeof test[1]).toBe('function');
	} );


	dt.html( 'basic' );

	it( 'Can load state using a custom function - sync', function () {
		var table = $('#example').DataTable( {
			stateSave: true,
			stateLoadCallback: function ( settings ){
				return {
					time: (+new Date())+100,
					start: 15,
					length: 5
				};
			}
		} );

		expect( table.page() ).toBe( 3 );
		expect( table.page.len() ).toBe( 5 );
		expect( $('#example tbody td:eq(0)').html() ).toBe( 'Finn Camacho' );
	} );


	dt.html( 'basic' );

	it( 'Can load state using a custom function with callback - sync', function () {
		var table = $('#example').DataTable( {
			stateSave: true,
			stateLoadCallback: function ( settings, callback ){
				callback( {
					time: (+new Date())+100,
					search: {
						search: 'Fiona'
					}
				} );
			}
		} );

		expect( table.search() ).toBe( 'Fiona' );
		expect( $('#example tbody td:eq(0)').html() ).toBe( 'Fiona Green' );
	} );


	dt.html( 'basic' );

	it( 'Loading using an async function', function (done) {
		var table = $('#example').DataTable( {
			stateSave: true,
			stateLoadCallback: function ( settings, callback ){
				setTimeout( function () {
					callback( {
						time: (+new Date())+100,
						start: 14,
						length: 7
					} );

					expect( table.page() ).toBe( 2 );
					expect( table.page.len() ).toBe( 7 );
					expect( $('#example tbody td:eq(0)').html() ).toBe( 'Doris Wilder' );
					done();
				}, 200 );
			}
		} );
	} );


	dt.html( 'basic' );

	it( 'State is not loaded if time is not given', function () {
		var table = $('#example').DataTable( {
			stateSave: true,
			stateLoadCallback: function ( settings ){
				return {
					start: 15,
					length: 5
				};
			}
		} );

		expect( table.page() ).toBe( 0 );
		expect( table.page.len() ).toBe( 10 );
		expect( $('#example tbody td:eq(0)').html() ).toBe( 'Airi Satou' );
	} );


	dt.html( 'basic' );

	it( 'State is not loaded if time is a long time in the past', function () {
		var table = $('#example').DataTable( {
			stateSave: true,
			stateLoadCallback: function ( settings ){
				return {
					time: 10000,
					start: 15,
					length: 5
				};
			}
		} );

		expect( table.page() ).toBe( 0 );
		expect( table.page.len() ).toBe( 10 );
		expect( $('#example tbody td:eq(0)').html() ).toBe( 'Airi Satou' );
	} );
} );