From 0a29603a9df2a1cafeaa47e3429bd32487b5107e Mon Sep 17 00:00:00 2001
From: Ayush <54752747+ayush-AI@users.noreply.github.com>
Date: Mon, 28 Aug 2023 00:31:27 +0530
Subject: [PATCH] (test): Add test for `patient-list-detail.component` (#790)
---
.../patient-list-detail.test.tsx | 105 ++++++++++++++++++
1 file changed, 105 insertions(+)
create mode 100644 packages/esm-patient-list-app/src/patient-list-detail/patient-list-detail.test.tsx
diff --git a/packages/esm-patient-list-app/src/patient-list-detail/patient-list-detail.test.tsx b/packages/esm-patient-list-app/src/patient-list-detail/patient-list-detail.test.tsx
new file mode 100644
index 000000000..5aee11bd5
--- /dev/null
+++ b/packages/esm-patient-list-app/src/patient-list-detail/patient-list-detail.test.tsx
@@ -0,0 +1,105 @@
+import React from 'react';
+import { render, screen, fireEvent, waitFor } from '@testing-library/react';
+import userEvent from '@testing-library/user-event';
+import { usePatientListDetails, usePatientListMembers } from '../api/hooks';
+import { showToast } from '@openmrs/esm-framework';
+import { deletePatientList } from '../api/api-remote';
+import PatientListDetailComponent from './patient-list-detail.component';
+
+const mockedUsePatientListDetails = usePatientListDetails as jest.Mock;
+const mockedUsePatientListMembers = usePatientListMembers as jest.Mock;
+const mockedDeletePatientList = deletePatientList as jest.Mock;
+
+jest.mock('../api/hooks', () => ({
+ usePatientListDetails: jest.fn(),
+ usePatientListMembers: jest.fn(),
+}));
+
+jest.mock('../api/api-remote');
+
+jest.mock('@openmrs/esm-framework', () => ({
+ ...jest.requireActual('@openmrs/esm-framework'),
+ showToast: jest.fn(),
+ navigate: jest.fn(),
+ ExtensionSlot: jest.fn(),
+}));
+
+const mockedPatientListDetails = {
+ name: 'Test Patient List',
+ description: 'This is a test patient list',
+ size: 5,
+ startDate: '2023-08-14',
+};
+
+const mockedPatientListMembers = [
+ {
+ patient: {
+ person: {
+ display: 'John Doe',
+ gender: 'Male',
+ },
+ identifiers: [
+ {
+ identifier: '100GEJ',
+ },
+ ],
+ uuid: '7cd38a6d-377e-491b-8284-b04cf8b8c6d8',
+ },
+ startDate: '2023-08-10',
+ },
+];
+
+describe('PatientListDetailComponent', () => {
+ beforeEach(() => {
+ jest.clearAllMocks();
+ mockedUsePatientListDetails.mockReturnValue({
+ data: mockedPatientListDetails,
+ });
+
+ mockedUsePatientListMembers.mockReturnValue({
+ data: mockedPatientListMembers,
+ });
+
+ mockedDeletePatientList.mockResolvedValue({});
+ });
+
+ it('renders patient list details', async () => {
+ render();
+
+ await waitFor(() => {
+ expect(screen.getByText('Test Patient List')).toBeInTheDocument();
+ expect(screen.getByText('This is a test patient list')).toBeInTheDocument();
+ });
+ });
+
+ it('displays patient list members', async () => {
+ render();
+
+ await waitFor(() => {
+ expect(screen.getByText('John Doe')).toBeInTheDocument();
+ expect(screen.getByText('Male')).toBeInTheDocument();
+ expect(screen.getByText('100GEJ')).toBeInTheDocument();
+ });
+ });
+
+ it('opens edit overlay when "Edit Name/ Description" is clicked', () => {
+ render();
+
+ userEvent.click(screen.getByText('Actions'));
+ const editBtn = screen.getByText('Edit Name/ Description');
+ userEvent.click(editBtn);
+ });
+
+ it('deletes patient list and navigates on successful delete', async () => {
+ render();
+
+ await waitFor(() => {
+ userEvent.click(screen.getByText('Delete'));
+ });
+
+ await waitFor(() => {
+ expect(mockedDeletePatientList).toHaveBeenCalledTimes(1);
+ expect(showToast).toHaveBeenCalledTimes(1);
+ });
+ });
+});