Skip to content

Commit

Permalink
updated and cleaned downloadPDF code with ResultsOptions.test
Browse files Browse the repository at this point in the history
  • Loading branch information
vickyc2266 committed Dec 8, 2024
1 parent 303251c commit 175c736
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 78 deletions.
71 changes: 42 additions & 29 deletions src/components/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -309,44 +309,57 @@ function App() {


const downloadPDF = async () => {
setTimeout(ratingToast, 1000);

try {
const formattedMatches = computedMatches.map(match => ({
score: match.match,
question1: {
question_text: getQuestion(match.qi).question_text,
instrument_name: getQuestion(match.qi).instrument.name
},
question2: {
question_text: getQuestion(match.mqi).question_text,
instrument_name: getQuestion(match.mqi).instrument.name
}
}));

const pdfExport = new HarmonyPDFExport();
const pdfBlob = await pdfExport.generateReport({
matches: formattedMatches,
instruments: apiData.instruments,
threshold: resultsOptions.threshold[0],
selectedMatches: computedMatches
.filter(m => m.selected)
.map(m => m.id)
const doc = new jsPDF();

doc.setFontSize(24);
doc.text('Harmony Data Report', 105, 20, { align: 'center' });

const summaryData = [
['Total Instruments', apiData.instruments.length],
['Total Matches', computedMatches.length],
['Selected Matches', computedMatches.filter(m => m.selected).length],
['Match Threshold', `${resultsOptions.threshold[0]}%`]
];

doc.autoTable({
startY: 30,
body: summaryData,
theme: 'plain',
margin: { left: 20 },
styles: { fontSize: 12 }
});

const matchesTableData = computedMatches.map(match => {
const q1 = getQuestion(match.qi);
const q2 = getQuestion(match.mqi);
return [
q1.question_text,
q1.instrument.name,
q2.question_text,
q2.instrument.name,
`${(match.match * 100).toFixed(1)}%`
];
});

doc.autoTable({
startY: doc.autoTable.previous.finalY + 10,
head: [['Question 1', 'Instrument 1', 'Question 2', 'Instrument 2', 'Score']],
body: matchesTableData,
theme: 'grid',
headStyles: { fillColor: [33, 69, 237], textColor: 255, fontSize: 12 },
styles: { overflow: 'linebreak', cellWidth: 'wrap', fontSize: 10 }
});

const url = URL.createObjectURL(pdfBlob);
const link = document.createElement('a');
link.href = url;
link.download = 'harmony_matches.pdf';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
URL.revokeObjectURL(url);
doc.save('harmony_matches.pdf');

ReactGA?.event({
category: "Actions",
action: "Export PDF"
});

setTimeout(ratingToast, 1000);
} catch (error) {
console.error('Error generating PDF:', error);
toast.error('Failed to generate PDF report');
Expand Down
100 changes: 51 additions & 49 deletions src/components/ResultsOptions.test.jsx
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');
});
});
});
});

0 comments on commit 175c736

Please sign in to comment.