UNPKG

2.26 kBJavaScriptView Raw
1
2var tf = new TableFilter('demo', {
3 base_path: '../dist/tablefilter/',
4 state: {
5 types: ['hash'],
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 hash = state.hash;
17
18module('Sanity checks');
19test('State instance', function() {
20 deepEqual(typeof hash, 'object', 'Hash is instantiated');
21 deepEqual(hash.lastHash, '', 'Last stored hash');
22 deepEqual(hash.state, state, 'State instance');
23 deepEqual(hash.emitter, state.emitter, 'Emitter instance');
24});
25
26module('Behaviour');
27test('Can update URL hash', function() {
28 // setup
29 var stateObj = {
30 'page': 2,
31 'page_length': 4,
32 'col_2': {'flt': '>500'}
33 };
34
35 // act
36 state.emitter.emit('state-changed', tf, stateObj);
37
38 // assert
39 deepEqual(location.hash,
40 '#%7B%22page%22%3A2%2C%22page_length%22%3A4%2C%22'+
41 'col_2%22%3A%7B%22flt%22%3A%22%3E500%22%7D%7D',
42 'URL hash updated');
43});
44
45test('Can parse a URL hash', function() {
46 // setup
47 // URL-encoded version of:
48 // #{"page":2,"page_length":4,"col_2":{"flt":">500"}}
49 var hashStr = '#%7B%22page%22%3A2%2C%22page_length%22%3A4'+
50 '%2C%22col_2%22%3A%7B%22flt%22%3A%22%3E500%22%7D%7D';
51 // act
52 var result = hash.parse(hashStr);
53
54 // assert
55 deepEqual(result,
56 {
57 'page': 2,
58 'page_length': 4,
59 'col_2': {'flt': '>500'}
60 },
61 'Parsed hash'
62 );
63});
64
65test('Can sync state', function() {
66 // setup
67 location.hash = '#{"page":2,"page_length":4,"col_2":{"flt":">500"}}';
68
69 // act
70 hash.sync();
71
72 // assert
73 deepEqual(tf.getValidRows(), [2,3,5,6,7,8], 'Table filters are synced');
74});
75
76module('Tear-down');
77test('Can destroy', function() {
78 // setup
79 location.hash = '';
80
81 // act
82 hash.destroy();
83
84 // assert
85 deepEqual(hash.state, null, 'State instance is null');
86 deepEqual(hash.lastHash, null, 'Last hash reference is null');
87 deepEqual(hash.emitter, null, 'Emitter instance is null');
88});