assert-element
Advanced tools
Comparing version 0.1.0 to 0.2.0
{ | ||
"name": "assert-element", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"dependencies": { | ||
@@ -5,0 +5,0 @@ "component/assert": "*" |
0.2.0 / 2015-09-16 | ||
================== | ||
* hasChild: allowing array for index argument | ||
0.1.0 / 2015-09-11 | ||
@@ -3,0 +8,0 @@ ================== |
24
index.js
@@ -97,8 +97,9 @@ | ||
assert(node.children.length > 0, 'provided node has no children'); | ||
assert(typeof index === 'number', 'provided index is not a number'); | ||
assert(index >= 0, 'provided index cannot be negative'); | ||
var child = node.children[index]; | ||
assert(typeof child !== 'undefined', 'child does not exist at the given index'); | ||
var child = Array.isArray(index) | ||
? deepChild(node, index) | ||
: node.children[index]; | ||
assert(typeof child !== 'undefined', 'child does not exist at the given index ' + index); | ||
if (criteria) { | ||
@@ -168,4 +169,19 @@ if (typeof criteria === 'function') { | ||
/** | ||
* Retrieve a deep child via an input array `index` of indices to traverse. | ||
* | ||
* @param {Object} node The virtual node to traverse. | ||
* @param {Array:Number} path The path to traverse. | ||
* @return {Object} | ||
*/ | ||
function deepChild(root, path) { | ||
return path.reduce(function (node, index, x) { | ||
assert(index in node.children, 'child does not exist at the given deep index ' + path.join('.')); | ||
return node.children[index]; | ||
}, root); | ||
} | ||
/** | ||
* No operation. | ||
*/ | ||
function noop() {} |
{ | ||
"name": "assert-element", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "Assertions for checking virtual nodes used by Deku/React/etc", | ||
@@ -5,0 +5,0 @@ "repository": "dekujs/assert-element", |
@@ -154,3 +154,3 @@ | ||
describe('.hasChild(node, child, children)', function () { | ||
describe('.hasChildren(node, children)', function () { | ||
it('should throw when missing the node', fail(function () { | ||
@@ -205,2 +205,20 @@ assertions.hasChildren(); | ||
describe('.notHasChildren(node)', function () { | ||
it('should throw when missing the node', fail(function () { | ||
assertions.notHasChildren(); | ||
})); | ||
it('should throw for objects that are not virtual nodes', fail(function () { | ||
assertions.notHasChildren({}); | ||
})); | ||
it('should not throw when there are no children', function () { | ||
assertions.notHasChildren(element('div')); | ||
}); | ||
it('should not throw when there are children', fail(function () { | ||
assertions.notHasChildren(element('div', null, 'hello world')); | ||
})); | ||
}); | ||
describe('.hasChild(node, index, [fn])', function () { | ||
@@ -234,2 +252,18 @@ it('should throw when missing the node', fail(function () { | ||
}); | ||
context('with array for index', function () { | ||
var root = element('ul', null, [ | ||
element('li', null, element('a', { href: 'http://example.com/' })), | ||
element('li', null, element('a', { href: 'http://example.org/' })) | ||
]); | ||
it('should not throw when it finds the right child', function () { | ||
assertions.hasChild(root, [ 0, 0 ]); | ||
assertions.hasChild(root, [ 1, 0 ]); | ||
}); | ||
it('should throw when it cannot find a child', fail(function () { | ||
assertions.hasChild(root, [ 2, 0 ]); | ||
})); | ||
}); | ||
}); | ||
@@ -247,2 +281,22 @@ | ||
})); | ||
context('with array for index', function () { | ||
var root = element('ul', null, [ | ||
element('li', null, element('b', null, 'Hello')), | ||
element('li', null, element('span', null, 'World')) | ||
]); | ||
it('should not throw when it finds the right child', function () { | ||
assertions.hasChild(root, [ 0, 0, 0 ], 'Hello'); | ||
assertions.hasChild(root, [ 1, 0, 0 ], 'World'); | ||
}); | ||
it('should throw when it cannot find a child', fail(function () { | ||
assertions.hasChild(root, [ 2, 0, 0 ], 'Hello'); | ||
})); | ||
it('should throw when deep comparison fails', fail(function () { | ||
assertions.hasChild(root, [ 1, 0, 0 ], 'Hello'); | ||
})); | ||
}); | ||
}); | ||
@@ -264,21 +318,24 @@ | ||
}); | ||
}); | ||
}); | ||
describe('.notHasChildren(node)', function () { | ||
it('should throw when missing the node', fail(function () { | ||
assertions.notHasChildren(); | ||
})); | ||
context('with array for index', function () { | ||
var root = element('ul', null, [ | ||
element('li', null, element('b', null, 'Hello')), | ||
element('li', null, element('span', null, 'World')) | ||
]); | ||
it('should throw for objects that are not virtual nodes', fail(function () { | ||
assertions.notHasChildren({}); | ||
})); | ||
it('should not throw when `criteria` does not throw', function () { | ||
assertions.hasChild(root, [ 0, 0, 0 ], test); | ||
it('should not throw when there are no children', function () { | ||
assertions.notHasChildren(element('div')); | ||
function test(child) {} | ||
}); | ||
it('should throw when `criteria throws`', fail(function () { | ||
assertions.hasChild(root, [ 2, 0, 0 ], test); | ||
function test(child) { | ||
throw new Error('fail'); | ||
} | ||
})); | ||
}); | ||
}); | ||
it('should not throw when there are children', fail(function () { | ||
assertions.notHasChildren(element('div', null, 'hello world')); | ||
})); | ||
}); | ||
@@ -285,0 +342,0 @@ }); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
24468
444