Skip to content

Commit f837408

Browse files
committed
test: add scenario for handleResponseType tgz
1 parent 8c9cffb commit f837408

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

Diff for: src/components/ActionBar/ActionBar.test.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ describe('<ActionBar /> component', () => {
4444
const ActionBar = require('./ActionBar').default;
4545
const wrapper = shallow(<ActionBar />);
4646
// FIXME: this only renders the DetailContextConsumer, thus
47-
// the wrapper will be
47+
// the wrapper will be always empty
4848
expect(wrapper.html()).toEqual('');
4949
});
5050

Diff for: src/utils/api.test.ts

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/* eslint-disable @typescript-eslint/no-object-literal-type-assertion */
2+
3+
import { handleResponseType } from '../../src/utils/api';
4+
5+
describe('api', () => {
6+
// no the best mock, but I'd look for a mock library to work with fetch in the future
7+
// @ts-ignore
8+
const headers: Headers = {
9+
// @ts-ignore
10+
get: () => [],
11+
};
12+
13+
describe('handleResponseType', () => {
14+
test('should test tgz scenario', async () => {
15+
const blob = new Blob(['foo']);
16+
const blobPromise = Promise.resolve<Blob>(blob);
17+
const response: Response = {
18+
url: 'http://localhost:8080/bootstrap/-/bootstrap-4.3.1.tgz',
19+
blob: () => blobPromise,
20+
ok: true,
21+
headers,
22+
} as Response;
23+
const handled = await handleResponseType(response);
24+
25+
expect(handled).toEqual([true, blob]);
26+
});
27+
});
28+
});

Diff for: src/utils/api.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import '../../types';
66
* @param {object} response
77
* @returns {promise}
88
*/
9-
function handleResponseType(response: Response): Promise<[boolean, Blob | string]> | Promise<void> {
9+
export function handleResponseType(response: Response): Promise<[boolean, Blob | string]> | Promise<void> {
1010
if (response.headers) {
1111
const contentType = response.headers.get('Content-Type') as string;
1212
if (contentType.includes('application/pdf')) {
@@ -21,7 +21,7 @@ function handleResponseType(response: Response): Promise<[boolean, Blob | string
2121
}
2222

2323
// unfortunatelly on download files there is no header available
24-
if (response.url && response.url.match(/.tgz/) !== null) {
24+
if (response.url && response.url.endsWith('.tgz') !== null) {
2525
return Promise.all([response.ok, response.blob()]);
2626
}
2727
}

0 commit comments

Comments
 (0)