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

Add support for DocumentFragments #18

Merged
merged 3 commits into from
May 15, 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
36 changes: 31 additions & 5 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,17 @@ module.exports = {
}
});

expect.addType({
name: 'DOMDocumentFragment',
base: 'DOMDocument',
identify: function (obj) {
return obj && obj.nodeType === 11; // In jsdom, documentFragment.toString() does not return [object DocumentFragment]
},
inspect: function (documentFragment, depth, output, inspect) {
output.text('DocumentFragment[').append(inspect(documentFragment.childNodes, depth)).text(']');
}
});

expect.addType({
name: 'DOMElement',
base: 'DOMNode',
Expand Down Expand Up @@ -728,24 +739,39 @@ module.exports = {
return expect(matchesSelector(subject, value), 'to be', (this.flags.not ? false : true));
});

expect.addAssertion('string', 'when parsed as (html|HTML)', function (expect, subject) {
expect.addAssertion('string', 'when parsed as (html|HTML) [fragment]', function (expect, subject) {
this.errorMode = 'nested';
var htmlSource = subject;
if (this.flags.fragment) {
htmlSource = '<html><head></head><body>' + htmlSource + '</body></html>';
}
var htmlDocument;
if (typeof DOMParser !== 'undefined') {
htmlDocument = new DOMParser().parseFromString(subject, 'text/html');
htmlDocument = new DOMParser().parseFromString(htmlSource, 'text/html');
} else if (typeof document !== 'undefined' && document.implementation && document.implementation.createHTMLDocument) {
htmlDocument = document.implementation.createHTMLDocument('');
htmlDocument.open();
htmlDocument.write(subject);
htmlDocument.write(htmlSource);
htmlDocument.close();
} else {
try {
htmlDocument = require('jsdom').jsdom(subject);
htmlDocument = require('jsdom').jsdom(htmlSource);
} catch (err) {
throw new Error('The assertion `' + this.testDescription + '` was run outside a browser, but could not find the `jsdom` module. Please npm install jsdom to make this work.');
}
}
return this.shift(expect, htmlDocument, 0);
if (this.flags.fragment) {
var body = htmlDocument.body;
var documentFragment = htmlDocument.createDocumentFragment();
if (body) {
for (var i = 0 ; i < body.childNodes.length ; i += 1) {
documentFragment.appendChild(body.childNodes[i].cloneNode(true));
}
}
return this.shift(expect, documentFragment, 0);
} else {
return this.shift(expect, htmlDocument, 0);
}
});

expect.addAssertion('string', 'when parsed as (xml|XML)', function (expect, subject) {
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": "7.1.0",
"unexpected": "7.3.0",
"unexpected-sinon": "5.1.2"
},
"dependencies": {
Expand Down
15 changes: 15 additions & 0 deletions test/unexpected-dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -1029,6 +1029,21 @@ describe('unexpected-dom', function () {
);
});

describe('with the "fragment" flag', function () {
it('should return a DocumentFragment instance', function () {
expect('<div>foo</div><div>bar</div>', 'when parsed as HTML fragment', 'to satisfy',
expect.it('to be a', 'DOMDocumentFragment')
.and('to satisfy', {
children: [
{ name: 'div', children: [ 'foo' ] },
{ name: 'div', children: [ 'bar' ] }
]
}
)
);
});
});

it('should fail when the next assertion fails', function () {
expect(function () {
expect(htmlSrc, 'when parsed as HTML', 'queried for first', 'body', 'to have attributes', { class: 'quux' });
Expand Down