2017-02-26 17:10:07 +05:30
|
|
|
import expect from 'unexpected';
|
|
|
|
import sinon from 'sinon';
|
|
|
|
|
|
|
|
import request from 'services/request';
|
|
|
|
import { InternalServerError } from 'services/request';
|
|
|
|
|
|
|
|
describe('services/request', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
sinon.stub(window, 'fetch').named('fetch');
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
window.fetch.restore();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('InternalServerError', () => {
|
|
|
|
it('should wrap fetch error', () => {
|
|
|
|
const resp = new TypeError('Fetch error');
|
|
|
|
|
|
|
|
fetch.returns(Promise.reject(resp));
|
|
|
|
|
|
|
|
return expect(request.get('/foo'), 'to be rejected')
|
|
|
|
.then((error) => {
|
|
|
|
expect(error, 'to be an', InternalServerError);
|
|
|
|
expect(error.originalResponse, 'to be undefined');
|
|
|
|
expect(error.message, 'to equal', resp.message);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should wrap json errors', () => {
|
|
|
|
const resp = new Response('bad resp format', {status: 200});
|
|
|
|
|
|
|
|
fetch.returns(Promise.resolve(resp));
|
|
|
|
|
|
|
|
return expect(request.get('/foo'), 'to be rejected')
|
|
|
|
.then((error) => {
|
|
|
|
expect(error, 'to be an', InternalServerError);
|
|
|
|
expect(error.originalResponse, 'to be', resp);
|
2017-02-26 17:26:35 +05:30
|
|
|
expect(error.message, 'to contain', 'Unexpected token');
|
2017-02-26 17:10:07 +05:30
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should wrap 5xx errors', () => {
|
|
|
|
const resp = new Response('{}', {status: 500});
|
|
|
|
|
|
|
|
fetch.returns(Promise.resolve(resp));
|
|
|
|
|
|
|
|
return expect(request.get('/foo'), 'to be rejected')
|
|
|
|
.then((error) => {
|
|
|
|
expect(error, 'to be an', InternalServerError);
|
|
|
|
expect(error.originalResponse, 'to be', resp);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2017-05-26 00:41:57 +05:30
|
|
|
|
|
|
|
describe('#buildQuery', () => {
|
|
|
|
it('should build query', () => {
|
|
|
|
const data = {
|
|
|
|
notSet: undefined,
|
2017-06-13 00:32:15 +05:30
|
|
|
notSet2: null,
|
2017-05-26 00:41:57 +05:30
|
|
|
numeric: 1,
|
|
|
|
complexString: 'sdfgs sdfg ',
|
|
|
|
positive: true,
|
|
|
|
negative: false
|
|
|
|
};
|
2017-06-13 00:32:15 +05:30
|
|
|
const expectedQs = 'notSet=¬Set2=&numeric=1&complexString=sdfgs%20sdfg%20&positive=1&negative=0';
|
2017-05-26 00:41:57 +05:30
|
|
|
|
|
|
|
expect(request.buildQuery(data), 'to equal', expectedQs);
|
|
|
|
});
|
|
|
|
});
|
2017-02-26 17:10:07 +05:30
|
|
|
});
|