Last active
November 23, 2015 04:40
-
-
Save dominics/423f7048ecb60d9db79b to your computer and use it in GitHub Desktop.
Mocha + Sinon FakeServer unit test, no browser required, runs entirely in node
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sinon from 'sinon'; | |
import jsdom from 'jsdom'; | |
global.document = jsdom.jsdom('<!doctype html><html><body></body></html>'); | |
global.window = document.defaultView; | |
global.sinon = sinon; | |
// because otherwise sinon will try to use window.XDomainRequest and fail | |
sinon.xhr.supportsCORS = true; | |
// because sinon refuses to look at window.XMLHttpRequest, and insists it's in global scope | |
global.XMLHttpRequest = global.window.XMLHttpRequest; | |
// [...] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe('client api class Setting', function() { | |
before(() => { | |
this.server = sinon.fakeServer.create(); | |
this.server.autoRespond = true; | |
this.server.respondWith('/api/setting/all', [ | |
200, | |
{ 'Content-Type': 'application/json' }, | |
'{"type": "setting", "data": [{ "id": "foo", "value": true }]}', | |
]); | |
this.setting = new Setting(); | |
}); | |
describe('.refresh()', () => { | |
it('eventually returns an array of settings', () => { | |
return expect(this.setting.refresh()).to.eventually.be.instanceOf(Array); | |
}); | |
}); | |
after(() => { | |
this.server.restore(); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment