Skip to content

Commit

Permalink
TableRow tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Kunalpal216 committed Nov 9, 2024
1 parent c0700be commit f45282f
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
1 change: 1 addition & 0 deletions scripts/__mocks__/@pdfme/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export const generate = async ({
inputs: Record<string, string>[];
}): Promise<Uint8Array> => {
if (template.schemas.length === 0 || inputs.length === 0) {
// console.log('pdf error: length : ', template, inputs, inputs.length);
throw new Error('Template or inputs cannot be empty.');
}
// Generate mock PDF-like header bytes
Expand Down
45 changes: 44 additions & 1 deletion src/components/CheckIn/TableRow.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { fireEvent, render, waitFor } from '@testing-library/react';
import { fireEvent, render } from '@testing-library/react';
import { TableRow } from './TableRow';
import { BrowserRouter } from 'react-router-dom';
import { Provider } from 'react-redux';
Expand Down Expand Up @@ -130,4 +130,47 @@ describe('Testing Table Row for CheckIn Table', () => {
expect(await findByText('Error checking in')).toBeInTheDocument();
expect(await findByText('Oops')).toBeInTheDocument();
});

test('If PDF generation fails, the error message should be shown', async () => {
const props = {
data: {
id: `123`,
name: '',
userId: `user123`,
checkIn: {
_id: '123',
time: '12:00:00',
},
eventId: `event123`,
},
refetch: jest.fn(),
};

const { findByText } = render(
<BrowserRouter>
<LocalizationProvider dateAdapter={AdapterDayjs}>
<MockedProvider addTypename={false} mocks={checkInMutationSuccess}>
<Provider store={store}>
<I18nextProvider i18n={i18nForTest}>
<ToastContainer />
<TableRow {...props} />
</I18nextProvider>
</Provider>
</MockedProvider>
</LocalizationProvider>
</BrowserRouter>,
);

// Mocking the PDF generation function to throw an error
global.URL.createObjectURL = jest.fn(() => 'mockURL');
global.window.open = jest.fn();

fireEvent.click(await findByText('Download Tag'));

expect(
await findByText(
'Error generating pdf: Template or inputs cannot be empty.',
),
).toBeInTheDocument();
});
});
5 changes: 4 additions & 1 deletion src/components/CheckIn/TableRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ export const TableRow = ({
*/
const generateTag = async (): Promise<void> => {
try {
const inputs = [{ name: data.name }];
const inputs = [];
if (data.name) {
inputs.push({ name: data.name });
}
const pdf = await generate({ template: tagTemplate, inputs });
// istanbul ignore next
const blob = new Blob([pdf.buffer], { type: 'application/pdf' });
Expand Down

0 comments on commit f45282f

Please sign in to comment.