1 |
|
2 | var 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 | });
|
14 | tf.init();
|
15 | var state = tf.feature('state');
|
16 | var hash = state.hash;
|
17 |
|
18 | module('Sanity checks');
|
19 | test('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 |
|
26 | module('Behaviour');
|
27 | test('Can update URL hash', function() {
|
28 |
|
29 | var stateObj = {
|
30 | 'page': 2,
|
31 | 'page_length': 4,
|
32 | 'col_2': {'flt': '>500'}
|
33 | };
|
34 |
|
35 |
|
36 | state.emitter.emit('state-changed', tf, stateObj);
|
37 |
|
38 |
|
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 |
|
45 | test('Can parse a URL hash', function() {
|
46 |
|
47 |
|
48 |
|
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 |
|
52 | var result = hash.parse(hashStr);
|
53 |
|
54 |
|
55 | deepEqual(result,
|
56 | {
|
57 | 'page': 2,
|
58 | 'page_length': 4,
|
59 | 'col_2': {'flt': '>500'}
|
60 | },
|
61 | 'Parsed hash'
|
62 | );
|
63 | });
|
64 |
|
65 | test('Can sync state', function() {
|
66 |
|
67 | location.hash = '#{"page":2,"page_length":4,"col_2":{"flt":">500"}}';
|
68 |
|
69 |
|
70 | hash.sync();
|
71 |
|
72 |
|
73 | deepEqual(tf.getValidRows(), [2,3,5,6,7,8], 'Table filters are synced');
|
74 | });
|
75 |
|
76 | module('Tear-down');
|
77 | test('Can destroy', function() {
|
78 |
|
79 | location.hash = '';
|
80 |
|
81 |
|
82 | hash.destroy();
|
83 |
|
84 |
|
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 | });
|