Skip to content

Commit

Permalink
Add test for end and start visit components
Browse files Browse the repository at this point in the history
  • Loading branch information
donaldkibet committed Mar 16, 2022
1 parent 8735261 commit 2e8bd49
Show file tree
Hide file tree
Showing 9 changed files with 191 additions and 55 deletions.
35 changes: 15 additions & 20 deletions __mocks__/visits.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,27 +54,22 @@ export const mockVisits = {
};

export const mockCurrentVisit = {
mode: VisitMode.LOADING,
visitData: {
uuid: '17f512b4-d264-4113-a6fe-160cb38cb46e',
encounters: [],
patient: { uuid: '8673ee4f-e2ab-4077-ba55-4980f408773e' },
visitType: {
uuid: '7b0f5697-27e3-40c4-8bae-f4049abfb4ed',
name: 'Facility Visit',
display: 'Facility Visit',
},
attributes: [],
location: {
uuid: '6351fcf4-e311-4a19-90f9-35667d99a8af',
name: 'Registration Desk',
display: 'Registration Desk',
},
startDatetime: new Date('2021-03-16T08:16:00.000+0000'),
stopDatetime: null,
uuid: '17f512b4-d264-4113-a6fe-160cb38cb46e',
encounters: [],
patient: { uuid: '8673ee4f-e2ab-4077-ba55-4980f408773e' },
visitType: {
uuid: '7b0f5697-27e3-40c4-8bae-f4049abfb4ed',
name: 'Facility Visit',
display: 'Facility Visit',
},
attributes: [],
startDatetime: new Date('2021-03-16T08:16:00.000+0000'),
stopDatetime: null,
location: {
uuid: '6351fcf4-e311-4a19-90f9-35667d99a8af',
name: 'Registration Desk',
display: 'Registration Desk',
},
status: VisitStatus.ONGOING,
uuid:'some-uuid'
};

export const visitOverviewDetailMockData = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,14 @@ jest.mock('@openmrs/esm-framework', () => ({
describe('ActiveVisitBannerTag: ', () => {
it('renders an active visit tag in the patient banner when an active visit is ongoing', () => {
mockUseVisit.mockReturnValue({
currentVisit: mockCurrentVisit.visitData,
currentVisit: mockCurrentVisit,
error: null,
});
const patient = { ...mockPatient, deceasedDateTime: null };
render(<ActiveVisitBannerTag patientUuid={mockPatient.id} patient={patient} />);

const visitMetadata =
mockCurrentVisit.visitData.visitType.name +
' Started: ' +
formatDatetime(mockCurrentVisit.visitData.startDatetime, { mode: 'wide' });
mockCurrentVisit.visitType.name + ' Started: ' + formatDatetime(mockCurrentVisit.startDatetime, { mode: 'wide' });

expect(
screen.getByRole('tooltip', {
Expand All @@ -36,16 +34,14 @@ describe('ActiveVisitBannerTag: ', () => {

it('should not render active visit tag if patient is dead', () => {
mockUseVisit.mockReturnValue({
currentVisit: mockCurrentVisit.visitData,
currentVisit: mockCurrentVisit,
error: null,
});
const patient = { ...mockPatient, deceasedDateTime: '2002-04-04' };
render(<ActiveVisitBannerTag patientUuid={mockPatient.id} patient={patient} />);

const visitMetadata =
mockCurrentVisit.visitData.visitType.name +
' Started: ' +
formatDatetime(mockCurrentVisit.visitData.startDatetime, { mode: 'wide' });
mockCurrentVisit.visitType.name + ' Started: ' + formatDatetime(mockCurrentVisit.startDatetime, { mode: 'wide' });

expect(
screen.queryByRole('tooltip', {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jest.mock('@openmrs/esm-framework', () => {
});

describe('CancelVisitOverflowMenuItem', () => {
it('should launch cance visit dialog box', () => {
it('should launch cancel visit dialog box', () => {
mockUseVisit.mockReturnValueOnce({ currentVisit: mockCurrentVisit });
render(<CancelVisitOverflowMenuItem patientUuid="some-uuid" />);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jest.mock('@openmrs/esm-patient-common-lib', () => {
};
});

fdescribe('VisitDialog', () => {
describe('VisitDialog', () => {
beforeEach(() => {
spyOn(mockEsmOpenmrsFramwork, 'useVisit').and.returnValue({ currentVisit: mockCurrentVisit });
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,16 @@ const CancelVisit: React.FC<CancelVisitProps> = ({ patientUuid }) => {
const { t } = useTranslation();
const { type } = useVisitDialog(patientUuid);
const { currentVisit, mutate } = useVisit(patientUuid);
const [isSubmitting, setIsSubmitting] = useState(false);
const [submitting, setSubmitting] = useState(false);

const closeModal = useCallback(
() => window.dispatchEvent(new CustomEvent('visit-dialog', { detail: { type: 'close' } })),
[],
);

const cancelActiveVisit = useCallback(() => {
setIsSubmitting(true);
// TO DO expand updateVisit function in esm-api to support this request
setSubmitting(true);
openmrsFetch(`/ws/rest/v1/visit/${currentVisit.uuid}`, {
headers: {
'Content-type': 'application/json',
Expand All @@ -37,7 +38,7 @@ const CancelVisit: React.FC<CancelVisitProps> = ({ patientUuid }) => {
description: t('visitCanceled', 'Canceled active visit successfully'),
});
closeModal();
setIsSubmitting(false);
setSubmitting(false);
},
(error) => {
showNotification({
Expand All @@ -46,7 +47,7 @@ const CancelVisit: React.FC<CancelVisitProps> = ({ patientUuid }) => {
critical: true,
description: error?.message,
});
setIsSubmitting(false);
setSubmitting(false);
},
);
}, [currentVisit]);
Expand All @@ -63,8 +64,8 @@ const CancelVisit: React.FC<CancelVisitProps> = ({ patientUuid }) => {
<Button kind="secondary" onClick={closeModal}>
{t('cancel', 'Cancel')}
</Button>
<Button disabled={isSubmitting} kind="danger" onClick={cancelActiveVisit}>
{isSubmitting ? <InlineLoading description={t('loading', 'Loading...')} /> : t('cancelVisit', 'Cancel Visit')}
<Button disabled={submitting} kind="danger" onClick={cancelActiveVisit}>
{submitting ? <InlineLoading description={t('loading', 'Loading...')} /> : t('cancelVisit', 'Cancel Visit')}
</Button>
</ModalFooter>
</ComposedModal>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { screen, render } from '@testing-library/react';
import { screen, render, waitFor } from '@testing-library/react';
import CancelVisit from './cancel-visit.component';
import userEvent from '@testing-library/user-event';
import { useVisit, openmrsFetch, showNotification, showToast } from '@openmrs/esm-framework';
Expand Down Expand Up @@ -38,19 +38,18 @@ describe('Cancel Visit', () => {
const cancelVisitButton = screen.getByRole('button', { name: /Cancel Visit/i, exact: true });

userEvent.click(cancelVisitButton);
expect(mockOpenmrsFetch).toHaveBeenCalledWith('/ws/rest/v1/visit/some-uuid', {
expect(mockOpenmrsFetch).toHaveBeenCalledWith('/ws/rest/v1/visit/17f512b4-d264-4113-a6fe-160cb38cb46e', {
body: { voided: true },
headers: { 'Content-type': 'application/json' },
method: 'POST',
});

const closeModalButton = await screen.findByRole('button', { name: 'Cancel', exact: true });
userEvent.click(closeModalButton);

expect(mockShowToast).toHaveBeenCalledWith({
kind: 'success',
title: 'Cancel visit',
description: 'Canceled active visit successfully',
await waitFor(() => {
expect(mockShowToast).toHaveBeenCalledWith({
kind: 'success',
title: 'Cancel visit',
description: 'Canceled active visit successfully',
});
});
});

Expand All @@ -66,19 +65,19 @@ describe('Cancel Visit', () => {
const cancelVisitButton = screen.getByRole('button', { name: /Cancel Visit/i, exact: true });

userEvent.click(cancelVisitButton);
expect(mockOpenmrsFetch).toHaveBeenCalledWith('/ws/rest/v1/visit/some-uuid', {
expect(mockOpenmrsFetch).toHaveBeenCalledWith('/ws/rest/v1/visit/17f512b4-d264-4113-a6fe-160cb38cb46e', {
body: { voided: true },
headers: { 'Content-type': 'application/json' },
method: 'POST',
});

const closeModalButton = await screen.findByRole('button', { name: 'Cancel', exact: true });
userEvent.click(closeModalButton);
expect(mockShowNotification).toHaveBeenCalledWith({
critical: true,
description: 'Internal server error',
kind: 'error',
title: 'Error canceling active visit',
await waitFor(() => {
expect(mockShowNotification).toHaveBeenCalledWith({
critical: true,
description: 'Internal server error',
kind: 'error',
title: 'Error canceling active visit',
});
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import React from 'react';
import EndVisit from './end-visit.component';
import { screen, render, waitFor } from '@testing-library/react';
import { showNotification, showToast, updateVisit, useVisit } from '@openmrs/esm-framework';
import { mockCurrentVisit } from '../../../../../__mocks__/visits.mock';
import * as mockUseVisitDialog from '../useVisitDialog';
import userEvent from '@testing-library/user-event';
import { of, throwError } from 'rxjs';

const endVisitPayload = {
location: '6351fcf4-e311-4a19-90f9-35667d99a8af',
startDatetime: new Date('2021-03-16T08:16:00.000Z'),
stopDatetime: expect.anything(),
visitType: '7b0f5697-27e3-40c4-8bae-f4049abfb4ed',
};

const mockUseVisit = useVisit as jest.Mock;
const mockUpdateVisit = updateVisit as jest.Mock;
const mockShowToast = showToast as jest.Mock;
const mockShowNotification = showNotification as jest.Mock;
const mockMutate = jest.fn();

jest.mock('@openmrs/esm-framework', () => {
const originalModule = jest.requireActual('@openmrs/esm-framework');
return {
...originalModule,
showToast: jest.fn(),
showNotification: jest.fn(),
updateVisit: jest.fn(),
};
});

describe('EndVisit', () => {
beforeEach(() => {
jest.resetAllMocks();
});

test('should end an active visit and display toast message', async () => {
spyOn(mockUseVisitDialog, 'useVisitDialog').and.returnValue({ type: 'end' });
mockUseVisit.mockReturnValue({ currentVisit: mockCurrentVisit, mutate: mockMutate });
mockUpdateVisit.mockReturnValueOnce(of({ status: 200 }));
render(<EndVisit patientUuid="some-patient-uuid" />);

expect(screen.getByRole('heading', { name: /End active visit/ })).toBeInTheDocument();
expect(
screen.getByText('Ending this visit, will not allow you to fill another encounter form for this patient'),
).toBeInTheDocument();

const endVisitButton = await screen.findByRole('button', { name: /End Visit/i });
expect(endVisitButton).toBeInTheDocument();

userEvent.click(endVisitButton);

expect(updateVisit).toHaveBeenCalledWith(
'17f512b4-d264-4113-a6fe-160cb38cb46e',
endVisitPayload,
expect.anything(),
);

expect(mockShowToast).toHaveBeenCalledWith({ description: 'Ended active visit successfully', kind: 'success' });
});

test('should display error message when rest api call to end visit fails', async () => {
spyOn(mockUseVisitDialog, 'useVisitDialog').and.returnValue({ type: 'end' });
mockUseVisit.mockReturnValue({ currentVisit: mockCurrentVisit, mutate: mockMutate });
mockUpdateVisit.mockReturnValueOnce(throwError(new Error('Internal error message')));
render(<EndVisit patientUuid="some-patient-uuid" />);

expect(screen.getByRole('heading', { name: /End active visit/ })).toBeInTheDocument();
expect(
screen.getByText('Ending this visit, will not allow you to fill another encounter form for this patient'),
).toBeInTheDocument();

const endVisitButton = await screen.findByRole('button', { name: /End Visit/i });
expect(endVisitButton).toBeInTheDocument();

userEvent.click(endVisitButton);

expect(updateVisit).toHaveBeenCalledWith(
'17f512b4-d264-4113-a6fe-160cb38cb46e',
endVisitPayload,
expect.anything(),
);

expect(mockShowNotification).toHaveBeenCalledWith({
description: 'Internal error message',
kind: 'error',
title: 'Error ending active visit',
critical: true,
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ const StartVisit: React.FC<StartVisitProps> = ({ patientUuid }) => {
const handleEditPastVisit = useCallback(() => {
launchPatientWorkspace('past-visits-overview');
closeModal();
}, [closeModal]);
}, []);

const handleStartNewVisit = useCallback(() => {
launchPatientWorkspace('start-visit-workspace-form');
closeModal();
}, [closeModal]);
}, []);

const modalHeaderText =
state?.type === 'past' ? t('addPastVisit', 'Add a past visit') : t('noActiveVisit', 'No active visit');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import React from 'react';
import StartVisit from './start-visit.component';
import { render, screen } from '@testing-library/react';
import * as mockUseVisitDialog from '../useVisitDialog';
import { launchPatientWorkspace } from '@openmrs/esm-patient-common-lib';
import userEvent from '@testing-library/user-event';

jest.mock('@openmrs/esm-patient-common-lib', () => {
const originalModule = jest.requireActual('@openmrs/esm-patient-common-lib');

return {
...originalModule,
launchPatientWorkspace: jest.fn(),
};
});

describe('StartVisit', () => {
beforeEach(() => {
jest.resetAllMocks();
});

test('should launch start visit form', () => {
spyOn(mockUseVisitDialog, 'useVisitDialog').and.returnValue({ type: 'prompt' });
render(<StartVisit patientUuid="some-uuid" />);

expect(
screen.getByText(
`You can't add data to the patient chart without an active visit. Choose from one of the options below to continue.`,
),
).toBeInTheDocument();

const startNewVisitButton = screen.getByRole('button', { name: /Start new visit/i });
userEvent.click(startNewVisitButton);

expect(launchPatientWorkspace).toHaveBeenCalledWith('start-visit-workspace-form');
});

test('should launch edit past visit form', () => {
spyOn(mockUseVisitDialog, 'useVisitDialog').and.returnValue({ type: 'prompt', state: { type: 'past' } });
render(<StartVisit patientUuid="some-uuid" />);

expect(
screen.getByText(
`You can add a new past visit or update an old one. Choose from one of the options below to continue.`,
),
).toBeInTheDocument();

const editPastVisitButton = screen.getByRole('button', { name: /Edit past visit/i });
userEvent.click(editPastVisitButton);

expect(launchPatientWorkspace).toHaveBeenCalledWith('past-visits-overview');
});
});

0 comments on commit 2e8bd49

Please sign in to comment.