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 'when parsed as HTML' assertion. #10

Merged
merged 3 commits into from
Apr 23, 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
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,19 @@ expect(node, 'to [only] have attributes', {

**Queried for**

**When parsed as HTML**

Parses the subject (string) as an HTML document using the facilities available in the browser, with a fallback to jsdom, then delegates to the next assertion in the argument list:

```js
expect(
'<html><body><div class="foo"></div></body></html>',
'when parsed as HTML',
'queried for', 'div',
'to have attributes', { class: 'foo' }
);
```


License
-------
Expand Down
15 changes: 15 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -422,5 +422,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);
});
}
};
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@
"magicpen-prism": "^1.1.1",
"mocha": "^2.1.0",
"mocha-lcov-reporter": "0.0.2",
"sinon": "1.14.1",
"uglifyjs": "^2.4.10",
"unexpected": "^7.0.1"
"unexpected": "^7.0.1",
"unexpected-sinon": "5.1.2"
},
"dependencies": {
"array-changes": "1.0.1",
Expand Down
82 changes: 80 additions & 2 deletions test/unexpected-dom.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
/*global describe, it, beforeEach*/
/*global describe, it, beforeEach, afterEach*/
var unexpected = require('unexpected'),
unexpectedDom = require('../lib/index'),
sinon = require('sinon'),
jsdom = require('jsdom');

var expect = unexpected.clone().installPlugin(unexpectedDom);
var expect = unexpected.clone().installPlugin(require('unexpected-sinon')).installPlugin(unexpectedDom);
expect.output.installPlugin(require('magicpen-prism'));

expect.addAssertion('to inspect as [itself]', function (expect, subject, value) {
Expand Down Expand Up @@ -491,4 +492,81 @@ describe('unexpected-dom', function () {
);
});
});

describe('when parsed as HTML', function () {
var htmlSrc = '<!DOCTYPE html><html><body class="bar">foo</body></html>';
it('should parse a string as a complete HTML document', function () {
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' })
);
});

describe('when the DOMParser global is available', function () {
var originalDOMParser,
DOMParserSpy,
parseFromStringSpy;

beforeEach(function () {
originalDOMParser = global.DOMParser;
global.DOMParser = DOMParserSpy = sinon.spy(function () {
return {
parseFromString: parseFromStringSpy = sinon.spy(function (htmlString, contentType) {
return jsdom.jsdom(htmlString);
})
};
});
});
afterEach(function () {
global.DOMParser = originalDOMParser;
});

it('should use DOMParser to parse the document', function () {
expect(htmlSrc, 'when parsed as HTML', 'queried for first', 'body', 'to have text', 'foo');
expect(DOMParserSpy, 'was called once');
expect(DOMParserSpy, 'was called with');
expect(DOMParserSpy.calledWithNew(), 'to be true');
expect(parseFromStringSpy, 'was called once');
expect(parseFromStringSpy, 'was called with', htmlSrc, 'text/html');
});
});

describe('when the document global is available', function () {
var originalDocument,
createHTMLDocumentSpy,
mockDocument;

beforeEach(function () {
originalDocument = global.document;
global.document = {
implementation: {
createHTMLDocument: createHTMLDocumentSpy = sinon.spy(function () {
mockDocument = jsdom.jsdom(htmlSrc);
mockDocument.open = sinon.spy();
mockDocument.write = sinon.spy();
mockDocument.close = sinon.spy();
return mockDocument;
})
}
};
});
afterEach(function () {
global.document = originalDocument;
});

it('should use document.implementation.createHTMLDocument to parse the document', function () {
expect(htmlSrc, 'when parsed as HTML', 'queried for first', 'body', 'to have text', 'foo');
expect(createHTMLDocumentSpy, 'was called once');
expect(createHTMLDocumentSpy, 'was called with');
expect(mockDocument.open, 'was called once');
expect(mockDocument.write, 'was called with');
expect(mockDocument.write, 'was called once');
expect(mockDocument.write, 'was called with', htmlSrc);
expect(mockDocument.close, 'was called once');
expect(mockDocument.write, 'was called with');
expect([mockDocument.open, mockDocument.write, mockDocument.close], 'given call order');
});
});
});
});