Skip to content

Commit

Permalink
Update downloadReport.js
Browse files Browse the repository at this point in the history
  • Loading branch information
alb3rtino committed Oct 27, 2023
1 parent 9c7ec04 commit 515affc
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 7 deletions.
12 changes: 5 additions & 7 deletions src/util/downloadReport.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { SubmissionError } from 'redux-form';
import saveAs from 'file-saver';
import fetchWithDefaultOptions from './fetchWithDefaultOptions';

Expand All @@ -17,11 +16,10 @@ const downloadCredentials = (aggregatorId, format, okapi, httpHeaders) => {
}
)
.then((response) => {
if (response.status >= 400) {
throw new SubmissionError({
identifier: `Error ${response.status} retrieving credentials of aggregator`,
_error: 'Fetch credentials failed',
});
if (response.status !== 200) {
throw new Error(
`Received ${response.status} - ${response.statusText} while fetching credentials`
);
} else {
if (format === 'csv') {
return response.text();
Expand All @@ -33,7 +31,7 @@ const downloadCredentials = (aggregatorId, format, okapi, httpHeaders) => {
saveReport(aggregatorId, text, format);
})
.catch((err) => {
throw new Error('Error while downloading credentials. ' + err.message);
throw new Error('Error while downloading credentials: ' + err.message);
});
};

Expand Down
52 changes: 52 additions & 0 deletions src/util/downloadReport.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import saveAs from 'file-saver';
import { downloadCredentials } from './downloadReport';
import fetchWithDefaultOptions from './fetchWithDefaultOptions';

jest.mock('./fetchWithDefaultOptions');
jest.mock('file-saver');

const aggregatorId = '123';
global.URL.createObjectURL = jest.fn();
saveAs.mockImplementation(jest.fn());

describe('downloadCredentials && saveReport', () => {
it('download credentials in csv format', async () => {
const format = 'csv';
const content = 'csv content';

fetchWithDefaultOptions.mockResolvedValue({
status: 200,
text: jest.fn().mockResolvedValue(content),
});

await downloadCredentials(aggregatorId, format, {}, {});
expect(saveAs).toHaveBeenCalledWith(
new Blob([content], { type: format }),
`${aggregatorId}.${format}`
);
});

it('download credentials in xlsx format', async () => {
const format = 'xlsx';
const content = 'binary content';

fetchWithDefaultOptions.mockResolvedValue({
status: 200,
blob: jest.fn().mockResolvedValue(content),
});

await downloadCredentials(aggregatorId, format, {}, {});
expect(saveAs).toHaveBeenCalledWith(
new Blob([content], { type: format }),
`${aggregatorId}.${format}`
);
});

it('handle error while downloading credentials', async () => {
fetchWithDefaultOptions.mockResolvedValue({ status: 404, statusText: 'Not Found' });

await expect(downloadCredentials(aggregatorId, 'csv', {}, {})).rejects.toThrow(
/Error while downloading.*404 - Not Found.*/
);
});
});

0 comments on commit 515affc

Please sign in to comment.