/**
* Test the request function
*/
import request from '../request';
import sinon from 'sinon';
import expect from 'expect';
describe('request', () => {
// Before each test, stub the fetch function
beforeEach(() => {
sinon.stub(window, 'fetch');
});
// After each test, restore the fetch function
afterEach(() => {
window.fetch.restore();
});
describe('stubbing successful response', () => {
// Before each test, pretend we got a successful response
beforeEach(() => {
const res = new Response('{"hello":"world"}', {
status: 200,
headers: {
'Content-type': 'application/json',
},
});
window.fetch.returns(Promise.resolve(res));
});
it('should format the response correctly', (done) => {
request('/thisurliscorrect')
.catch(done)
.then((json) => {
expect(json.data.hello).toEqual('world');
done();
});
});
});
1) request stubbing successful response should format the response correctly:
Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test.
import 'babel-polyfill';
import fetchPonyFill from 'fetch-ponyfill'; // module "whatwg-fetch" is browser-only
import jsdom from 'jsdom';
import chai from 'chai';
import chaiEnzyme from 'chai-enzyme';
chai.use(chaiEnzyme());
// setup the simplest document possible
const doc = jsdom.jsdom('<!doctype html><html><body></body></html>');
// get the window object out of the document
const win = doc.defaultView;
// set globals for mocha that make access to document and window feel
// natural in the test environment
global.document = doc;
global.window = win;
global.navigator = window.navigator;
// Fetch API
Object.assign(global, fetchPonyFill());
// Take all properties of the window object and also attach it to the
// mocha global object
// @see https://github.com/rstacruz/mocha-jsdom/blob/master/index.js#L80
function propagateToGlobal(window) {
Object
.keys(window)
.forEach(key => {
if ({}.hasOwnProperty.call(window, key)) {
if (typeof global[key] === 'undefined') {
global[key] = window[key];
}
}
});
}
propagateToGlobal(window);