2019-12-08 00:32:00 +05:30
|
|
|
import expect from 'app/test/unexpected';
|
2017-02-26 17:10:07 +05:30
|
|
|
import sinon from 'sinon';
|
|
|
|
|
2019-12-08 00:32:00 +05:30
|
|
|
import request from 'app/services/request';
|
|
|
|
import { InternalServerError, RequestAbortedError } from 'app/services/request';
|
2017-02-26 17:10:07 +05:30
|
|
|
|
|
|
|
describe('services/request', () => {
|
2020-05-24 04:38:24 +05:30
|
|
|
beforeEach(() => {
|
|
|
|
sinon.stub(window, 'fetch').named('fetch');
|
2019-11-27 14:33:32 +05:30
|
|
|
});
|
2017-02-26 17:10:07 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
afterEach(() => {
|
|
|
|
(window.fetch as any).restore();
|
|
|
|
});
|
2017-02-26 17:10:07 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
describe('InternalServerError', () => {
|
|
|
|
it('should wrap json errors', () => {
|
|
|
|
const resp = new Response('bad resp format', { status: 200 });
|
2017-02-26 17:10:07 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
(fetch as any).returns(Promise.resolve(resp));
|
2017-02-26 17:10:07 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
return expect(request.get('/foo'), 'to be rejected').then((error: InternalServerError) => {
|
|
|
|
expect(error, 'to be an', InternalServerError);
|
|
|
|
expect(error.originalResponse, 'to be', resp);
|
|
|
|
expect(error.message, 'to contain', 'Unexpected token');
|
|
|
|
});
|
|
|
|
});
|
2018-11-04 13:39:42 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
it('should wrap 5xx errors', () => {
|
|
|
|
const resp = new Response('{}', { status: 500 });
|
2018-11-04 13:39:42 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
(fetch as any).returns(Promise.resolve(resp));
|
|
|
|
|
|
|
|
return expect(request.get('/foo'), 'to be rejected').then((error: InternalServerError) => {
|
|
|
|
expect(error, 'to be an', InternalServerError);
|
|
|
|
expect(error.originalResponse, 'to be', resp);
|
|
|
|
});
|
|
|
|
});
|
2018-11-04 13:39:42 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
it('should wrap aborted errors', () => {
|
|
|
|
const resp = new Response('{}', { status: 0 });
|
2017-02-26 17:10:07 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
(fetch as any).returns(Promise.resolve(resp));
|
2017-02-26 17:10:07 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
return expect(request.get('/foo'), 'to be rejected').then((error: RequestAbortedError) => {
|
|
|
|
expect(error, 'to be an', RequestAbortedError);
|
|
|
|
// @ts-ignore
|
|
|
|
expect(error.error, 'to be', resp);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should wrap "Failed to fetch" errors', () => {
|
|
|
|
const resp = new TypeError('Failed to fetch');
|
|
|
|
|
|
|
|
(fetch as any).returns(Promise.resolve(resp));
|
|
|
|
|
|
|
|
return expect(request.get('/foo'), 'to be rejected').then((error: RequestAbortedError) => {
|
|
|
|
expect(error, 'to be an', RequestAbortedError);
|
|
|
|
expect(error.message, 'to be', resp.message);
|
|
|
|
// @ts-ignore
|
|
|
|
expect(error.error, 'to be', resp);
|
|
|
|
});
|
|
|
|
});
|
2017-02-26 17:10:07 +05:30
|
|
|
});
|
2020-05-24 04:38:24 +05:30
|
|
|
|
|
|
|
describe('#buildQuery', () => {
|
|
|
|
it('should build query', () => {
|
|
|
|
const data = {
|
|
|
|
notSet: undefined,
|
|
|
|
notSet2: null,
|
|
|
|
numeric: 1,
|
|
|
|
complexString: 'sdfgs sdfg ',
|
|
|
|
positive: true,
|
|
|
|
negative: false,
|
|
|
|
};
|
|
|
|
const expectedQs = 'notSet=¬Set2=&numeric=1&complexString=sdfgs%20sdfg%20&positive=1&negative=0';
|
|
|
|
|
|
|
|
expect(request.buildQuery(data), 'to equal', expectedQs);
|
|
|
|
});
|
2017-05-26 00:41:57 +05:30
|
|
|
});
|
2017-02-26 17:10:07 +05:30
|
|
|
});
|