test: add scenario for handleResponseType tgz

This commit is contained in:
Juan Picado @jotadeveloper 2019-07-29 08:42:37 +02:00
parent 8c9cffbc6a
commit f8374084b5
No known key found for this signature in database
GPG Key ID: 15AA875EF3768142
3 changed files with 31 additions and 3 deletions

View File

@ -44,7 +44,7 @@ describe('<ActionBar /> component', () => {
const ActionBar = require('./ActionBar').default; const ActionBar = require('./ActionBar').default;
const wrapper = shallow(<ActionBar />); const wrapper = shallow(<ActionBar />);
// FIXME: this only renders the DetailContextConsumer, thus // FIXME: this only renders the DetailContextConsumer, thus
// the wrapper will be // the wrapper will be always empty
expect(wrapper.html()).toEqual(''); expect(wrapper.html()).toEqual('');
}); });

28
src/utils/api.test.ts Normal file
View File

@ -0,0 +1,28 @@
/* eslint-disable @typescript-eslint/no-object-literal-type-assertion */
import { handleResponseType } from '../../src/utils/api';
describe('api', () => {
// no the best mock, but I'd look for a mock library to work with fetch in the future
// @ts-ignore
const headers: Headers = {
// @ts-ignore
get: () => [],
};
describe('handleResponseType', () => {
test('should test tgz scenario', async () => {
const blob = new Blob(['foo']);
const blobPromise = Promise.resolve<Blob>(blob);
const response: Response = {
url: 'http://localhost:8080/bootstrap/-/bootstrap-4.3.1.tgz',
blob: () => blobPromise,
ok: true,
headers,
} as Response;
const handled = await handleResponseType(response);
expect(handled).toEqual([true, blob]);
});
});
});

View File

@ -6,7 +6,7 @@ import '../../types';
* @param {object} response * @param {object} response
* @returns {promise} * @returns {promise}
*/ */
function handleResponseType(response: Response): Promise<[boolean, Blob | string]> | Promise<void> { export function handleResponseType(response: Response): Promise<[boolean, Blob | string]> | Promise<void> {
if (response.headers) { if (response.headers) {
const contentType = response.headers.get('Content-Type') as string; const contentType = response.headers.get('Content-Type') as string;
if (contentType.includes('application/pdf')) { if (contentType.includes('application/pdf')) {
@ -21,7 +21,7 @@ function handleResponseType(response: Response): Promise<[boolean, Blob | string
} }
// unfortunatelly on download files there is no header available // unfortunatelly on download files there is no header available
if (response.url && response.url.match(/.tgz/) !== null) { if (response.url && response.url.endsWith('.tgz') !== null) {
return Promise.all([response.ok, response.blob()]); return Promise.all([response.ok, response.blob()]);
} }
} }