Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[test] add initial search-simple testcase
- Loading branch information
1 parent
90cb2d3
commit 1e36849
Showing
5 changed files
with
259 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
|
||
/node_modules/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,7 +18,7 @@ | |
"html" | ||
], | ||
"devDependencies": { | ||
"blow" : "0.2.x" | ||
"blow" : "0.3.x" | ||
}, | ||
"license": "MIT", | ||
"engines": { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<!DOCTYPE html> | ||
<html lang=en checked> | ||
<head> | ||
<title>Template document</title> | ||
<link rel="stylesheet" data-href="file_1.css"> | ||
<link rel="stylesheet" data-href="file_2.css"> | ||
<link rel="stylesheet" data-href="file_3.css"> | ||
</head> | ||
<body> | ||
<script>"<span></span>";</script> | ||
|
||
<menu> | ||
<li data-match="1">1</li> | ||
<li data-match="2">2</li> | ||
<li data-match="1">1</li> | ||
</menu> | ||
|
||
<li data-match="1">1</li> | ||
<li data-match="2">2</li> | ||
<li data-match="3">3</li> | ||
|
||
<div id="main"> | ||
<a href="/foo/">could look like singleton</a> | ||
<input type="checkbox" checked> | ||
</div> | ||
|
||
<footer data-attr="custom">Bottom</footer> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="utf8"> | ||
<title></title> | ||
<script src="../module.js"></script> | ||
</head> | ||
<body> | ||
<iframe id="content" style="display: none;" src="/static?src=fixture/template.html"> | ||
</iframe> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,214 @@ | ||
/** | ||
* Copyright (c) 2012 Andreas Madsen | ||
* MIT License | ||
*/ | ||
|
||
var assert = chai.assert; | ||
|
||
suite('testing document search', ready(function(){ | ||
|
||
var iframe = document.getElementById('content'); | ||
var content = iframe.contentDocument || iframe.contentWindow.document; | ||
|
||
var doc = domstream(content); | ||
|
||
suite('when searching for a missing element using', function(){ | ||
|
||
suite('tagname', | ||
expectNoResult(doc.find().elem('missing'))); | ||
|
||
suite('attribute name', | ||
expectNoResult(doc.find().attr('missing'))); | ||
|
||
suite('attribute value', | ||
expectNoResult(doc.find().attr('rel', 'missing'))); | ||
|
||
suite('attribute RegExp', | ||
expectNoResult(doc.find().attr('rel', /missing/))); | ||
|
||
suite('tagname followed by attribute value', | ||
expectNoResult(doc.find().elem('link').attr('data-match', '1'))); | ||
|
||
suite('attribute followed by tagname value', | ||
expectNoResult(doc.find().attr('data-match', '1').elem('missing'))); | ||
|
||
suite('multiply steps', expectNoResult(function () { | ||
var search = doc.find(); | ||
|
||
// this will perform a real search for all <link> element | ||
search.elem('link'); | ||
search.toArray(); | ||
|
||
// this will setup search for all <link data-match="1" *> | ||
// note that such elements do not exist, but a <link *> element and <* data-match="1" *> do. | ||
search.attr('data-match', '1'); | ||
|
||
return search; | ||
})); | ||
}); | ||
|
||
function expectNoResult(search) { | ||
|
||
return function () { | ||
if (search instanceof Function) { | ||
search = search(); | ||
} | ||
|
||
test('toValue returns false', function () { | ||
assert.isFalse(search.toValue()); | ||
}); | ||
|
||
test('toArray returns empty array', function () { | ||
assert.lengthOf(search.toArray(), 0); | ||
}); | ||
}; | ||
} | ||
|
||
suite('when searching with .only() using', function () { | ||
|
||
var links = [ | ||
content.getElementsByTagName('link')[0], | ||
content.getElementsByTagName('link')[1] | ||
]; | ||
|
||
suite('tagname', | ||
expectOneResult(doc.find().only().elem('link'), links[0])); | ||
|
||
suite('attribute name', | ||
expectOneResult(doc.find().only().attr('rel'), links[0])); | ||
|
||
suite('attribute value', | ||
expectOneResult(doc.find().only().attr('rel', 'stylesheet'), links[0])); | ||
|
||
suite('attribute RegExp', | ||
expectOneResult(doc.find().only().attr('href', /^file_(2|3)\.css$/), links[1])); | ||
|
||
suite('tagname followed by attribute value', | ||
expectOneResult(doc.find().only().elem('link').attr('rel', 'stylesheet'), links[0])); | ||
|
||
suite('attribute followed by tagname value', | ||
expectOneResult(doc.find().only().attr('rel', 'stylesheet').elem('link'), links[0])); | ||
|
||
suite('multiply steps with only at start', function () { | ||
var search = doc.find(); | ||
|
||
// this will perform a real search for the first <link> element | ||
search.only().elem('link').toValue(); | ||
|
||
// since nodeList only contains one element, and futher search | ||
// will be performed on nodeList, the use is not allowed t | ||
// add more search criterias, since the will result in a wrong | ||
// search result | ||
var error = null; | ||
try { | ||
search.attr('href', 'file_2.css').toValue(); // throw | ||
} catch (e) { | ||
error = e; | ||
} finally { | ||
test('it should throw', function (error) { | ||
assert.instanceOf(error, Error); | ||
}); | ||
} | ||
}); | ||
|
||
suite('multiply steps with only at middle', expectOneResult(function () { | ||
var search = doc.find(); | ||
|
||
// this will perform a real search for all <link> element | ||
search.elem('link').toValue(); | ||
|
||
// find second link element | ||
search.only().attr('href', 'file_2.css'); // no throw | ||
|
||
return search; | ||
}, links[1])); | ||
|
||
suite('multiply steps with only at end', expectOneResult(function () { | ||
var search = doc.find(); | ||
|
||
// this will perform a real search for all <link> element | ||
search.elem('link').toValue(); | ||
|
||
// find second link element | ||
search.attr('href', 'file_2.css').only(); // no throw | ||
|
||
return search; | ||
}, links[1])); | ||
|
||
}); | ||
|
||
function expectOneResult(search, result) { | ||
return function () { | ||
if (search instanceof Function) { | ||
search = search(); | ||
} | ||
|
||
test('toValue returns an element', function () { | ||
assert.strictEqual(search.toValue().elem, result); | ||
}); | ||
|
||
test('toArray returns array with one item', function () { | ||
assert.lengthOf(search.toArray(), 1); | ||
assert.strictEqual(search.toArray()[0].elem, result); | ||
}); | ||
}; | ||
} | ||
|
||
var items = content.getElementsByTagName('li'); | ||
|
||
suite('when searching using', function () { | ||
suite('tagname', | ||
expectResult(doc.find().elem('li'), items)); | ||
|
||
suite('attribute name', | ||
expectResult(doc.find().attr('data-match'), items)); | ||
|
||
suite('attribute value', | ||
expectResult(doc.find().attr('data-match', '1'), [ items[0], items[2], items[3] ])); | ||
|
||
suite('attribute RegExp', | ||
expectResult(doc.find().attr('data-match', /^(2|3)$/), [ items[1], items[4], items[5] ])); | ||
|
||
suite('tagname followed by attribute value', | ||
expectResult(doc.find().elem('li').attr('data-match', '1'), [ items[0], items[2], items[3] ])); | ||
|
||
suite('attribute followed by tagname value', | ||
expectResult(doc.find().attr('data-match', '1').elem('li'), [ items[0], items[2], items[3] ])); | ||
|
||
suite('multiply steps', expectResult(function () { | ||
var search = doc.find(); | ||
|
||
// this will perform a real search for all <li> element | ||
search.elem('li'); | ||
search.toArray(); | ||
|
||
// this will setup search for all <li data-match="1" *> | ||
search.attr('data-match', '1'); | ||
|
||
return search; | ||
}, [ items[0], items[2], items[3] ])); | ||
}); | ||
|
||
function expectResult(search, results) { | ||
var length = results.length; | ||
|
||
return function () { | ||
test('toValue returns an element', function () { | ||
assert.lengthOf(search.toValue(), length); | ||
|
||
search.toValue().forEach(function (node, index) { | ||
assert.strictEqual(node.elem, results[index]); | ||
}); | ||
}); | ||
|
||
test('toArray returns array with one item', function () { | ||
assert.lengthOf(search.toArray(), length); | ||
|
||
search.toArray().forEach(function (node, index) { | ||
assert.strictEqual(node.elem, results[index]); | ||
}); | ||
}); | ||
}; | ||
} | ||
|
||
})); |