Skip to content

Commit

Permalink
chore: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
usamaidrsk committed Aug 7, 2024
1 parent f4e486f commit e2860a5
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 114 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -15,6 +14,7 @@ const testProps = {
setTitle: jest.fn(),
onWorkspaceClose: jest.fn(),
setOnCloseCallback: jest.fn(),
emrConfiguration: emrConfigurationMock,
};

const mockSavePatientNote = savePatientNote as jest.Mock;
Expand All @@ -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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import styles from './styles.scss';

export const InPatientNoteSkeleton: React.FC = () => {
return (
<Tile className={styles.noteTile}>
<Tile className={styles.noteTile} data-testid="in-patient-note-skeleton">
<div className={styles.noteHeader}>
<SkeletonText heading width="30%" />
<SkeletonText width="20%" />
Expand Down
Original file line number Diff line number Diff line change
@@ -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(
<PatientNotesHistory
patientUuid={mockPatientUuid}
emrConfiguration={mockEmrConfiguration}
isLoadingEmrConfiguration={false}
/>,
);

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(
<PatientNotesHistory
patientUuid={mockPatientUuid}
emrConfiguration={mockEmrConfiguration}
isLoadingEmrConfiguration={true}
/>,
);

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(
<PatientNotesHistory
patientUuid={mockPatientUuid}
emrConfiguration={mockEmrConfiguration}
isLoadingEmrConfiguration={false}
/>,
);

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(<PatientNotesForm {...testProps} />);
}
2 changes: 2 additions & 0 deletions packages/esm-ward-app/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit e2860a5

Please sign in to comment.