Skip to content

Commit

Permalink
fix: export vulnerabilities as xls using export-from-json
Browse files Browse the repository at this point in the history
Signed-off-by: Andreea-Lupu <[email protected]>
  • Loading branch information
Andreea-Lupu committed Jan 16, 2024
1 parent 12b474e commit 51794f1
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 37 deletions.
23 changes: 1 addition & 22 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@
"react-dom": "^17.0.2",
"react-router-dom": "^6.2.1",
"react-sticky-el": "^2.0.9",
"web-vitals": "^2.1.3",
"xlsx": "^0.18.5"
"web-vitals": "^2.1.3"
},
"devDependencies": {
"@babel/plugin-proposal-private-property-in-object": "^7.16.7",
Expand Down
7 changes: 1 addition & 6 deletions src/__tests__/TagPage/VulnerabilitiesDetails.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import VulnerabilitiesDetails from 'components/Tag/Tabs/VulnerabilitiesDetails';
import React from 'react';
import { MemoryRouter } from 'react-router-dom';

jest.mock('xlsx');

const StateVulnerabilitiesWrapper = () => {
return (
<MockThemeProvider>
Expand Down Expand Up @@ -561,9 +559,6 @@ describe('Vulnerabilties page', () => {
});

it('should allow export of vulnerabilities list', async () => {
const xlsxMock = jest.createMockFromModule('xlsx');
xlsxMock.writeFile = jest.fn();

jest
.spyOn(api, 'get')
.mockResolvedValueOnce({ status: 200, data: { data: mockCVEList } })
Expand All @@ -580,7 +575,7 @@ describe('Vulnerabilties page', () => {
await fireEvent.click(exportAsCSVBtn);
expect(await screen.findByTestId('export-csv-menuItem')).not.toBeInTheDocument();
fireEvent.click(downloadBtn[0]);
const exportAsExcelBtn = screen.getByText(/MS Excel/i);
const exportAsExcelBtn = screen.getByText(/xls/i);
expect(exportAsExcelBtn).toBeInTheDocument();
await fireEvent.click(exportAsExcelBtn);
expect(await screen.findByTestId('export-excel-menuItem')).not.toBeInTheDocument();
Expand Down
11 changes: 4 additions & 7 deletions src/components/Tag/Tabs/VulnerabilitiesDetails.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import { EXPLORE_PAGE_SIZE } from 'utilities/paginationConstants';
import SearchIcon from '@mui/icons-material/Search';
import DownloadIcon from '@mui/icons-material/Download';

import * as XLSX from 'xlsx';
import exportFromJSON from 'export-from-json';

import VulnerabilitiyCard from '../../Shared/VulnerabilityCard';
Expand Down Expand Up @@ -179,12 +178,10 @@ function VulnerabilitiesDetails(props) {
};

const handleOnExportExcel = () => {
const wb = XLSX.utils.book_new(),
ws = XLSX.utils.json_to_sheet(allCveData);

XLSX.utils.book_append_sheet(wb, ws, name + '_' + tag);
const fileName = `${name}:${tag}-vulnerabilities`;
const exportType = exportFromJSON.types.xls;

XLSX.writeFile(wb, `${name}:${tag}-vulnerabilities.xlsx`);
exportFromJSON({ data: allCveData, fileName, exportType });

handleCloseExport();
};
Expand Down Expand Up @@ -325,7 +322,7 @@ function VulnerabilitiesDetails(props) {
className={classes.popper}
data-testid="export-excel-menuItem"
>
MS Excel
xls
</MenuItem>
</Menu>
</Stack>
Expand Down

0 comments on commit 51794f1

Please sign in to comment.