Skip to content

Commit

Permalink
Implemented 'when parsed as HTML' assertion.
Browse files Browse the repository at this point in the history
  • Loading branch information
papandreou committed Apr 21, 2015
1 parent 129c6da commit dadca0a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
15 changes: 15 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -418,5 +418,20 @@ module.exports = {
}
this.shift(expect, queryResult, 1);
});

expect.addAssertion('string', 'when parsed as (html|HTML)', function (expect, subject) {
var htmlDocument;
if (typeof DOMParser !== 'undefined') {
htmlDocument = new DOMParser().parseFromString(subject, 'text/html');
} else if (typeof document !== 'undefined' && document.implementation && document.implementation.createHTMLDocument) {
htmlDocument = document.implementation.createHTMLDocument('');
htmlDocument.open();
htmlDocument.write(subject);
htmlDocument.close();
} else {
htmlDocument = require('jsdom').jsdom(subject);
}
return this.shift(expect, htmlDocument, 0);
});
}
};
11 changes: 11 additions & 0 deletions test/unexpected-dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -468,4 +468,15 @@ describe('unexpected-dom', function () {
);
});
});

describe('when parsed as HTML', function () {
it('should parse a string as a complete HTML document', function () {
var htmlSrc = '<!DOCTYPE html><html><body class="bar">foo</body></html>';
expect(htmlSrc, 'when parsed as HTML',
expect.it('to be a', 'HTMLDocument')
.and('to equal', jsdom.jsdom(htmlSrc))
.and('queried for first', 'body', 'to have attributes', { class: 'bar' })
);
});
});
});

0 comments on commit dadca0a

Please sign in to comment.