Skip to content

Commit

Permalink
Merge pull request javve#152 from caiges/master
Browse files Browse the repository at this point in the history
Added tests for table based lists.
  • Loading branch information
javve committed Sep 18, 2013
2 parents 234da5f + 785ac86 commit d3d75b7
Show file tree
Hide file tree
Showing 5 changed files with 249 additions and 36 deletions.
86 changes: 57 additions & 29 deletions tests/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,71 @@
<head>
<title>QUnit Tests</title>
<link rel="stylesheet" href="qunit/qunit.css" media="screen">

<script src="../src/list.js"></script>
<script src="qunit/qunit.js"></script>
<script src="list.tests.js"></script>

</head>
<body class="flora">
<h1 id="qunit-header">QUnit Test Suite</h1>
<h2 id="qunit-banner"></h2>
<div id="qunit-testrunner-toolbar"></div>
<h2 id="qunit-userAgent"></h2>
<ol id="qunit-tests"></ol>
<div id="qunit-fixture">test markup</div>
<div id="qunit-fixture">
<div id="list">
<input class="search">
<span class="sort" rel="id">Sort value 1</span>
<span class="sort" rel="name">Sort value 2</span>
<span class="sort" rel="feature">Sort value 3</span>
<ul class="list">
<li class="item">
<span class="id">1</span>
<span class="name">John Resig</span>
<span class="feature">jQuery</span>
</li>
<li class="item">
<span class="id">2</span>
<span class="name">Paul Irish</span>
<span class="feature">HTML5 boilerplate</span>
</li>
<li class="item">
<span class="id">3</span>
<span class="name">Anton Johansson</span>
<span class="feature">Hacker</span>
</li>
</ul><!-- !.list -->
</div><!-- !#list -->

<div id="table">
<table>
<thead>
<tr>
<th class="sort" data-sort="id">id</th>
<th class="sort" data-sort="name">name</th>
<th class="sort" sata-sort="feature">feature</th>
</tr>
</thead>
<tbody class="list">
<tr>
<td class="id">1</td>
<td class="name">John Resig</td>
<td class="feature">jQuery</td>
</tr>
<tr>
<td class="id">2</td>
<td class="name">Paul Irish</td>
<td class="feature">HTML5 Boilerplate</td>
</tr>
<tr>
<td class="id">3</td>
<td class="name">Anton Johansson</td>
<td class="feature">Hacker</td>
</tr>
</tbody>
</table>
</div><!-- !#table -->
</div><!-- !#qunit-fixture -->

<div id="list" style="visibility:hidden;">
<input class="search" />
<span class="sort" rel="id">Sort value 1</span>
<span class="sort" rel="name">Sort value 2</span>
<span class="sort" rel="feature">Sort value 3</span>
<ul class="list">
<li class="item">
<span class="id">1</span>
<span class="name">John Resig</span>
<span class="feature">jQuery</span>
</li>
<li class="item">
<span class="id">2</span>
<span class="name">Paul Irish</span>
<span class="feature">HTML5 boilerplate</span>
</li>
<li class="item">
<span class="id">3</span>
<span class="name">Anton Johansson</span>
<span class="feature">Hacker</span>
</li>
</ul>
</div>
<script src="../src/list.js"></script>
<script src="qunit/qunit.js"></script>
<script src="list.tests.js"></script>
<script src="table.js"></script>
</body>
</html>
12 changes: 6 additions & 6 deletions tests/list-creation.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ <h2 id="qunit-userAgent"></h2>


<div id="list1" style="visibility:hidden;">
<input class="search" />
<input class="search">
<ul class="list">
<li class="item">
<span class="id">1</span>
Expand All @@ -28,12 +28,12 @@ <h2 id="qunit-userAgent"></h2>
</ul>
</div>
<div id="list2" style="visibility:hidden;">
<input class="search" />
<input class="search">
<ul class="list">
</ul>
</div>
<div id="list3" style="visibility:hidden;">
<input class="search" />
<input class="search">
<ul class="list">
</ul>
</div>
Expand All @@ -44,14 +44,14 @@ <h2 id="qunit-userAgent"></h2>
</li>
</ul>
<div class="list4" style="visibility:hidden;">
<input class="search" />
<input class="search">
<ul class="list">
</ul>
</div>


<div id="list-broken" style="visibility:hidden;">
<input class="search" />
<input class="search">
<ul class="list">
<li>
<span class="id">1</span>
Expand All @@ -68,7 +68,7 @@ <h2 id="qunit-userAgent"></h2>
</div>

<div id="list-broken-item" style="visibility:hidden;">
<input class="search" />
<input class="search">
<ul class="list">
</ul>
</div>
Expand Down
3 changes: 2 additions & 1 deletion tests/list.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ test('Search with null values', function() {
, name: null
, feature: null
};
theList.add(objectWithNulls);
try {
var items = theList.search('Node');
equals(items.length, 2);
Expand Down Expand Up @@ -160,5 +161,5 @@ test('Filter', function() {

test('Restore from filter', function() {
var visibleItems = theList.filter();
equals(visibleItems.length, 7);
equals(visibleItems.length, 8);
});
Empty file added tests/table.html
Empty file.
184 changes: 184 additions & 0 deletions tests/table.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
var list,
fixtureMarkup = document.querySelector('#table').innerHTML,
options = {
valueNames: [
'id',
'name',
'feature'
]
},
ryan = {
id: 4,
name: 'Ryan Dahl',
feature: 'Node'
},
tj = {
id: 5,
name: 'TJ Holowaychuk',
feature: 'Node'
},
jonny = {
id: 6,
name: 'Jonny Strömberg',
feature: 'List.js'
};

module('table', {
setup: function() {
list = new List('table', options);
},
teardown: function() {
document.getElementById('table').innerHTML = fixtureMarkup;
}
});

test('List creation', function() {
equal(list.items.length, 3);
});

test('size: Count items', function() {
equal(list.size(), 3);
});

test('add: Add an item', function() {
list.add({ id: '14', name: 'caige', feature: 'listjs' });
equal(list.size(), 4);
});

test('add: Add multiple items', function() {
list.add([ryan, tj]);
equal(list.size(), 5);
});

test('remove: Remove one item', function() {
var item = list.remove('id', 1);
equals(item, 1, 'Item should have been found when removed.');
equal(list.size(), 2, 'One item should be removed.');
});

test('remove: Try and remove one item that does not exist', function() {
var item = list.remove('id', 9);

equals(item, 0, 'Item should not be found');
equals(list.size(), 3, 'List contents should not have changed');
});

test('get: Get one item', function() {
var item = list.get('id', 3);
deepEqual(item.values(), {
id: '3',
name: 'Anton Johansson',
feature: 'Hacker'
});
});

test('get: Get two items,', function() {
var items;
list.add([ ryan, tj ]);
items = list.get('feature', 'Node');
items = [
items[0].values(),
items[1].values()
];
deepEqual(items, [ryan, tj]);
});

test('get: Get item that does not exist', function() {
var item = list.get('id', 200);
equals(item, null);
});

test('search: term', function() {
var items;
list.add([ryan, tj]);
items = list.search('Node');
items = [
items[0].values(),
items[1].values()
];
deepEqual(items, [ryan, tj]);
});

test('search: null values', function() {
var items,
objectWithNulls = {
id: 7,
name: null,
feature: null
};

expect(2);
list.add([ryan, tj]);

try {
items = list.search('Node');
equals(items.length, 2);
items = [
items[0].values(),
items[1].values()
];
deepEqual(items, [ryah, tj]);
} catch(e) {}
});

test('search: for 0', function() {
var items,
objectWithZeros = {
id: 7,
name: 'Node',
feature: 0
};

expect(2);
list.add(objectWithZeros);
try {
items = list.search(0);
equals(items.length, 1);
items = [
items[0].values()
];
deepEqual(items, [objectWithZeros]);
} catch(e) {}
});

test('search: undefined values', function() {
var items,
objectWithZeros = {
id: 7,
name: 'Node',
feature: 0
},
objectWithUndefined = {
id: 8,
name: 'Node',
feature: undefined
};
expect(2);
list.add([objectWithZeros, objectWithUndefined]);
try {
items = list.search(0);
equals(items.length, 1);
items = [
items[0].values()
];
deepEqual(items, [objectWithZeros]);
} catch(e) {}
});

test('filter: Filter list', function() {
var visibleItems = list.filter(function(item) {
if (+item.values().id < 3) {
return true;
} else {
return false;
}
});
equals(visibleItems.length, 2);
equals(visibleItems[0].values().id, 1);
equals(visibleItems[1].values().id, 2);
});

test('filter: Restore via filter', function() {
var visibleItems = list.filter();
equals(visibleItems.length, 3);
});

0 comments on commit d3d75b7

Please sign in to comment.