Skip to content

Commit

Permalink
(fix) Fix: Correctly launch enrollment forms in edit or discontinuati…
Browse files Browse the repository at this point in the history
…on mode (#39)
  • Loading branch information
donaldkibet authored Sep 28, 2023
1 parent 63c4614 commit 99f3b28
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import React from 'react';
import { render, screen } from '@testing-library/react';
import { render, screen, fireEvent } from '@testing-library/react';
import ProgramEnrollment, { ProgramEnrollmentProps } from './program-enrollment.component';
import { showNotification } from '@openmrs/esm-framework';

const mockLaunchWorkspace = jest.fn();
const mockShowNotification = jest.fn();

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

describe('ProgramEnrollment Component', () => {
const mockProps: ProgramEnrollmentProps = {
Expand All @@ -21,7 +30,7 @@ describe('ProgramEnrollment Component', () => {
},
],
formEntrySub: jest.fn(),
launchPatientWorkspace: jest.fn(),
launchPatientWorkspace: mockLaunchWorkspace,
};

it('displays active program enrollment details correctly', () => {
Expand Down Expand Up @@ -69,4 +78,54 @@ describe('ProgramEnrollment Component', () => {
expect(screen.getByText(/Entry Point/i)).toBeInTheDocument();
expect(screen.getByText(/IPD/i)).toBeInTheDocument();
});

test('should show notification error when there is no encounter when launching forms', () => {
render(<ProgramEnrollment {...mockProps} />);

const editButton = screen.getByText(/Edit/i);
const deleteButton = screen.getByText(/Discontinue/i);

expect(editButton).toBeInTheDocument();
expect(deleteButton).toBeInTheDocument();

fireEvent.click(editButton);
expect(showNotification).toHaveBeenCalledWith({
title: 'Edit enrollment',
kind: 'error',
description:
'The enrollment form does not have an encounter associated with it, Please contact your system administrator to add an encounter to the enrollment',
});
});

test("should launch patient workspace when clicking on 'Edit' button", () => {
const updatedMockProps = {
...mockProps,
data: [
{
...mockProps.data[0],
data: [
{
...mockProps.data[0].data[0],
enrollmentFormUuid: 'encounter-123',
enrollmentFormName: 'some form name',
enrollmentEncounterUuid: 'some-encounter-uuid',
discontinuationFormUuid: 'discontinuation-123',
discontinuationFormName: 'some form name',
},
],
},
],
};
render(<ProgramEnrollment {...updatedMockProps} />);

// Trigger the edit button
const editButton = screen.getByText(/Edit/i);

fireEvent.click(editButton);
expect(mockLaunchWorkspace).toHaveBeenCalled();
expect(mockLaunchWorkspace).toHaveBeenCalledWith('patient-form-entry-workspace', {
formInfo: { encounterUuid: 'some-encounter-uuid', formUuid: 'encounter-123' },
workspaceTitle: 'some form name',
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useTranslation } from 'react-i18next';
import { Tile, Button, ButtonSet } from '@carbon/react';
import styles from './program-enrollment.scss';
import { Edit, TrashCan, Add } from '@carbon/react/icons';
import { useLayoutType, useVisit } from '@openmrs/esm-framework';
import { showNotification, useLayoutType } from '@openmrs/esm-framework';
import isNull from 'lodash-es/isNull';
import { ProgramType } from '../types';
export interface ProgramEnrollmentProps {
Expand All @@ -14,25 +14,27 @@ export interface ProgramEnrollmentProps {
launchPatientWorkspace: Function;
}
const ProgramEnrollment: React.FC<ProgramEnrollmentProps> = ({
patientUuid,
programName,
data,
formEntrySub,

launchPatientWorkspace,
}) => {
const { t } = useTranslation();
const isTablet = useLayoutType() == 'tablet';
const { currentVisit } = useVisit(patientUuid);
const handleOpenWorkspace = (formUuid, formName, encounterUuid) => {
const mutateForm = () => {};
formEntrySub.next({ formUuid, encounterUuid });
launchPatientWorkspace('patient-form-entry-workspace', {
workspaceTitle: formName,
mutateForm,
formUuid,
encounterUuid,
visit: currentVisit,
});
!encounterUuid
? showNotification({
title: t('editEnrollment', 'Edit enrollment'),
description: t(
'editEnrollmentMessage',
'The enrollment form does not have an encounter associated with it, Please contact your system administrator to add an encounter to the enrollment',
),
kind: 'error',
})
: launchPatientWorkspace('patient-form-entry-workspace', {
workspaceTitle: formName,
formInfo: { formUuid, encounterUuid: encounterUuid ?? '' },
});
};
if (isNull(data)) {
return;
Expand Down

0 comments on commit 99f3b28

Please sign in to comment.