Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expect attribute to be absent #21

Merged
merged 2 commits into from
Jun 23, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,9 @@ module.exports = {
return topLevelExpect(attrs.style, 'to satisfy', expectedStyleObj);
}
} else if (expectedAttributeValue === true) {
expect(subject.hasAttribute(attributeName), 'to be true');
topLevelExpect(subject.hasAttribute(attributeName), 'to be true');
} else if (typeof expectedAttributeValue === 'undefined') {
topLevelExpect(subject.hasAttribute(attributeName), 'to be false');
} else {
return topLevelExpect(attributeValue, 'to satisfy', expectedAttributeValue);
}
Expand Down Expand Up @@ -615,7 +617,7 @@ module.exports = {
output
.sp()
.annotationBlock(function () {
if (promise) {
if (promise && typeof expectedValueByAttributeName[attributeName] !== 'undefined') {
this.append(promise.reason().getErrorMessage());
} else {
// onlyAttributes === true
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"mocha-lcov-reporter": "0.0.2",
"sinon": "1.14.1",
"uglifyjs": "^2.4.10",
"unexpected": "^8.0.0",
"unexpected": "^8.4.0",
"unexpected-sinon": "^6.1.3"
},
"dependencies": {
Expand Down
44 changes: 32 additions & 12 deletions test/unexpected-dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,27 @@ describe('unexpected-dom', function () {
'>Press me</button>'
);
});

describe('with the absence of an attribute asserted by providing undefined as the expected value', function () {
it('should succeed', function () {
this.body.innerHTML = '<button id="foo">Press me</button>';
expect(this.body.firstChild, 'to have attributes', { quux: undefined });
});

it('should fail with a diff', function () {
this.body.innerHTML = '<button id="foo" quux="baz">Press me</button>';
var el = this.body.firstChild;

expect(function () {
expect(el, 'to have attributes', { quux: undefined });
}, 'to throw',
'expected <button id="foo" quux="baz">Press me</button> to have attributes { quux: undefined }\n' +
'\n' +
'<button id="foo" quux="baz" // should be removed\n' +
'>Press me</button>'
);
});
});
});

describe('object comparison', function () {
Expand Down Expand Up @@ -762,7 +783,7 @@ describe('unexpected-dom', function () {
'<div foo="bar">\n' +
' hey // expected NodeList[ hey ] to satisfy [ \'there\' ]\n' +
' //\n' +
' // DOMNodeList[\n' +
' // NodeList[\n' +
' // hey // should equal \'there\'\n' +
' // // -hey\n' +
' // // +there\n' +
Expand All @@ -788,15 +809,14 @@ describe('unexpected-dom', function () {
' expected NodeList[ <div foo="bar" id="quux">foobar</div>, <div foo="quux">hey</div> ]\n' +
' to satisfy { 1: { attributes: { foo: \'bar\' } } }\n' +
'\n' +
' NodeList({\n' +
' 0: <div foo="bar" id="quux">...</div>,\n' +
' 1:\n' +
' <div foo="quux" // expected \'quux\' to satisfy \'bar\'\n' +
' //\n' +
' // -quux\n' +
' // +bar\n' +
' >hey</div>\n' +
' })'
' NodeList[\n' +
' <div foo="bar" id="quux">...</div>,\n' +
' <div foo="quux" // expected \'quux\' to satisfy \'bar\'\n' +
' //\n' +
' // -quux\n' +
' // +bar\n' +
' >hey</div>\n' +
' ]'
);
});
});
Expand Down Expand Up @@ -866,7 +886,7 @@ describe('unexpected-dom', 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' +
'DOMNodeList[\n' +
'NodeList[\n' +
' <div class="foo"></div> // should be removed\n' +
']'
);
Expand All @@ -879,7 +899,7 @@ describe('unexpected-dom', 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' +
'DOMNodeList[\n' +
'NodeList[\n' +
' <div class="foo"></div>, // should be removed\n' +
' <div class="foo"></div> // should be removed\n' +
']'
Expand Down