-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
updated and cleaned downloadPDF code with ResultsOptions.test
- Loading branch information
1 parent
303251c
commit 175c736
Showing
2 changed files
with
93 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,64 @@ | ||
|
||
import { render, fireEvent, screen, waitFor } from '@testing-library/react'; | ||
import '@testing-library/jest-dom'; | ||
import ResultsOptions from './ResultsOptions'; | ||
|
||
|
||
jest.mock('react-ga4', () => ({ | ||
event: jest.fn(), | ||
event: jest.fn(), | ||
})); | ||
|
||
describe('PDF Export', () => { | ||
const mockProps = { | ||
resultsOptions: { | ||
threshold: [70, 100], | ||
searchTerm: "", | ||
intraInstrument: false, | ||
}, | ||
setResultsOptions: jest.fn(), | ||
makePublicShareLink: jest.fn(), | ||
saveToMyHarmony: jest.fn(), | ||
downloadExcel: jest.fn(), | ||
downloadPDF: jest.fn().mockResolvedValue(), | ||
ReactGA: { event: jest.fn() }, | ||
toaster: { error: jest.fn() }, | ||
}; | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('renders PDF export button', () => { | ||
render(<ResultsOptions {...mockProps} />); | ||
expect(screen.getByText('PDF Export')).toBeInTheDocument(); | ||
}); | ||
|
||
it('calls downloadPDF when clicking export button', async () => { | ||
render(<ResultsOptions {...mockProps} />); | ||
|
||
const exportButton = screen.getByText('PDF Export'); | ||
fireEvent.click(exportButton); | ||
|
||
await waitFor(() => { | ||
expect(mockProps.downloadPDF).toHaveBeenCalled(); | ||
describe('ResultsOptions', () => { | ||
const mockProps = { | ||
resultsOptions: { | ||
threshold: [70, 100], | ||
searchTerm: "", | ||
intraInstrument: false, | ||
}, | ||
setResultsOptions: jest.fn(), | ||
makePublicShareLink: jest.fn(), | ||
saveToMyHarmony: jest.fn(), | ||
downloadExcel: jest.fn(), | ||
downloadPDF: jest.fn(), | ||
ReactGA: { event: jest.fn() }, | ||
toaster: { error: jest.fn() }, | ||
}; | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
}); | ||
|
||
it('shows error toast when PDF export fails', async () => { | ||
const errorProps = { | ||
...mockProps, | ||
downloadPDF: jest.fn().mockRejectedValue(new Error('Export failed')), | ||
toaster: { error: jest.fn() } | ||
}; | ||
it('renders PDF export button', () => { | ||
render(<ResultsOptions {...mockProps} />); | ||
expect(screen.getByText('PDF Export')).toBeInTheDocument(); | ||
}); | ||
|
||
it('calls downloadPDF and tracks event when clicking export button', async () => { | ||
render(<ResultsOptions {...mockProps} />); | ||
|
||
const exportButton = screen.getByText('PDF Export'); | ||
fireEvent.click(exportButton); | ||
|
||
expect(mockProps.ReactGA.event).toHaveBeenCalledWith({ | ||
category: "Actions", | ||
action: "Export PDF" | ||
}); | ||
expect(mockProps.downloadPDF).toHaveBeenCalled(); | ||
}); | ||
|
||
it('shows error toast when PDF export fails', async () => { | ||
const errorProps = { | ||
...mockProps, | ||
downloadPDF: jest.fn().mockRejectedValue(new Error('Export failed')), | ||
toaster: { error: jest.fn() } | ||
}; | ||
|
||
render(<ResultsOptions {...errorProps} />); | ||
|
||
const exportButton = screen.getByText('PDF Export'); | ||
fireEvent.click(exportButton); | ||
render(<ResultsOptions {...errorProps} />); | ||
const exportButton = screen.getByText('PDF Export'); | ||
fireEvent.click(exportButton); | ||
|
||
await waitFor(() => { | ||
expect(errorProps.toaster.error).toHaveBeenCalledWith('Failed to generate PDF report'); | ||
await waitFor(() => { | ||
expect(errorProps.toaster.error).toHaveBeenCalledWith('Failed to generate PDF report'); | ||
}); | ||
}); | ||
}); | ||
}); |