Skip to content

Commit 6ef1b71

Browse files
committed
Added 'to contain no elements matching' assertion
1 parent defa430 commit 6ef1b71

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

lib/index.js

+4
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,10 @@ module.exports = {
715715
return this.shift(expect, queryResult, 1);
716716
});
717717

718+
expect.addAssertion(['DOMDocument', 'DOMElement'], 'to contain no elements matching', function (expect, subject, value) {
719+
return expect(subject.querySelectorAll(value), 'to satisfy', []);
720+
});
721+
718722
expect.addAssertion('string', 'when parsed as (html|HTML)', function (expect, subject) {
719723
this.errorMode = 'nested';
720724
var htmlDocument;

test/unexpected-dom.js

+35
Original file line numberDiff line numberDiff line change
@@ -834,6 +834,41 @@ describe('unexpected-dom', function () {
834834
});
835835
});
836836

837+
describe('to contain no elements matching', function () {
838+
it('should pass when not matching anything', function () {
839+
var document = jsdom.jsdom('<!DOCTYPE html><html><body></body></html>');
840+
841+
expect(document, 'to contain no elements matching', '.foo');
842+
});
843+
844+
it('should fail when matching a single node', function () {
845+
var document = jsdom.jsdom('<!DOCTYPE html><html><body><div class="foo"></div></body></html>');
846+
847+
expect(function () {
848+
expect(document, 'to contain no elements matching', '.foo');
849+
}, 'to throw', 'expected <!DOCTYPE html><html><head></head><body>...</body></html> to contain no elements matching \'.foo\'\n' +
850+
'\n' +
851+
'[\n' +
852+
' <div class="foo"></div> // should be removed\n' +
853+
']'
854+
);
855+
});
856+
857+
it('should fail when matching a NodeList', function () {
858+
var document = jsdom.jsdom('<!DOCTYPE html><html><body><div class="foo"></div><div class="foo"></div></body></html>');
859+
860+
expect(function () {
861+
expect(document, 'to contain no elements matching', '.foo');
862+
}, 'to throw', 'expected <!DOCTYPE html><html><head></head><body>......</body></html> to contain no elements matching \'.foo\'\n' +
863+
'\n' +
864+
'[\n' +
865+
' <div class="foo"></div>, // should be removed\n' +
866+
' <div class="foo"></div> // should be removed\n' +
867+
']'
868+
);
869+
});
870+
});
871+
837872
describe('diffing', function () {
838873
function parseHtmlElement(str) {
839874
return jsdom.jsdom('<!DOCTYPE html><html><body>' + str + '</body></html>').body.firstChild;

0 commit comments

Comments
 (0)