Skip to content

Commit

Permalink
fix: fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
usamaidrsk committed Jul 23, 2024
1 parent 9cd0ee7 commit 0935272
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 37 deletions.
10 changes: 10 additions & 0 deletions __mocks__/patient-note-config.mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export const ConfigMock = {
notesConfig: {
clinicianEncounterRole: '240b26f9-dd88-4172-823d-4a8bfeb7841f',
encounterNoteTextConceptUuid: '162169AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA',
encounterTypeUuid: 'd7151f82-c1f3-4152-a605-2f9ea7414a79',
formConceptUuid: 'c75f120a-04ec-11e3-8780-2b40bef9a44b',
visitDiagnosesConceptUuid: '159947AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA',
problemListConceptUuid: '1284AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA',
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,22 @@ import React from 'react';
import userEvent from '@testing-library/user-event';
import { render, screen } from '@testing-library/react';
import { showSnackbar, useConfig, useSession } from '@openmrs/esm-framework';
import { saveVisitNote } from '../visit-notes.resource';
import {
ConfigMock,
mockFetchLocationByUuidResponse,
mockFetchProviderByUuidResponse,
mockSessionDataResponse,
} from '__mocks__';
import { mockPatient } from 'tools';
import WardPatientNotesForm from './notes-form.component';
import { savePatientNote } from './notes-form.resource';
import PatientNotesForm from './notes-form.component';
import { mockPatient, mockSession } from '__mocks__';
import { ConfigMock } from '../../../../../../__mocks__/patient-note-config.mock';

const testProps = {
patientUuid: mockPatient.id,
patientUuid: mockPatient.uuid,
closeWorkspace: jest.fn(),
closeWorkspaceWithSavedChanges: jest.fn(),
promptBeforeClosing: jest.fn(),
setTitle: jest.fn(),
onWorkspaceClose: jest.fn(),
setOnCloseCallback: jest.fn(),
};

const mockSaveVisitNote = saveVisitNote as jest.Mock;
const mockSavePatientNote = savePatientNote as jest.Mock;
const mockedShowSnackbar = jest.mocked(showSnackbar);
const mockUseConfig = useConfig as jest.Mock;
const mockUseSession = useSession as jest.Mock;
Expand All @@ -34,25 +30,12 @@ jest.mock('@openmrs/esm-framework', () => {
createErrorHandler: jest.fn(),
showSnackbar: jest.fn(),
useConfig: jest.fn().mockReturnValue(() => ConfigMock),
useSession: jest.fn().mockReturnValue(() => mockSessionDataResponse),
useSession: jest.fn().mockReturnValue(() => mockSession),
};
});

jest.mock('../visit-notes.resource', () => ({
fetchDiagnosisConceptsByName: jest.fn(),
useLocationUuid: jest.fn().mockImplementation(() => ({
data: mockFetchLocationByUuidResponse.data.uuid,
})),
useProviderUuid: jest.fn().mockImplementation(() => ({
data: mockFetchProviderByUuidResponse.data.uuid,
})),
saveVisitNote: jest.fn(),
useVisitNotes: jest.fn().mockImplementation(() => ({
mutateVisitNotes: jest.fn(),
})),
useInfiniteVisits: jest.fn().mockImplementation(() => ({
mutateVisits: jest.fn(),
})),
jest.mock('./notes-form.resource', () => ({
savePatientNote: jest.fn(),
}));

test('renders the visit notes form with all the relevant fields and values', () => {
Expand All @@ -66,22 +49,22 @@ test('renders a success snackbar upon successfully recording a visit note', asyn
const successPayload = {
encounterProviders: expect.arrayContaining([
{
encounterRole: ConfigMock.visitNoteConfig.clinicianEncounterRole,
encounterRole: ConfigMock.notesConfig.clinicianEncounterRole,
provider: undefined,
},
]),
encounterType: ConfigMock.visitNoteConfig.encounterTypeUuid,
encounterType: ConfigMock.notesConfig.encounterTypeUuid,
location: undefined,
obs: expect.arrayContaining([
{
concept: { display: '', uuid: '162169AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA' },
value: 'Sample clinical note',
},
]),
patient: mockPatient.id,
patient: mockPatient.uuid,
};

mockSaveVisitNote.mockResolvedValueOnce({ status: 201, body: 'Condition created' });
mockSavePatientNote.mockResolvedValueOnce({ status: 201, body: 'Condition created' });

renderWardPatientNotesForm();

Expand All @@ -93,8 +76,8 @@ test('renders a success snackbar upon successfully recording a visit note', asyn
const submitButton = screen.getByRole('button', { name: /Save/i });
await userEvent.click(submitButton);

expect(mockSaveVisitNote).toHaveBeenCalledTimes(1);
expect(mockSaveVisitNote).toHaveBeenCalledWith(new AbortController(), expect.objectContaining(successPayload));
expect(mockSavePatientNote).toHaveBeenCalledTimes(1);
expect(mockSavePatientNote).toHaveBeenCalledWith(new AbortController(), expect.objectContaining(successPayload));
});

test('renders an error snackbar if there was a problem recording a visit note', async () => {
Expand All @@ -106,7 +89,7 @@ test('renders an error snackbar if there was a problem recording a visit note',
},
};

mockSaveVisitNote.mockRejectedValueOnce(error);
mockSavePatientNote.mockRejectedValueOnce(error);
renderWardPatientNotesForm();

const note = screen.getByRole('textbox', { name: /Write your notes/i });
Expand All @@ -122,12 +105,12 @@ test('renders an error snackbar if there was a problem recording a visit note',
isLowContrast: false,
kind: 'error',
subtitle: 'Internal Server Error',
title: 'Error saving visit note',
title: 'Error saving patient note',
});
});

function renderWardPatientNotesForm() {
mockUseConfig.mockReturnValue(ConfigMock);
mockUseSession.mockReturnValue(mockSessionDataResponse);
render(<WardPatientNotesForm {...testProps} />);
mockUseSession.mockReturnValue(mockSession);
render(<PatientNotesForm {...testProps} />);
}

0 comments on commit 0935272

Please sign in to comment.