1
0
Fork 0
mirror of https://github.com/koalyptus/TableFilter.git synced 2024-05-02 14:52:50 +02:00
TableFilter/test/test-hash.js

89 lines
2.1 KiB
JavaScript
Raw Normal View History

2016-03-20 12:44:41 +01:00
var tf = new TableFilter('demo', {
base_path: '../dist/tablefilter/',
state: {
2016-04-08 17:43:55 +02:00
types: ['hash'],
2016-03-20 12:44:41 +01:00
filters: true,
page_number: true,
page_length: true
},
2017-06-14 14:12:44 +02:00
paging: {
results_per_page: ['Records: ', [2, 4, 6]]
}
2016-03-20 12:44:41 +01:00
});
tf.init();
var state = tf.feature('state');
var hash = state.hash;
module('Sanity checks');
test('State instance', function() {
deepEqual(typeof hash, 'object', 'Hash is instantiated');
2016-05-15 04:56:12 +02:00
deepEqual(hash.lastHash, '', 'Last stored hash');
2016-03-20 12:44:41 +01:00
deepEqual(hash.state, state, 'State instance');
deepEqual(hash.emitter, state.emitter, 'Emitter instance');
});
module('Behaviour');
test('Can update URL hash', function() {
// setup
var stateObj = {
'page': 2,
'page_length': 4,
'col_2': {'flt': '>500'}
};
// act
state.emitter.emit('state-changed', tf, stateObj);
// assert
deepEqual(location.hash,
'#%7B%22page%22%3A2%2C%22page_length%22%3A4%2C%22'+
'col_2%22%3A%7B%22flt%22%3A%22%3E500%22%7D%7D',
2016-03-20 12:44:41 +01:00
'URL hash updated');
});
test('Can parse a URL hash', function() {
// setup
// URL-encoded version of:
// #{"page":2,"page_length":4,"col_2":{"flt":">500"}}
var hashStr = '#%7B%22page%22%3A2%2C%22page_length%22%3A4'+
2017-05-25 05:51:44 +02:00
'%2C%22col_2%22%3A%7B%22flt%22%3A%22%3E500%22%7D%7D';
2016-03-20 12:44:41 +01:00
// act
var result = hash.parse(hashStr);
// assert
deepEqual(result,
{
'page': 2,
'page_length': 4,
'col_2': {'flt': '>500'}
},
'Parsed hash'
);
});
test('Can sync state', function() {
// setup
location.hash = '#{"page":2,"page_length":4,"col_2":{"flt":">500"}}';
// act
hash.sync();
// assert
deepEqual(tf.getValidRows(), [2,3,5,6,7,8], 'Table filters are synced');
});
module('Tear-down');
test('Can destroy', function() {
// setup
location.hash = '';
// act
hash.destroy();
// assert
deepEqual(hash.state, null, 'State instance is null');
deepEqual(hash.lastHash, null, 'Last hash reference is null');
deepEqual(hash.emitter, null, 'Emitter instance is null');
});