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

To have attributes object #1

Merged
merged 4 commits into from
Mar 5, 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
18 changes: 13 additions & 5 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ function getAttributes(elm) {
var result = {};

for (var i = 0; i < attrs.length; i += 1) {
result[attrs[i].name] = attrs[i].value;
result[attrs[i].name] = attrs[i].value || true;
}

return result;
Expand All @@ -26,7 +26,11 @@ function stringifyElement(elm) {
var attrs = getCanonicalAttributes(elm);

openTag.push(Object.keys(attrs).map(function (key) {
return key + '="' + attrs[key] + '"';
if (typeof attrs[key] === 'boolean') {
return key;
} else {
return key + '="' + attrs[key] + '"';
}
}).join(' '));

if (elm.children.length) {
Expand All @@ -42,6 +46,10 @@ module.exports = {
expect.addType({
name: 'HTMLElement',
identify: function (obj) {
if (!obj) {
return false;
}

if ('HTMLElement' in this) {
return obj instanceof HTMLElement;
}
Expand Down Expand Up @@ -77,10 +85,10 @@ module.exports = {
} else if (Array.isArray(cmp)) {
expect(attrs, 'to [only] have keys', cmp);
} else {
expect(attrs, '');
}

this.flags.exhaustively = this.flags.only;

expect(attrs, 'to [exhaustively] satisfy', cmp);
}
});
}
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@
"magicpen-prism": "^1.1.1",
"mocha": "^2.1.0",
"mocha-lcov-reporter": "0.0.2",
"unexpected": "^5.10.0"
"unexpected": "^6.0.7"
}
}
53 changes: 49 additions & 4 deletions test/unexpected-dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ describe('unexpected-dom', function () {
expect(function () {
expect(el, 'to only have attributes', 'id');
}, 'to throw exception', function (err) {
expect(err.output.toString(), 'to be', 'expected <button class="bar" data-info="baz" disabled="" id="foo"/> to only have attributes \'id\'');
expect(err.output.toString(), 'to be', 'expected <button class="bar" data-info="baz" disabled id="foo"/> to only have attributes \'id\'');
});
});

Expand All @@ -70,7 +70,7 @@ describe('unexpected-dom', function () {
expect(function () {
expect(el, 'to have attributes', 'id', 'foo');
}, 'to throw exception', function (err) {
expect(err.output.toString(), 'to be', 'expected <button class="bar" data-info="baz" disabled="" id="foo"/> to have attributes \'id\', \'foo\'');
expect(err.output.toString(), 'to be', 'expected <button class="bar" data-info="baz" disabled id="foo"/> to have attributes \'id\', \'foo\'');
});
});
});
Expand All @@ -89,7 +89,7 @@ describe('unexpected-dom', function () {
expect(function () {
expect(el, 'to only have attributes', ['id']);
}, 'to throw exception', function (err) {
expect(err.output.toString(), 'to be', 'expected <button class="bar" data-info="baz" disabled="" id="foo"/> to only have attributes [ \'id\' ]');
expect(err.output.toString(), 'to be', 'expected <button class="bar" data-info="baz" disabled id="foo"/> to only have attributes [ \'id\' ]');
});
});

Expand All @@ -106,10 +106,55 @@ describe('unexpected-dom', function () {
expect(function () {
expect(el, 'to have attributes', ['id', 'foo']);
}, 'to throw exception', function (err) {
expect(err.output.toString(), 'to be', 'expected <button class="bar" data-info="baz" disabled="" id="foo"/> to have attributes [ \'id\', \'foo\' ]');
expect(err.output.toString(), 'to be', 'expected <button class="bar" data-info="baz" disabled id="foo"/> to have attributes [ \'id\', \'foo\' ]');
});
});
});

describe('object comparison', function () {
it('should match exact object', function () {
this.body.innerHTML = '<button id="foo" class="bar" data-info="baz" disabled>Press me</button>';

expect(this.body.firstChild, 'to only have attributes', {
id: 'foo',
'class': 'bar',
'data-info': 'baz',
disabled: true
});
});

it('should fail on exact object not satisfied', function () {
this.body.innerHTML = '<button id="foo" class="bar" data-info="baz" disabled>Press me</button>';
var el = this.body.firstChild;

expect(function () {
expect(el, 'to only have attributes', {
id: 'foo'
});
}, 'to throw exception', /^expected <button class="bar" data-info="baz" disabled id="foo"\/> to only have attributes/);
});

it('should match partial object', function () {
this.body.innerHTML = '<button id="foo" class="bar" data-info="baz" disabled>Press me</button>';

expect(this.body.firstChild, 'to have attributes', {
id: 'foo',
'class': 'bar'
});
});

it('should fail on partial object not satisfied', function () {
this.body.innerHTML = '<button id="foo" class="bar" data-info="baz" disabled>Press me</button>';
var el = this.body.firstChild;

expect(function () {
expect(el, 'to have attributes', {
id: 'foo',
foo: 'bar'
});
}, 'to throw exception', /expected <button class="bar" data-info="baz" disabled id="foo"\/> to have attributes/);
});
});
});

});