Skip to content

Commit

Permalink
Added 'to contain no elements matching' assertion
Browse files Browse the repository at this point in the history
  • Loading branch information
Munter committed May 11, 2015
1 parent defa430 commit 6ef1b71
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,10 @@ module.exports = {
return this.shift(expect, queryResult, 1);
});

expect.addAssertion(['DOMDocument', 'DOMElement'], 'to contain no elements matching', function (expect, subject, value) {
return expect(subject.querySelectorAll(value), 'to satisfy', []);
});

expect.addAssertion('string', 'when parsed as (html|HTML)', function (expect, subject) {
this.errorMode = 'nested';
var htmlDocument;
Expand Down
35 changes: 35 additions & 0 deletions test/unexpected-dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -834,6 +834,41 @@ describe('unexpected-dom', function () {
});
});

describe('to contain no elements matching', function () {
it('should pass when not matching anything', function () {
var document = jsdom.jsdom('<!DOCTYPE html><html><body></body></html>');

expect(document, 'to contain no elements matching', '.foo');
});

it('should fail when matching a single node', function () {
var document = jsdom.jsdom('<!DOCTYPE html><html><body><div class="foo"></div></body></html>');

expect(function () {
expect(document, 'to contain no elements matching', '.foo');
}, 'to throw', 'expected <!DOCTYPE html><html><head></head><body>...</body></html> to contain no elements matching \'.foo\'\n' +
'\n' +
'[\n' +
' <div class="foo"></div> // should be removed\n' +
']'
);
});

it('should fail when matching a NodeList', function () {
var document = jsdom.jsdom('<!DOCTYPE html><html><body><div class="foo"></div><div class="foo"></div></body></html>');

expect(function () {
expect(document, 'to contain no elements matching', '.foo');
}, 'to throw', 'expected <!DOCTYPE html><html><head></head><body>......</body></html> to contain no elements matching \'.foo\'\n' +
'\n' +
'[\n' +
' <div class="foo"></div>, // should be removed\n' +
' <div class="foo"></div> // should be removed\n' +
']'
);
});
});

describe('diffing', function () {
function parseHtmlElement(str) {
return jsdom.jsdom('<!DOCTYPE html><html><body>' + str + '</body></html>').body.firstChild;
Expand Down

0 comments on commit 6ef1b71

Please sign in to comment.