Skip to content

Commit

Permalink
Add 'to have attributes' assertion
Browse files Browse the repository at this point in the history
  • Loading branch information
Munter committed Mar 3, 2015
1 parent 2745695 commit 8abcb6f
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 15 deletions.
10 changes: 10 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,15 @@ module.exports = {
return diff(stringifyElement(actual), stringifyElement(expected));
}
});

expect.addAssertion('HTMLElement', ['[not] to [only] have (attribute|attributes)', '[not] to have (attribute|attributes)'], function (expect, subject, cmp) {
var attrs = getAttributes(subject);

if (typeof cmp === 'string') {
expect(attrs, '[not] to [only] have keys', Array.prototype.slice.call(arguments, 2));
} else if (Array.isArray(cmp)) {
expect(attrs, '[not] to have keys', cmp);
}
});
}
};
97 changes: 82 additions & 15 deletions test/unexpected-dom.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*global describe, it*/
/*global describe, it, beforeEach*/
var unexpected = require('unexpected'),
unexpectedDom = require('../lib/index'),
jsdom = require('jsdom');
Expand All @@ -7,22 +7,89 @@ var expect = unexpected.clone().installPlugin(unexpectedDom);
expect.output.installPlugin(require('magicpen-prism'));

describe('unexpected-dom', function () {
beforeEach(function (done) {
var self = this;
jsdom.env(' ', function (err, window) {
self.window = window;
self.document = window.document;
self.body = window.document.body;

done();
});
});

it('should consider two DOM elements equal when they are of same type and have same attributes', function () {
jsdom.env('<h1>Hello world</h1>', function (err, window) {
var document = window.document;

var el1 = document.createElement('h1');
var el2 = document.createElement('h1');
var el3 = document.createElement('h1');
el3.id = 'el3';
var paragraph = document.createElement('p');

expect(el1, 'to be', el1);
expect(el1, 'not to be', el2);
expect(el1, 'to equal', el2);
expect(el1, 'not to equal', el3);
expect(el1, 'not to equal', paragraph);
var document = this.document;

var el1 = document.createElement('h1');
var el2 = document.createElement('h1');
var el3 = document.createElement('h1');
el3.id = 'el3';
var paragraph = document.createElement('p');

expect(el1, 'to be', el1);
expect(el1, 'not to be', el2);
expect(el1, 'to equal', el2);
expect(el1, 'not to equal', el3);
expect(el1, 'not to equal', paragraph);
});

it('should to things', function () {
//expect(this.document.createElement('p'), 'to match', '<p />');
});

describe('to have attributes', function () {
describe('argument comparison', function () {
it('should match exact arguments', 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', 'class', 'data-info', 'disabled');
});

it('should fail on exact arguments not met', 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');
}, 'to throw exception', function (err) {
expect(err.output.toString(), 'to match', 'expected <button class="bar" data-info="baz" disabled="" id="foo"/> to only have attributes \'id\'');
});
});

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

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

it('should fail on partial arguments not met', 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');
}, 'to throw exception', function (err) {
expect(err.output.toString(), 'to match', 'expected <button class="bar" data-info="baz" disabled="" id="foo"/> to have attributes \'id\', \'foo\'');
});
});

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

expect(this.body.firstChild, 'not to have attributes', 'foo', 'bar');
});

it('should fail on negative partial arguments met', 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, 'not to have attributes', 'id');
}, 'to throw exception', function (err) {
expect(err.output.toString(), 'to match', 'expected <button class="bar" data-info="baz" disabled="" id="foo"/> not to have attributes \'id\'');
});
});
});
});

Expand Down

0 comments on commit 8abcb6f

Please sign in to comment.