Skip to content

Commit

Permalink
feat: tests of F24 and PagoPa payment boxes
Browse files Browse the repository at this point in the history
  • Loading branch information
alessandrogelmi committed Mar 3, 2025
1 parent d2534c9 commit c8de823
Show file tree
Hide file tree
Showing 6 changed files with 368 additions and 24 deletions.
4 changes: 2 additions & 2 deletions packages/pn-pa-webapp/src/__mocks__/NewNotification.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export const newNotificationGroups: Array<UserGroup> = [
},
];

const newNotificationPagoPa: NewNotificationPagoPaPayment = {
export const newNotificationPagoPa: NewNotificationPagoPaPayment = {
id: 'mocked-pagopa-id',
idx: 0,
contentType: 'application/pdf',
Expand Down Expand Up @@ -76,7 +76,7 @@ const newNotificationPagoPaForBff: PagoPaPayment = {
},
};

const newNotificationF24: NewNotificationF24Payment = {
export const newNotificationF24: NewNotificationF24Payment = {
id: 'mocked-f24-id',
idx: 0,
name: 'mocked-name',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,8 @@ import { NewNotificationF24Payment, NotificationFeePolicy } from '../../models/N

type PaymentBoxProps = {
id: string;
onFileUploaded: (
id: string,
file?: File,
sha256?: { hashBase64: string; hashHex: string }
) => void;
onRemoveFile: (id: string) => void;
onFileUploaded: (file?: File, sha256?: { hashBase64: string; hashHex: string }) => void;
onRemoveFile: () => void;
f24Payment: NewNotificationF24Payment;
notificationFeePolicy: NotificationFeePolicy;
handleChange: (event: React.ChangeEvent<HTMLInputElement>) => void;
Expand Down Expand Up @@ -56,13 +52,12 @@ const F24PaymentBox: React.FC<PaymentBoxProps> = ({
return (
<Fragment>
<FileUpload
data-testid="fileUploadInput"
uploadText={
isMobile ? t('new-notification.drag-doc-mobile') : t('new-notification.drag-doc-pc')
}
accept="application/json"
onFileUploaded={(file, sha256) => onFileUploaded(id, file, sha256)}
onRemoveFile={() => onRemoveFile(id)}
onFileUploaded={(file, sha256) => onFileUploaded(file, sha256)}
onRemoveFile={() => onRemoveFile()}
sx={{ marginTop: '10px' }}
calcSha256
fileUploaded={{ file }}
Expand All @@ -81,6 +76,7 @@ const F24PaymentBox: React.FC<PaymentBoxProps> = ({
size="small"
margin="normal"
required
data-testid="f24-name"
/>

{(notificationFeePolicy === NotificationFeePolicy.DELIVERY_MODE || showDeleteButton) && (
Expand All @@ -95,6 +91,7 @@ const F24PaymentBox: React.FC<PaymentBoxProps> = ({
value={applyCost}
checked={applyCost}
onChange={(e) => handleChange(e)}
data-testid="f24-apply-cost"
/>
}
label={t(
Expand All @@ -117,6 +114,7 @@ const F24PaymentBox: React.FC<PaymentBoxProps> = ({
justifyContent: { xs: 'flex-start', md: 'flex-end' },
ml: { xs: 'none', md: 'auto' },
}}
data-testid="f24-delete-button"
>
{t('button.delete', { ns: 'common' })}
</ButtonNaked>
Expand All @@ -125,7 +123,7 @@ const F24PaymentBox: React.FC<PaymentBoxProps> = ({
)}

{showDeleteButton && (
<Alert severity="warning">
<Alert severity="warning" data-testid="f24-installment-alert">
{t('new-notification.steps.debt-position-detail.payment-methods.apply-cost-installment')}
</Alert>
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,8 @@ import { NewNotificationPagoPaPayment, NotificationFeePolicy } from '../../model

type PaymentBoxProps = {
id: string;
onFileUploaded: (
id: string,
file?: File,
sha256?: { hashBase64: string; hashHex: string }
) => void;
onRemoveFile: (id: string) => void;
onFileUploaded: (file?: File, sha256?: { hashBase64: string; hashHex: string }) => void;
onRemoveFile: () => void;
pagoPaPayment: NewNotificationPagoPaPayment;
notificationFeePolicy: NotificationFeePolicy;
handleChange: (event: React.ChangeEvent<HTMLInputElement>) => void;
Expand Down Expand Up @@ -56,13 +52,13 @@ const PagoPaPaymentBox: React.FC<PaymentBoxProps> = ({
return (
<Fragment>
<FileUpload
data-testid="fileUploadInput"
data-testid="pagopa-file-upload-input"
uploadText={
isMobile ? t('new-notification.drag-doc-mobile') : t('new-notification.drag-doc-pc')
}
accept="application/pdf"
onFileUploaded={(file, sha256) => onFileUploaded(id, file, sha256)}
onRemoveFile={() => onRemoveFile(id)}
onFileUploaded={(file, sha256) => onFileUploaded(file, sha256)}
onRemoveFile={() => onRemoveFile()}
calcSha256
fileUploaded={{ file }}
showHashCode={false}
Expand All @@ -82,6 +78,7 @@ const PagoPaPaymentBox: React.FC<PaymentBoxProps> = ({
size="small"
margin="normal"
required
data-testid="pagopa-notice-code"
/>
<TextField
id="creditorTaxId"
Expand All @@ -97,6 +94,7 @@ const PagoPaPaymentBox: React.FC<PaymentBoxProps> = ({
size="small"
margin="normal"
required
data-testid="pagopa-creditor-tax-id"
/>
</Stack>

Expand All @@ -112,6 +110,7 @@ const PagoPaPaymentBox: React.FC<PaymentBoxProps> = ({
value={applyCost}
checked={applyCost}
onChange={(e) => handleChange(e)}
data-testid="pagopa-apply-cost"
/>
}
label={t(
Expand All @@ -134,6 +133,7 @@ const PagoPaPaymentBox: React.FC<PaymentBoxProps> = ({
justifyContent: { xs: 'flex-start', md: 'flex-end' },
ml: { xs: 'none', md: 'auto' },
}}
data-testid="pagopa-delete-button"
>
{t('button.delete', { ns: 'common' })}
</ButtonNaked>
Expand All @@ -142,7 +142,7 @@ const PagoPaPaymentBox: React.FC<PaymentBoxProps> = ({
)}

{showDeleteButton && notificationFeePolicy === NotificationFeePolicy.DELIVERY_MODE && (
<Alert severity="warning">
<Alert severity="warning" data-testid="pagopa-installment-alert">
{t('new-notification.steps.debt-position-detail.payment-methods.apply-cost-installment')}
</Alert>
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ const PaymentMethods: React.FC<Props> = ({
<PagoPaPaymentBox
id={`recipients.${recipient.taxId}.pagoPa.${index}`}
key={`${recipient.taxId}-pagoPa-${pagoPaPayment.idx}`}
onFileUploaded={(_, file, sha256) =>
onFileUploaded={(file, sha256) =>
fileUploadedHandler(recipient.taxId, 'pagoPa', index, file, sha256)
}
onRemoveFile={() => removeFileHandler(recipient.taxId, 'pagoPa', index)}
Expand Down Expand Up @@ -195,7 +195,7 @@ const PaymentMethods: React.FC<Props> = ({
<F24PaymentBox
id={`recipients.${recipient.taxId}.f24.${index}`}
key={`${recipient.taxId}-f24-${f24Payment?.idx}`}
onFileUploaded={(_, file, sha256) =>
onFileUploaded={(file, sha256) =>
fileUploadedHandler(recipient.taxId, 'f24', index, file, sha256)
}
onRemoveFile={() => removeFileHandler(recipient.taxId, 'f24', index)}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
import { Formik } from 'formik';
import { beforeEach, describe, expect, it, vi } from 'vitest';

import { newNotificationF24 } from '../../../__mocks__/NewNotification.mock';
import { fireEvent, render, waitFor } from '../../../__test__/test-utils';
import { NotificationFeePolicy } from '../../../models/NewNotification';
import F24PaymentBox from '../F24PaymentBox';

// mock imports
vi.mock('react-i18next', () => ({
// this mock makes sure any components using the translate hook can use it without a warning being shown
useTranslation: () => ({
t: (str: string) => str,
i18n: { language: 'it' },
}),
}));

describe('F24PaymentBox', () => {
const defaultProps = {
id: 'payment-0',
onFileUploaded: vi.fn(),
onRemoveFile: vi.fn(),
f24Payment: newNotificationF24,
notificationFeePolicy: NotificationFeePolicy.FLAT_RATE,
handleChange: vi.fn(),
showDeleteButton: false,
onDeletePayment: vi.fn(),
fieldMeta: () => ({
touched: false,
error: undefined,
value: undefined,
initialTouched: false,
}),
};

beforeEach(() => {
vi.clearAllMocks();
});

it('should renders the component correctly with basic props', () => {
const { getByTestId, queryByTestId } = render(
<Formik initialValues={{}} onSubmit={() => {}}>
{() => <F24PaymentBox {...defaultProps} />}
</Formik>
);

// expect(getByTestId('pagopa-file-upload-input')).toBeInTheDocument();
expect(getByTestId('f24-name')).toBeInTheDocument();
expect(queryByTestId('f24-delete-button')).not.toBeInTheDocument();
expect(queryByTestId('f24-apply-cost')).not.toBeInTheDocument();
});

it('should shows apply cost switch when notificationFeePolicy is DELIVERY_MODE', () => {
const { getByTestId } = render(
<Formik initialValues={{}} onSubmit={() => {}}>
{() => (
<F24PaymentBox
{...defaultProps}
notificationFeePolicy={NotificationFeePolicy.DELIVERY_MODE}
/>
)}
</Formik>
);

expect(getByTestId('f24-apply-cost')).toBeInTheDocument();
});

it('should shows and click delete button when showDeleteButton is true', () => {
const { getByTestId } = render(
<Formik initialValues={{}} onSubmit={() => {}}>
{() => <F24PaymentBox {...defaultProps} showDeleteButton={true} />}
</Formik>
);

const deleteButton = getByTestId('f24-delete-button');
expect(deleteButton).toBeInTheDocument();
fireEvent.click(deleteButton);
expect(defaultProps.onDeletePayment).toHaveBeenCalledTimes(1);
});

it('should shows warning alert when both showDeleteButton and DELIVERY_MODE are true', () => {
const { getByTestId } = render(
<Formik initialValues={{}} onSubmit={() => {}}>
{() => (
<F24PaymentBox
{...defaultProps}
showDeleteButton={true}
notificationFeePolicy={NotificationFeePolicy.DELIVERY_MODE}
/>
)}
</Formik>
);

expect(getByTestId('f24-installment-alert')).toBeInTheDocument();
});

it('should calls onFileUploaded when a file is uploaded', async () => {
const paymentWithoutFile = {
...defaultProps.f24Payment,
file: {
data: undefined,
sha256: {
hashBase64: '',
hashHex: '',
},
},
};

const { getByTestId } = render(
<Formik initialValues={{}} onSubmit={() => {}}>
{() => <F24PaymentBox {...defaultProps} f24Payment={paymentWithoutFile} />}
</Formik>
);
const uploadButton = getByTestId('loadFromPc');
expect(uploadButton).toBeInTheDocument();
const fileInput = getByTestId('fileInput');
const input = fileInput?.querySelector('input');
const mockFile = new File(['mocked content'], 'mocked-file.json', {
type: 'application/json',
});
fireEvent.change(input!, { target: { files: [mockFile] } });
await waitFor(() => {
expect(defaultProps.onFileUploaded).toHaveBeenCalledTimes(1);
});
expect(defaultProps.onFileUploaded).toHaveBeenCalledWith(mockFile, {
hashBase64: 'mocked-hashBase64',
hashHex: 'mocked-hashHex',
});
});

it('should calls onRemoveFile when file is removed', () => {
const { getByTestId } = render(
<Formik initialValues={{}} onSubmit={() => {}}>
{() => <F24PaymentBox {...defaultProps} />}
</Formik>
);

fireEvent.click(getByTestId('removeDocument'));
expect(defaultProps.onRemoveFile).toHaveBeenCalledTimes(1);
});

it('should displays error messages when fields have errors and are touched', () => {
const fieldMetaWithErrors = (name: string) => {
if (name === 'payment-0.name') {
return {
touched: true,
error: 'Name is required',
initialTouched: false,
value: undefined,
};
}
return {
touched: false,
error: undefined,
initialTouched: false,
value: undefined,
};
};

const { getByText } = render(
<Formik initialValues={{}} onSubmit={() => {}}>
{() => <F24PaymentBox {...defaultProps} fieldMeta={fieldMetaWithErrors} />}
</Formik>
);

expect(getByText('Name is required')).toBeInTheDocument();
});
});
Loading

0 comments on commit c8de823

Please sign in to comment.