Skip to content

Commit

Permalink
(Test) Add Tests for the Require Payment Modal Component (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
ODORA0 authored Sep 24, 2024
1 parent ec7150f commit c5e3f4f
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions src/modal/require-payment-modal.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import React from 'react';
import { render, screen, fireEvent } from '@testing-library/react';
import '@testing-library/jest-dom';
import { useBills } from '../billing.resource';
import RequirePaymentModal from './require-payment-modal.component';

jest.mock('react-i18next', () => ({
useTranslation: () => ({ t: (key: string) => key }),
}));

jest.mock('@openmrs/esm-framework', () => ({
useConfig: () => ({ defaultCurrency: 'USD' }),
}));

jest.mock('../billing.resource', () => ({
useBills: jest.fn(),
}));

jest.mock('../helpers', () => ({
convertToCurrency: (value, currency) => `${currency} ${value.toFixed(2)}`,
}));

describe('RequirePaymentModal', () => {
const closeModal = jest.fn();
const patientUuid = '12345';

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

it('renders correctly', () => {
(useBills as jest.Mock).mockReturnValue({ bills: [], isLoading: false, error: null });
render(<RequirePaymentModal closeModal={closeModal} patientUuid={patientUuid} />);
expect(screen.getByText('patientBillingAlert')).toBeInTheDocument();
});

it('displays loading state', () => {
(useBills as jest.Mock).mockReturnValue({ bills: [], isLoading: true, error: null });
render(<RequirePaymentModal closeModal={closeModal} patientUuid={patientUuid} />);
expect(screen.getByText('inlineLoading')).toBeInTheDocument();
});

it('displays line items', () => {
const bills = [
{
status: 'UNPAID',
lineItems: [
{ billableService: 'Service 1', quantity: 1, price: 100 },
{ item: 'Item 1', quantity: 2, price: 50 },
],
},
];
(useBills as jest.Mock).mockReturnValue({ bills, isLoading: false, error: null });
render(<RequirePaymentModal closeModal={closeModal} patientUuid={patientUuid} />);
expect(screen.getByText('Service 1')).toBeInTheDocument();
expect(screen.getByText('Item 1')).toBeInTheDocument();
});

it('handles closeModal', () => {
(useBills as jest.Mock).mockReturnValue({ bills: [], isLoading: false, error: null });
render(<RequirePaymentModal closeModal={closeModal} patientUuid={patientUuid} />);
fireEvent.click(screen.getByText('cancel'));
fireEvent.click(screen.getByText('ok'));
expect(closeModal).toHaveBeenCalledTimes(2);
});
});

0 comments on commit c5e3f4f

Please sign in to comment.