Skip to content

Commit

Permalink
fix(utils): remove circular notifications dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
Hyperkid123 committed Oct 10, 2024
1 parent b376233 commit 85edd2c
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 55 deletions.
25 changes: 0 additions & 25 deletions packages/utils/src/useExportPDF/useExportPDF.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import useExportPDF from './useExportPDF';
import { addNotification } from '@redhat-cloud-services/frontend-components-notifications/redux';
import { pdfGeneratorURL } from './useExportPDF';
jest.mock('@redhat-cloud-services/frontend-components-notifications/redux', () => ({
...jest.requireActual('@redhat-cloud-services/frontend-components-notifications/redux'),
addNotification: jest.fn(() => ({})),
}));

window.URL.createObjectURL = jest.fn();
global.fetch = jest.fn();
Expand All @@ -20,25 +15,5 @@ describe('useExportPDF', () => {
headers: { 'Content-Type': 'application/json' },
method: 'POST',
});
expect(addNotification).toHaveBeenCalledWith({
description: 'Once complete, your download will start automatically.',
title: 'Preparing export',
variant: 'info',
});
expect(addNotification).toHaveBeenCalledWith({
variant: 'success',
title: 'Downloading export',
});
});
it('Should fail to download PDF with notification', async () => {
global.fetch.mockReturnValueOnce(Promise.reject('error'));

const exportPDF = useExportPDF('vulnerability', dispatch);
await exportPDF('executiveReport', 'vulnerability-test-export', { someRequestPayload: 'some value' });
expect(addNotification).toHaveBeenCalledWith({
description: 'Reinitiate this export to try again.',
title: 'Couldn’t download export',
variant: 'danger',
});
});
});
33 changes: 3 additions & 30 deletions packages/utils/src/useExportPDF/useExportPDF.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
//@ts-ignore
import { addNotification } from '@redhat-cloud-services/frontend-components-notifications/redux';

const CRC_PDF_GENERATE_API = '/api/crc-pdf-generator/v1/generate';
export const pdfGeneratorURL = new URL(CRC_PDF_GENERATE_API, window.location.origin);
const fetchPDF = (service: string, template: string, params: Record<string, unknown>) => {
Expand Down Expand Up @@ -30,35 +27,11 @@ const renderPDF = (pdfBlob: Blob, fileName = 'new-report') => {

// Hook to provide a function that request pdf-generator service to generate report blob
// and convert returned blob into pdf
const usePDFExport = (service: string, dispatch: any) => {
const usePDFExport = (service: string) => {
const exportPDF = async (template: string, filename: string, exportSettings: Record<string, unknown>) => {
dispatch(
addNotification({
variant: 'info',
title: 'Preparing export',
description: 'Once complete, your download will start automatically.',
})
);
try {
const pdfBlob = await fetchPDF(service, template, exportSettings);
renderPDF(pdfBlob, filename);
dispatch(
addNotification({
variant: 'success',
title: 'Downloading export',
})
);
} catch (e) {
dispatch(
addNotification({
variant: 'danger',
title: 'Couldn’t download export',
description: 'Reinitiate this export to try again.',
})
);
}
const pdfBlob = await fetchPDF(service, template, exportSettings);
renderPDF(pdfBlob, filename);
};

return exportPDF;
};

Expand Down

0 comments on commit 85edd2c

Please sign in to comment.