Skip to content

Commit

Permalink
Stop using Sinon, we should just use a normal stub
Browse files Browse the repository at this point in the history
  • Loading branch information
sunesimonsen committed Apr 14, 2019
1 parent 206bc79 commit 1db2e03
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 86 deletions.
2 changes: 0 additions & 2 deletions karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ module.exports = function(config) {

files: [
'./node_modules/unexpected/unexpected.js',
'./node_modules/unexpected-sinon/lib/unexpected-sinon.js',
'./node_modules/sinon/pkg/sinon.js',
'./unexpected-dom.js',
'./test/common/browser.js',
'./build/test/index.spec.js'
Expand Down
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,10 @@
"nyc": "^13.1.0",
"offline-github-changelog": "^1.2.0",
"prettier": "~1.17.0",
"sinon": "^7.1.0",
"uglifyjs": "^2.4.11",
"unexpected": "^11.0.0-4",
"unexpected-documentation-site-generator": "^6.0.0",
"unexpected-markdown": "^3.0.0",
"unexpected-sinon": "^10.7.1"
"unexpected-markdown": "^3.0.0"
},
"dependencies": {
"extend": "^3.0.1",
Expand Down
1 change: 0 additions & 1 deletion test/common/browser.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/*global expect:true*/
/* eslint no-unused-vars: "off" */
expect = window.weknowhow.expect.clone();
expect.use(window.weknowhow.unexpectedSinon);
expect.use(window.unexpected.dom);
4 changes: 1 addition & 3 deletions test/common/node.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
/*global expect:true, jsdom:true, sinon:true*/
/*global expect:true, jsdom:true*/
/* eslint no-unused-vars: "off" */
expect = require('unexpected')
.clone()
.use(require('unexpected-sinon'))
.use(require('../../src/index'));
jsdom = require('jsdom');
sinon = require('sinon');
105 changes: 28 additions & 77 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, describe, it, beforeEach, afterEach, DOMParser:true*/
const isIe =
window.navigator &&
/Windows/.test(window.navigator.userAgent) &&
Expand Down Expand Up @@ -2533,22 +2533,20 @@ describe('unexpected-dom', () => {

describe('when the DOMParser global is available', () => {
const OriginalDOMParser = root.DOMParser;
const safeParseHtmlDocument =
typeof jsdom !== 'undefined'
? str => new jsdom.JSDOM(str).window.document
: str => new OriginalDOMParser().parseFromString(str, 'text/html');

let DOMParserSpy;
let parseFromStringSpy;
beforeEach(() => {
DOMParser = DOMParserSpy = sinon
.spy(() => ({
parseFromString: (parseFromStringSpy = sinon
.spy(htmlString => safeParseHtmlDocument(htmlString))
.named('parseFromString'))
}))
.named('DOMParser');
DOMParser = class DOMParser {
parseFromString(htmlString) {
return typeof jsdom !== 'undefined'
? new jsdom.JSDOM(htmlString).window.document
: new OriginalDOMParser().parseFromString(
htmlString,
'text/html'
);
}
};
});

afterEach(() => {
DOMParser = OriginalDOMParser;
});
Expand All @@ -2562,46 +2560,29 @@ describe('unexpected-dom', () => {
'to have text',
'foo'
);
expect(
[DOMParserSpy, parseFromStringSpy],
'to have calls satisfying',
() => {
// eslint-disable-next-line no-new
new DOMParserSpy();
parseFromStringSpy(htmlSrc, 'text/html');
}
);
});
});

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

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');

DOMParser = undefined; // force the "implementation" path
originalDocument = root.document;

// eslint-disable-next-line no-global-assign
document = {
implementation: {
createHTMLDocument: (createHTMLDocumentSpy = sinon
.spy(() => {
return mockDocument;
})
.named('createHTMLDocument'))
createHTMLDocument() {
return parseHtmlDocument(htmlSrc);
}
}
};
});

afterEach(() => {
DOMParser = OriginalDOMParser;
DOMParser = originalDOMParser;
// eslint-disable-next-line no-global-assign
document = originalDocument;
});
Expand All @@ -2615,21 +2596,6 @@ describe('unexpected-dom', () => {
'to have text',
'foo'
);
expect(
[
createHTMLDocumentSpy,
mockDocument.open,
mockDocument.write,
mockDocument.close
],
'to have calls satisfying',
() => {
createHTMLDocumentSpy('');
mockDocument.open();
mockDocument.write(htmlSrc);
mockDocument.close();
}
);
});
});
}
Expand Down Expand Up @@ -2687,24 +2653,18 @@ describe('unexpected-dom', () => {

describe('when the DOMParser global is available', () => {
const OriginalDOMParser = root.DOMParser;
const safeParseXmlDocument =
typeof jsdom !== 'undefined'
? str =>
new jsdom.JSDOM(str, { contentType: 'text/xml' }).window.document
: str =>
new OriginalDOMParser().parseFromString(str, 'application/xml');

let DOMParserSpy;
let parseFromStringSpy;

beforeEach(() => {
DOMParser = DOMParserSpy = sinon
.spy(() => ({
parseFromString: (parseFromStringSpy = sinon
.spy(xmlString => safeParseXmlDocument(xmlString))
.named('parseFromString'))
}))
.named('DOMParser');
DOMParser = class DOMParser {
parseFromString(str) {
return typeof jsdom !== 'undefined'
? new jsdom.JSDOM(str, { contentType: 'text/xml' }).window
.document
: new OriginalDOMParser().parseFromString(str, 'application/xml');
}
};
});

afterEach(() => {
DOMParser = OriginalDOMParser;
});
Expand All @@ -2718,15 +2678,6 @@ describe('unexpected-dom', () => {
'to have text',
'foo'
);
expect(
[DOMParserSpy, parseFromStringSpy],
'to have calls satisfying',
() => {
// eslint-disable-next-line no-new
new DOMParserSpy();
parseFromStringSpy(xmlSrc, 'text/xml');
}
);
});
});
});
Expand Down

0 comments on commit 1db2e03

Please sign in to comment.