From 51794f1ea06720a4c88cbffff1a2e451e0c95cce Mon Sep 17 00:00:00 2001 From: Andreea-Lupu Date: Mon, 15 Jan 2024 19:35:59 +0200 Subject: [PATCH] fix: export vulnerabilities as xls using export-from-json Signed-off-by: Andreea-Lupu --- package-lock.json | 23 +------------------ package.json | 3 +-- .../TagPage/VulnerabilitiesDetails.test.js | 7 +----- .../Tag/Tabs/VulnerabilitiesDetails.jsx | 11 ++++----- 4 files changed, 7 insertions(+), 37 deletions(-) diff --git a/package-lock.json b/package-lock.json index 226762f0..5f8f0819 100644 --- a/package-lock.json +++ b/package-lock.json @@ -27,8 +27,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", @@ -19214,26 +19213,6 @@ } } }, - "node_modules/xlsx": { - "version": "0.18.5", - "resolved": "https://registry.npmjs.org/xlsx/-/xlsx-0.18.5.tgz", - "integrity": "sha512-dmg3LCjBPHZnQp5/F/+nnTa+miPJxUXB6vtk42YjBBKayDNagxGEeIdWApkYPOf3Z3pm3k62Knjzp7lMeTEtFQ==", - "dependencies": { - "adler-32": "~1.3.0", - "cfb": "~1.2.1", - "codepage": "~1.15.0", - "crc-32": "~1.2.1", - "ssf": "~0.11.2", - "wmf": "~1.0.1", - "word": "~0.3.0" - }, - "bin": { - "xlsx": "bin/xlsx.njs" - }, - "engines": { - "node": ">=0.8" - } - }, "node_modules/xml-name-validator": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/xml-name-validator/-/xml-name-validator-3.0.0.tgz", diff --git a/package.json b/package.json index aa2a0ddd..ade905f6 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/__tests__/TagPage/VulnerabilitiesDetails.test.js b/src/__tests__/TagPage/VulnerabilitiesDetails.test.js index 13d3cdff..9a9a73fb 100644 --- a/src/__tests__/TagPage/VulnerabilitiesDetails.test.js +++ b/src/__tests__/TagPage/VulnerabilitiesDetails.test.js @@ -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 ( @@ -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 } }) @@ -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(); diff --git a/src/components/Tag/Tabs/VulnerabilitiesDetails.jsx b/src/components/Tag/Tabs/VulnerabilitiesDetails.jsx index 1872f6a0..466df28a 100644 --- a/src/components/Tag/Tabs/VulnerabilitiesDetails.jsx +++ b/src/components/Tag/Tabs/VulnerabilitiesDetails.jsx @@ -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'; @@ -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(); }; @@ -325,7 +322,7 @@ function VulnerabilitiesDetails(props) { className={classes.popper} data-testid="export-excel-menuItem" > - MS Excel + xls