mirror of
https://github.com/SomboChea/ui
synced 2024-12-25 05:34:26 +07:00
test: add scenario for handleResponseType tgz
This commit is contained in:
parent
8c9cffbc6a
commit
f8374084b5
@ -44,7 +44,7 @@ describe('<ActionBar /> component', () => {
|
||||
const ActionBar = require('./ActionBar').default;
|
||||
const wrapper = shallow(<ActionBar />);
|
||||
// FIXME: this only renders the DetailContextConsumer, thus
|
||||
// the wrapper will be
|
||||
// the wrapper will be always empty
|
||||
expect(wrapper.html()).toEqual('');
|
||||
});
|
||||
|
||||
|
28
src/utils/api.test.ts
Normal file
28
src/utils/api.test.ts
Normal 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]);
|
||||
});
|
||||
});
|
||||
});
|
@ -6,7 +6,7 @@ import '../../types';
|
||||
* @param {object} response
|
||||
* @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) {
|
||||
const contentType = response.headers.get('Content-Type') as string;
|
||||
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
|
||||
if (response.url && response.url.match(/.tgz/) !== null) {
|
||||
if (response.url && response.url.endsWith('.tgz') !== null) {
|
||||
return Promise.all([response.ok, response.blob()]);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user