From a62a41cbc4bb86e30229444b82b4da08764f40d7 Mon Sep 17 00:00:00 2001 From: Max Guglielmi Date: Sun, 20 Mar 2016 22:44:41 +1100 Subject: [PATCH] Added hash unit tests --- test/test-hash.html | 78 +++++++++++++++++++++++++++++++++++++++++ test/test-hash.js | 84 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 162 insertions(+) create mode 100644 test/test-hash.html create mode 100644 test/test-hash.js diff --git a/test/test-hash.html b/test/test-hash.html new file mode 100644 index 00000000..e0197ba3 --- /dev/null +++ b/test/test-hash.html @@ -0,0 +1,78 @@ + + + + + TableFilter hash persistence + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
FromDestinationRoad Distance (km)By Air (hrs)By Rail (hrs)
SydneyAdelaide14121.425.3
SydneyBrisbane9821.516
SydneyCanberra286.64.3
SydneyMelbourne8721.110.5
AdelaidePerth27813.138
AdelaideAlice Springs1533220.25
AdelaideBrisbane20452.1540
+ + + + +
+
+ + diff --git a/test/test-hash.js b/test/test-hash.js new file mode 100644 index 00000000..54c72b0e --- /dev/null +++ b/test/test-hash.js @@ -0,0 +1,84 @@ + +var tf = new TableFilter('demo', { + base_path: '../dist/tablefilter/', + state: { + type: ['hash'], + filters: true, + page_number: true, + page_length: true + }, + paging: true, + results_per_page: ['Records: ', [2, 4, 6]], +}); +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'); + deepEqual(hash.lastHash, '', 'Last store hash'); + 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, + '#{"page":2,"page_length":4,"col_2":{"flt":">500"}}', + 'URL hash updated'); +}); + +test('Can parse a URL hash', function() { + // setup + var hashStr = '#{"page":2,"page_length":4,"col_2":{"flt":">500"}}'; + + // 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'); +});