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

Browser tests #246

Merged
merged 11 commits into from
Jan 8, 2019
Prev Previous commit
Next Next commit
Repair document global .implementation test.
This test appears to have pre-existing problems but endure it works.

It appears however that it cannot function in the browser because
the DOMImplementation property cannot be overridden - thus skip it
in that environment.
alexjeffburke committed Jan 6, 2019
commit a4fa9b85b2e90d0c0ac324c9a3410c5398321905
101 changes: 54 additions & 47 deletions test/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*global expect, jsdom, sinon, describe, it, beforeEach, afterEach, DOMParser:true*/
/*global expect, jsdom, sinon, describe, it, beforeEach, afterEach, document:true, DOMParser:true*/
expect.addAssertion(
'<any> to inspect as <string>',
(expect, subject, value) => {
@@ -2528,55 +2528,62 @@ describe('unexpected-dom', () => {
});
});

describe('when the document global is available', () => {
let originalDocument, createHTMLDocumentSpy, mockDocument;
if (typeof jsdom !== 'undefined') {
describe('when the document global is available', () => {
const OriginalDOMParser = root.DOMParser;
let originalDocument, createHTMLDocumentSpy, mockDocument;

beforeEach(() => {
originalDocument = global.document;
global.document = {
implementation: {
createHTMLDocument: (createHTMLDocumentSpy = sinon
.spy(() => {
mockDocument = new jsdom.JSDOM(htmlSrc).window.document;
mockDocument.open = sinon.spy().named('document.open');
mockDocument.write = sinon.spy().named('document.write');
mockDocument.close = sinon.spy().named('document.close');
return mockDocument;
})
.named('createHTMLDocument'))
}
};
});
afterEach(() => {
global.document = originalDocument;
});
beforeEach(() => {
mockDocument = parseHtmlDocument(htmlSrc);
mockDocument.open = sinon.spy().named('document.open');
mockDocument.write = sinon.spy().named('document.write');
mockDocument.close = sinon.spy().named('document.close');

it('should use document.implementation.createHTMLDocument to parse the document', () => {
expect(
htmlSrc,
'when parsed as HTML',
'queried for first',
'body',
'to have text',
'foo'
);
expect(
[
createHTMLDocumentSpy,
mockDocument.open,
mockDocument.write,
mockDocument.close
],
'to have calls satisfying',
() => {
createHTMLDocumentSpy('');
mockDocument.open();
mockDocument.write(htmlSrc);
mockDocument.close();
}
);
DOMParser = undefined; // force the "implementation" path
originalDocument = root.document;

document = {
implementation: {
createHTMLDocument: (createHTMLDocumentSpy = sinon
.spy(() => {
return mockDocument;
})
.named('createHTMLDocument'))
}
};
});
afterEach(() => {
DOMParser = OriginalDOMParser;
document = originalDocument;
});

it('should use document.implementation.createHTMLDocument to parse the document', () => {
expect(
htmlSrc,
'when parsed as HTML',
'queried for first',
'body',
'to have text',
'foo'
);
expect(
[
createHTMLDocumentSpy,
mockDocument.open,
mockDocument.write,
mockDocument.close
],
'to have calls satisfying',
() => {
createHTMLDocumentSpy('');
mockDocument.open();
mockDocument.write(htmlSrc);
mockDocument.close();
}
);
});
});
});
}
});

describe('when parsed as XML', () => {