From e2860a5a85218e76499be3ec5f50eeed890e0bc0 Mon Sep 17 00:00:00 2001 From: Usama Idriss Kakumba Date: Wed, 7 Aug 2024 14:16:01 +0300 Subject: [PATCH] chore: add tests --- .../form/notes-form.test.tsx | 17 +- .../history/note.component.tsx | 2 +- .../history/notes-container.test.tsx | 184 ++++++++---------- packages/esm-ward-app/translations/en.json | 2 + 4 files changed, 91 insertions(+), 114 deletions(-) diff --git a/packages/esm-ward-app/src/ward-workspace/ward-patient-notes/form/notes-form.test.tsx b/packages/esm-ward-app/src/ward-workspace/ward-patient-notes/form/notes-form.test.tsx index 8112e0958..aaadb2f64 100644 --- a/packages/esm-ward-app/src/ward-workspace/ward-patient-notes/form/notes-form.test.tsx +++ b/packages/esm-ward-app/src/ward-workspace/ward-patient-notes/form/notes-form.test.tsx @@ -2,10 +2,9 @@ import React from 'react'; import userEvent from '@testing-library/user-event'; import { render, screen } from '@testing-library/react'; import { createErrorHandler, ResponsiveWrapper, showSnackbar, translateFrom, useSession } from '@openmrs/esm-framework'; -import { savePatientNote } from './notes-form.resource'; +import { savePatientNote } from '../notes.resource'; import PatientNotesForm from './notes-form.component'; import { emrConfigurationMock, mockPatient, mockSession } from '__mocks__'; -import useEmrConfiguration from '../../../hooks/useEmrConfiguration'; const testProps = { patientUuid: mockPatient.uuid, @@ -15,6 +14,7 @@ const testProps = { setTitle: jest.fn(), onWorkspaceClose: jest.fn(), setOnCloseCallback: jest.fn(), + emrConfiguration: emrConfigurationMock, }; const mockSavePatientNote = savePatientNote as jest.Mock; @@ -24,21 +24,10 @@ const mockedTranslateFrom = jest.mocked(translateFrom); const mockedResponsiveWrapper = jest.mocked(ResponsiveWrapper); const mockedUseSession = jest.mocked(useSession); -jest.mock('./notes-form.resource', () => ({ +jest.mock('../notes.resource', () => ({ savePatientNote: jest.fn(), })); -jest.mock('../../../hooks/useEmrConfiguration', () => jest.fn()); - -const mockedUseEmrConfiguration = jest.mocked(useEmrConfiguration); - -mockedUseEmrConfiguration.mockReturnValue({ - emrConfiguration: emrConfigurationMock, - mutateEmrConfiguration: jest.fn(), - isLoadingEmrConfiguration: false, - errorFetchingEmrConfiguration: null, -}); - test('renders the visit notes form with all the relevant fields and values', () => { renderWardPatientNotesForm(); diff --git a/packages/esm-ward-app/src/ward-workspace/ward-patient-notes/history/note.component.tsx b/packages/esm-ward-app/src/ward-workspace/ward-patient-notes/history/note.component.tsx index 64ac4b116..c04eda147 100644 --- a/packages/esm-ward-app/src/ward-workspace/ward-patient-notes/history/note.component.tsx +++ b/packages/esm-ward-app/src/ward-workspace/ward-patient-notes/history/note.component.tsx @@ -6,7 +6,7 @@ import styles from './styles.scss'; export const InPatientNoteSkeleton: React.FC = () => { return ( - +
diff --git a/packages/esm-ward-app/src/ward-workspace/ward-patient-notes/history/notes-container.test.tsx b/packages/esm-ward-app/src/ward-workspace/ward-patient-notes/history/notes-container.test.tsx index c5a16fb23..1f5d043e2 100644 --- a/packages/esm-ward-app/src/ward-workspace/ward-patient-notes/history/notes-container.test.tsx +++ b/packages/esm-ward-app/src/ward-workspace/ward-patient-notes/history/notes-container.test.tsx @@ -1,110 +1,96 @@ import React from 'react'; -import userEvent from '@testing-library/user-event'; import { render, screen } from '@testing-library/react'; -import { createErrorHandler, ResponsiveWrapper, showSnackbar, translateFrom, useSession } from '@openmrs/esm-framework'; -import { savePatientNote } from './notes-form.resource'; -import PatientNotesForm from './notes-form.component'; -import { emrConfigurationMock, mockPatient, mockSession } from '__mocks__'; -import useEmrConfiguration from '../../../hooks/useEmrConfiguration'; +import PatientNotesHistory from './notes-container.component'; +import { usePatientNotes } from '../notes.resource'; -const testProps = { - patientUuid: mockPatient.uuid, - closeWorkspace: jest.fn(), - closeWorkspaceWithSavedChanges: jest.fn(), - promptBeforeClosing: jest.fn(), - setTitle: jest.fn(), - onWorkspaceClose: jest.fn(), - setOnCloseCallback: jest.fn(), -}; - -const mockSavePatientNote = savePatientNote as jest.Mock; -const mockedShowSnackbar = jest.mocked(showSnackbar); -const mockedCreateErrorHandler = jest.mocked(createErrorHandler); -const mockedTranslateFrom = jest.mocked(translateFrom); -const mockedResponsiveWrapper = jest.mocked(ResponsiveWrapper); -const mockedUseSession = jest.mocked(useSession); - -jest.mock('./notes-form.resource', () => ({ - savePatientNote: jest.fn(), +jest.mock('../notes.resource', () => ({ + usePatientNotes: jest.fn(), })); -jest.mock('../../../hooks/useEmrConfiguration', () => jest.fn()); - -const mockedUseEmrConfiguration = jest.mocked(useEmrConfiguration); - -mockedUseEmrConfiguration.mockReturnValue({ - emrConfiguration: emrConfigurationMock, - mutateEmrConfiguration: jest.fn(), - isLoadingEmrConfiguration: false, - errorFetchingEmrConfiguration: null, -}); - -test('renders the visit notes form with all the relevant fields and values', () => { - renderWardPatientNotesForm(); - - expect(screen.getByRole('textbox', { name: /Write your notes/i })).toBeInTheDocument(); - expect(screen.getByRole('button', { name: /Save/i })).toBeInTheDocument(); -}); - -test('renders a success snackbar upon successfully recording a visit note', async () => { - const successPayload = { - encounterType: emrConfigurationMock.visitNoteEncounterType.uuid, - location: undefined, - obs: expect.arrayContaining([ - { - concept: { display: '', uuid: '162169AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA' }, - value: 'Sample clinical note', - }, - ]), - patient: mockPatient.uuid, - }; - - mockSavePatientNote.mockResolvedValue({ status: 201, body: 'Condition created' }); - - renderWardPatientNotesForm(); - - const note = screen.getByRole('textbox', { name: /Write your notes/i }); - await userEvent.clear(note); - await userEvent.type(note, 'Sample clinical note'); - expect(note).toHaveValue('Sample clinical note'); - - const submitButton = screen.getByRole('button', { name: /Save/i }); - await userEvent.click(submitButton); - - expect(mockSavePatientNote).toHaveBeenCalledTimes(1); - expect(mockSavePatientNote).toHaveBeenCalledWith(expect.objectContaining(successPayload), new AbortController()); -}); - -test('renders an error snackbar if there was a problem recording a visit note', async () => { - const error = { - message: 'Internal Server Error', - response: { - status: 500, - statusText: 'Internal Server Error', - }, - }; - - mockSavePatientNote.mockRejectedValueOnce(error); - renderWardPatientNotesForm(); +const mockPatientUuid = 'sample-patient-uuid'; +const mockEmrConfiguration = { + visitNoteEncounterType: { uuid: 'visit-note-encounter-type' }, + consultFreeTextCommentsConcept: { uuid: 'consult-free-text-comments-concept' }, +}; - const note = screen.getByRole('textbox', { name: /Write your notes/i }); - await userEvent.clear(note); - await userEvent.type(note, 'Sample clinical note'); - expect(note).toHaveValue('Sample clinical note'); +const mockPatientNotes = [ + { + id: 'note-1', + diagnoses: '', + encounterDate: '2024-08-01', + encounterNote: 'Patient shows improvement with current medication.', + encounterNoteRecordedAt: '2024-08-01T12:34:56Z', + encounterProvider: 'Dr. John Doe', + encounterProviderRole: 'Endocrinologist', + }, + { + id: 'note-2', + diagnoses: '', + encounterDate: '2024-08-02', + encounterNote: 'Blood pressure is slightly elevated. Consider adjusting medication.', + encounterNoteRecordedAt: '2024-08-02T14:22:00Z', + encounterProvider: 'Dr. Jane Smith', + encounterProviderRole: 'Cardiologist', + }, +]; + +describe('PatientNotesHistory', () => { + beforeEach(() => { + jest.resetAllMocks(); + }); - const submitButton = screen.getByRole('button', { name: /Save/i }); + test('renders the component with header and dropdown', () => { + usePatientNotes.mockReturnValue({ + patientNotes: [], + isLoadingPatientNotes: false, + }); + + render( + , + ); + + expect(screen.getByText('History')).toBeInTheDocument(); + expect(screen.getByRole('combobox', { name: /Show/i })).toBeInTheDocument(); + }); - await userEvent.click(submitButton); + test('displays loading skeletons when loading', () => { + usePatientNotes.mockReturnValue({ + patientNotes: [], + isLoadingPatientNotes: true, + }); + + render( + , + ); + + expect(screen.getAllByTestId('in-patient-note-skeleton')).toHaveLength(4); + }); - expect(mockedShowSnackbar).toHaveBeenCalledWith({ - isLowContrast: false, - kind: 'error', - subtitle: 'Internal Server Error', - title: 'Error saving patient note', + test('displays patient notes when available', () => { + usePatientNotes.mockReturnValue({ + patientNotes: mockPatientNotes, + isLoadingPatientNotes: false, + }); + + render( + , + ); + + expect(screen.getByText('Patient shows improvement with current medication.')).toBeInTheDocument(); + expect(screen.getByText('Dr. John Doe')).toBeInTheDocument(); + expect(screen.getByText('Blood pressure is slightly elevated. Consider adjusting medication.')).toBeInTheDocument(); + expect(screen.getByText('Dr. Jane Smith')).toBeInTheDocument(); }); }); - -function renderWardPatientNotesForm() { - mockedUseSession.mockReturnValue(mockSession); - render(); -} diff --git a/packages/esm-ward-app/translations/en.json b/packages/esm-ward-app/translations/en.json index 65a66119f..84483fe99 100644 --- a/packages/esm-ward-app/translations/en.json +++ b/packages/esm-ward-app/translations/en.json @@ -5,6 +5,7 @@ "admit": "Admit", "admitPatient": "Admit patient", "admitting": "Admitting...", + "all": "All", "bedManagementModuleNotInstalled": "Bed management module is not present to allow bed selection", "bedShare": "Bed share", "cancel": "Cancel", @@ -39,6 +40,7 @@ "saving": "Saving...", "searchForPatient": "Search for a patient", "selectABed": "Select a bed", + "show": "Show", "somePartsOfTheFormDidntLoad": "Some parts of the form didn't load", "transferElsewhere": "Transfer elsewhere", "unableToSelectBeds": "Unable to select beds",