mirror of
https://github.com/koalyptus/TableFilter.git
synced 2024-05-19 06:47:00 +02:00
Added hash unit tests
This commit is contained in:
parent
ea604b76ba
commit
a62a41cbc4
78
test/test-hash.html
Normal file
78
test/test-hash.html
Normal file
|
@ -0,0 +1,78 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<title>TableFilter hash persistence</title>
|
||||||
|
<link rel="stylesheet" href="libs/qunit/qunit.css">
|
||||||
|
<script src="libs/qunit/qunit.js"></script>
|
||||||
|
<script src="libs/polyfill.js"></script>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<table id="demo" cellpadding="0" cellspacing="0">
|
||||||
|
<tbody>
|
||||||
|
<tr>
|
||||||
|
<th>From</th>
|
||||||
|
<th>Destination</th>
|
||||||
|
<th>Road Distance (km)</th>
|
||||||
|
<th>By Air (hrs)</th>
|
||||||
|
<th>By Rail (hrs)</th>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td><strong>Sydney</strong></td>
|
||||||
|
<td>Adelaide</td>
|
||||||
|
<td>1412</td>
|
||||||
|
<td>1.4</td>
|
||||||
|
<td>25.3</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td><strong>Sydney</strong></td>
|
||||||
|
<td>Brisbane</td>
|
||||||
|
<td>982</td>
|
||||||
|
<td>1.5</td>
|
||||||
|
<td>16</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td><strong>Sydney</strong></td>
|
||||||
|
<td>Canberra</td>
|
||||||
|
<td>286</td>
|
||||||
|
<td>.6</td>
|
||||||
|
<td>4.3</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td><strong>Sydney</strong></td>
|
||||||
|
<td>Melbourne</td>
|
||||||
|
<td>872</td>
|
||||||
|
<td>1.1</td>
|
||||||
|
<td>10.5</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td><strong>Adelaide</strong></td>
|
||||||
|
<td>Perth</td>
|
||||||
|
<td>2781</td>
|
||||||
|
<td>3.1</td>
|
||||||
|
<td>38</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td><strong>Adelaide</strong></td>
|
||||||
|
<td>Alice Springs</td>
|
||||||
|
<td>1533</td>
|
||||||
|
<td>2</td>
|
||||||
|
<td>20.25</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td><strong>Adelaide</strong></td>
|
||||||
|
<td>Brisbane</td>
|
||||||
|
<td>2045</td>
|
||||||
|
<td>2.15</td>
|
||||||
|
<td>40</td>
|
||||||
|
</tr>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
|
||||||
|
<script src="../dist/tablefilter/tablefilter.js"></script>
|
||||||
|
<script src="test-hash.js"></script>
|
||||||
|
|
||||||
|
<div id="qunit"></div>
|
||||||
|
<div id="qunit-fixture"></div>
|
||||||
|
</body>
|
||||||
|
</html>
|
84
test/test-hash.js
Normal file
84
test/test-hash.js
Normal file
|
@ -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');
|
||||||
|
});
|
Loading…
Reference in a new issue