UNPKG

2.16 kBJavaScriptView Raw
1
2var tf = new TableFilter('demo', {
3 base_path: '../dist/tablefilter/',
4 state: {
5 types: ['local_storage'],
6 filters: true,
7 page_number: true,
8 page_length: true
9 },
10 paging: {
11 results_per_page: ['Records: ', [2, 4, 6]],
12 }
13});
14tf.init();
15var state = tf.feature('state');
16var storage = state.storage;
17
18module('Sanity checks');
19test('State instance', function() {
20 deepEqual(typeof storage, 'object', 'Storage is instantiated');
21 deepEqual(storage.enableLocalStorage, true, 'Local storage enabled');
22 deepEqual(storage.enableCookie, false, 'Cookies disabled');
23 deepEqual(storage.state, state, 'State instance');
24 deepEqual(storage.emitter, state.emitter, 'Emitter instance');
25});
26
27module('Behaviour');
28test('Can save and retrieve state', function() {
29 // setup
30 var stateObj = {
31 'page': 2,
32 'page_length': 4,
33 'col_2': {'flt': '>500'}
34 };
35
36 // act
37 state.emitter.emit('state-changed', tf, stateObj);
38
39 // assert
40 deepEqual(storage.retrieve(), stateObj, 'State saved');
41});
42
43test('Can sync state', function() {
44 // setup
45 storage.save({'page':2,'page_length':4,'col_2':{'flt':'>500'}});
46
47 // act
48 storage.sync();
49
50 // assert
51 deepEqual(tf.getValidRows(), [2,3,5,6,7,8], 'Table filters are synced');
52});
53
54test('Can remove state', function() {
55 // setup
56 storage.save({'page':2,'page_length':4,'col_2':{'flt':'>500'}});
57
58 // act
59 storage.remove();
60
61 // assert
62 deepEqual(storage.retrieve(), null, 'State removed from storage');
63});
64
65test('Can get storage key', function() {
66 // assert
67 deepEqual(storage.getKey(),
68 '{"key":"TF_demo","path":"/test/test-storage-local.html"}',
69 'Storage key returned'
70 );
71});
72
73module('Tear-down');
74test('Can destroy', function() {
75 // act
76 storage.destroy();
77
78 // assert
79 deepEqual(storage.state, null, 'State instance is null');
80 deepEqual(storage.emitter, null, 'Emitter instance is null');
81 deepEqual(storage.retrieve(), null, 'Persisted state cleared');
82});