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

Implemented 'to have text' assertion. #11

Merged
merged 1 commit into from
Apr 22, 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
4 changes: 4 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,10 @@ module.exports = {
}
});

expect.addAssertion('HTMLElement', 'to have text', function (expect, subject, value) {
return expect(subject.textContent, 'to satisfy', value);
});

expect.addAssertion(['HTMLDocument', 'HTMLElement'], 'queried for [first]', function (expect, subject, value) {
var queryResult;
if (this.flags.first) {
Expand Down
31 changes: 27 additions & 4 deletions test/unexpected-dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@ expect.addAssertion('to inspect as [itself]', function (expect, subject, value)
});

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

done();
});
Expand Down Expand Up @@ -75,8 +76,6 @@ describe('unexpected-dom', function () {
});

it('should consider two DOM elements equal when they are of same type and have same attributes', function () {
var document = this.document;

var el1 = document.createElement('h1');
var el2 = document.createElement('h1');
var el3 = document.createElement('h1');
Expand All @@ -94,6 +93,30 @@ describe('unexpected-dom', function () {
//expect(this.document.createElement('p'), 'to match', '<p />');
});

describe('to have text', function () {
it('should succeed', function () {
document.body.innerHTML = '<div>foo</div>';
return expect(document.body, 'to have text', 'foo');
});

it('should fail with a diff', function () {
document.body.innerHTML = '<div>foo</div>';
expect(function () {
expect(document.body, 'to have text', 'bar');
}, 'to throw',
'expected <body><div>foo</div></body> to have text \'bar\'\n' +
'\n' +
'-foo\n' +
'+bar'
);
});

it('should use "to satisfy" semantics', function () {
document.body.innerHTML = '<div>foo</div>';
return expect(document.body, 'to have text', /fo/);
});
});

describe('to have attributes', function () {
describe('argument comparison', function () {
it('should match exact arguments', function () {
Expand Down