mirror of
https://github.com/koalyptus/TableFilter.git
synced 2024-06-10 09:52:33 +02:00
Added state unit tests
This commit is contained in:
parent
4a8613f68b
commit
1e3f51db10
78
test/test-state.html
Normal file
78
test/test-state.html
Normal file
|
@ -0,0 +1,78 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>TableFilter state object</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-state.js"></script>
|
||||
|
||||
<div id="qunit"></div>
|
||||
<div id="qunit-fixture"></div>
|
||||
</body>
|
||||
</html>
|
107
test/test-state.js
Normal file
107
test/test-state.js
Normal file
|
@ -0,0 +1,107 @@
|
|||
|
||||
var tf = new TableFilter('demo', {
|
||||
base_path: '../dist/tablefilter/',
|
||||
state: true
|
||||
});
|
||||
tf.init();
|
||||
var state = tf.feature('state');
|
||||
module('Sanity checks');
|
||||
test('State instance', function() {
|
||||
deepEqual(tf.state, true, 'State is enabled');
|
||||
deepEqual(typeof state, 'object', 'State is instantiated');
|
||||
deepEqual(state.initialized, true, 'State is initialized');
|
||||
deepEqual(state.enableHash, true, 'Hash is enabled by default');
|
||||
deepEqual(state.persistFilters, true, 'Filters are persisted by default');
|
||||
deepEqual(state.persistPageNumber, false, 'Page number not persisted');
|
||||
deepEqual(state.persistPageLength, false, 'Page length not persisted');
|
||||
deepEqual(typeof state.state, 'object', 'State field is an object');
|
||||
});
|
||||
|
||||
module('Behaviour');
|
||||
test('Can update', function() {
|
||||
// setup
|
||||
state.state = {};
|
||||
|
||||
// act
|
||||
tf.setFilterValue(1, 'Adelaide');
|
||||
tf.filter();
|
||||
|
||||
// assert
|
||||
deepEqual(state.state.col_1, { 'flt': 'Adelaide' },
|
||||
'State object updated');
|
||||
});
|
||||
|
||||
test('Can update on clear filters', function() {
|
||||
// setup
|
||||
state.state = {};
|
||||
|
||||
// act
|
||||
tf.clearFilters();
|
||||
|
||||
// assert
|
||||
deepEqual(state.state.col_1, undefined, 'State object updated');
|
||||
});
|
||||
|
||||
test('Can sync state', function() {
|
||||
// setup
|
||||
state.state = {};
|
||||
state.state.col_2 = { 'flt': '>1500' };
|
||||
|
||||
// act
|
||||
state.sync();
|
||||
|
||||
// assert
|
||||
deepEqual(tf.getValidRows(), [6, 7, 8], 'Table filters are synced');
|
||||
});
|
||||
|
||||
test('Can override state', function() {
|
||||
// setup
|
||||
state.state = {};
|
||||
|
||||
// act
|
||||
state.override({ 'col_1': { 'flt': 'Ade' } });
|
||||
|
||||
// assert
|
||||
deepEqual(state.state, { 'col_1': { 'flt': 'Ade' } },
|
||||
'State field overriden');
|
||||
});
|
||||
|
||||
test('Can update page number', function() {
|
||||
// setup
|
||||
state.persistPageNumber = true;
|
||||
state.state = {};
|
||||
|
||||
// act
|
||||
state.updatePage('2');
|
||||
|
||||
// assert
|
||||
deepEqual(state.state.page, '2', 'Page number updated');
|
||||
|
||||
state.persistPageNumber = false;
|
||||
});
|
||||
|
||||
test('Can update page length', function() {
|
||||
// setup
|
||||
state.persistPageLength = true;
|
||||
state.state = {};
|
||||
|
||||
// act
|
||||
state.updatePageLength('10');
|
||||
|
||||
// assert
|
||||
deepEqual(state.state.page_length, '10', 'Page length updated');
|
||||
|
||||
state.persistPageLength = false;
|
||||
});
|
||||
|
||||
module('Tear-down');
|
||||
test('can destroy TF', function() {
|
||||
// setup
|
||||
location.hash = null;
|
||||
|
||||
// act
|
||||
tf.destroy();
|
||||
|
||||
// assert
|
||||
deepEqual(state.initialized, false, 'State not initialized');
|
||||
});
|
Loading…
Reference in a new issue