Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancements to the Allergies Form #487

Merged
merged 4 commits into from
Jan 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 57 additions & 46 deletions __mocks__/allergies.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1011,58 +1011,69 @@ export const mockPatientAllergyResult = {
};

export const mockAllergyResult = {
data: {
display: 'ARBs (angiotensin II receptor blockers)',
uuid: '90c17541-833d-419e-b5d3-bc06828bf95f',
allergen: {
allergenType: 'DRUG',
codedAllergen: {
display: 'ARBs (angiotensin II receptor blockers)',
uuid: '921fbd85-fa49-46c3-9ee1-77e093fd10a5',
},
nonCodedAllergen: null,
display: 'ACE inhibitors',
uuid: 'dbba59ef-c8a5-4967-b20a-5761b1954f6d',
allergen: {
allergenType: 'DRUG',
codedAllergen: {
uuid: '162298AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA',
display: 'ACE inhibitors',
links: [
{
rel: 'self',
uri: 'http://localhost:8090/openmrslocalhost:8080/openmrs/ws/rest/v1/concept/162298AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA',
},
],
},
severity: {
uuid: '1498AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA',
display: 'Mild',
name: {
display: 'Mild',
uuid: '1738BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB',
name: 'Mild',
locale: 'en',
localePreferred: true,
conceptNameType: 'FULLY_SPECIFIED',
nonCodedAllergen: null,
},
severity: {
uuid: '1498AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA',
display: 'Mild',
links: [
{
rel: 'self',
uri: 'http://localhost:8090/openmrslocalhost:8080/openmrs/ws/rest/v1/concept/1498AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA',
},
],
},
comment: null,
reactions: [
{
reaction: {
uuid: '143264AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA',
display: 'Cough',
links: [
{
rel: 'self',
uri: 'http://localhost:8090/openmrslocalhost:8080/openmrs/ws/rest/v1/concept/143264AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA',
},
],
},
reactionNonCoded: null,
},
comment: 'The patient is showing a mild reaction to the above allergens',
reactions: [
],
patient: {
uuid: '011cffa0-7383-45ef-962c-d1976718b8d4',
display: '103H22 - test test test',
links: [
{
reaction: {
uuid: '121677AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA',
display: 'Mental status change',
name: {
display: 'Mental status change',
uuid: '127084BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB',
name: 'Mental status change',
locale: 'en',
localePreferred: true,
conceptNameType: null,
},
},
rel: 'self',
uri: 'http://localhost:8090/openmrslocalhost:8080/openmrs/ws/rest/v1/patient/011cffa0-7383-45ef-962c-d1976718b8d4',
},
],
patient: {
uuid: '90f7f0b4-06a8-4a97-9678-e7a977f4b518',
display: '10010W - John Taylor',
},
links: [
{
rel: 'self',
uri: 'http://localhost:8090/openmrslocalhost:8080/openmrs/ws/rest/v1/patient/011cffa0-7383-45ef-962c-d1976718b8d4/allergy/dbba59ef-c8a5-4967-b20a-5761b1954f6d',
},
auditInfo: {
creator: {
uuid: '285f67ce-3d8b-4733-96e5-1e2235e8e804',
display: 'doc',
},
dateChanged: '2020-01-03T07:05:12.000+0000',
{
rel: 'full',
uri: 'http://localhost:8090/openmrslocalhost:8080/openmrs/ws/rest/v1/patient/011cffa0-7383-45ef-962c-d1976718b8d4/allergy/dbba59ef-c8a5-4967-b20a-5761b1954f6d?v=full',
},
},
],
resourceVersion: '1.8',
};

export const mockUpdatedAllergyResult = {
Expand Down Expand Up @@ -1689,7 +1700,7 @@ export const mockFhirAllergyIntoleranceResponse = {
],
};

export const mockAllergenAndReactions = {
export const mockAllergensAndAllergicReactions = {
drugAllergens: [
{ uuid: '162298AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA', display: 'ACE inhibitors' },
{ uuid: '162299AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA', display: 'ARBs (angiotensin II receptor blockers)' },
Expand Down Expand Up @@ -1730,7 +1741,7 @@ export const mockAllergenAndReactions = {
{ uuid: '162542AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA', display: 'Adhesive tape' },
{ uuid: '5622AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA', display: 'Other' },
],
allergyReaction: [
allergicReactions: [
{ uuid: '121677AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA', display: 'Mental status change' },
{ uuid: '121629AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA', display: 'Anaemia' },
{ uuid: '148888AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA', display: 'Anaphylaxis' },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import React from 'react';
import dayjs from 'dayjs';
import { useTranslation } from 'react-i18next';
import { CardHeader, EmptyState, ErrorState, launchPatientWorkspace } from '@openmrs/esm-patient-common-lib';
import { useAllergies } from './allergy-intolerance.resource';
import Add16 from '@carbon/icons-react/es/add/16';
import {
Button,
Expand All @@ -18,19 +17,19 @@ import {
TableRow,
} from 'carbon-components-react';
import styles from './allergies-detailed-summary.scss';
import { useAllergies } from './allergy-intolerance.resource';
import { patientAllergiesFormWorkspace } from '../constants';

interface AllergiesDetailedSummaryProps {
patient: fhir.Patient;
showAddAllergy: boolean;
showAddAllergyButton: boolean;
}

const AllergiesDetailedSummary: React.FC<AllergiesDetailedSummaryProps> = ({ patient, showAddAllergy }) => {
const AllergiesDetailedSummary: React.FC<AllergiesDetailedSummaryProps> = ({ patient, showAddAllergyButton }) => {
const { t } = useTranslation();
const displayText = t('allergyIntolerances', 'allergy intolerances');
const headerTitle = t('allergies', 'Allergies');

const { data: allergies, isError, isLoading, isValidating } = useAllergies(patient.id);
const { allergies, isError, isLoading, isValidating } = useAllergies(patient.id);

const launchAllergiesForm = React.useCallback(() => launchPatientWorkspace(patientAllergiesFormWorkspace), []);

Expand Down Expand Up @@ -75,8 +74,8 @@ const AllergiesDetailedSummary: React.FC<AllergiesDetailedSummaryProps> = ({ pat
</div>
),
},
recordedDate: dayjs(allergy.recordedDate).format('MMM-YYYY') ?? '-',
lastUpdated: dayjs(allergy.lastUpdated).format('DD-MMM-YYYY'),
recordedDate: dayjs(allergy.recordedDate).format('MMM-YYYY') ?? '--',
lastUpdated: allergy.lastUpdated ? dayjs(allergy.lastUpdated).format('DD-MMM-YYYY') : '--',
}));
}, [allergies]);

Expand All @@ -87,7 +86,7 @@ const AllergiesDetailedSummary: React.FC<AllergiesDetailedSummaryProps> = ({ pat
<div className={styles.widgetCard}>
<CardHeader title={headerTitle}>
<span>{isValidating ? <InlineLoading /> : null}</span>
{showAddAllergy && (
{showAddAllergyButton && (
<Button kind="ghost" renderIcon={Add16} iconDescription="Add allergies" onClick={launchAllergiesForm}>
{t('add', 'Add')}
</Button>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import AllergiesDetailedSummary from './allergies-detailed-summary.component';

const testProps = {
patient: mockPatient,
showAddAllergy: false,
showAddAllergyButton: false,
};

const mockOpenmrsFetch = openmrsFetch as jest.Mock;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import React from 'react';
import userEvent from '@testing-library/user-event';
import { screen, render, act } from '@testing-library/react';
import { of } from 'rxjs/internal/observable/of';
import { showNotification, showToast, useConfig } from '@openmrs/esm-framework';
import { mockPatient } from '../../../../../__mocks__/patient.mock';
import { mockAllergensAndAllergicReactions, mockAllergyResult } from '../../../../../__mocks__/allergies.mock';
import { fetchAllergensAndAllergicReactions, saveAllergy } from './allergy-form.resource';
import AllergyForm from './allergy-form.component';

const mockUseConfig = useConfig as jest.Mock;
const mockFetchAllergensAndAllergicReactions = fetchAllergensAndAllergicReactions as jest.Mock;
const mockSaveAllergy = saveAllergy as jest.Mock;
const mockShowNotification = showNotification as jest.Mock;
const mockShowToast = showToast as jest.Mock;

jest.mock('./allergy-form.resource', () => ({
fetchAllergensAndAllergicReactions: jest.fn(),
saveAllergy: jest.fn(),
}));

describe('AllergiesForm: ', () => {
it('renders the Record Allergy form', () => {
mockFetchAllergensAndAllergicReactions.mockReturnValueOnce(of(mockAllergensAndAllergicReactions));

renderAllergyForm();

expect(screen.getByRole('heading', { name: /allergens and reactions/i })).toBeInTheDocument();
expect(screen.getByRole('heading', { name: /select the allergens/i })).toBeInTheDocument();
expect(screen.getByRole('heading', { name: /select the reactions/i })).toBeInTheDocument();
expect(screen.getByRole('heading', { name: /severity and date of onset/i })).toBeInTheDocument();
expect(screen.getByRole('heading', { name: /severity of worst reaction/i })).toBeInTheDocument();
expect(screen.getByRole('heading', { name: /date and comments/i })).toBeInTheDocument();

const tabNames = [/drug/i, /food/i, /environmental/i];
tabNames.map((tabName) => expect(screen.getByRole('tab', { name: tabName })).toBeInTheDocument());

expect(screen.getByRole('radio', { name: /ace inhibitors/i })).toBeInTheDocument();
expect(screen.getByRole('radio', { name: /mild/i })).toBeInTheDocument();
expect(screen.getByRole('radio', { name: /moderate/i })).toBeInTheDocument();
expect(screen.getByRole('radio', { name: /severe/i })).toBeInTheDocument();
expect(screen.getByRole('checkbox', { name: /anaemia/i })).toBeInTheDocument();
expect(screen.getByRole('textbox', { name: /date of first onset/i })).toBeInTheDocument();
expect(screen.getByRole('textbox', { name: /comments/i })).toBeInTheDocument();
expect(screen.getByRole('button', { name: /discard/i })).toBeInTheDocument();
expect(screen.getByRole('button', { name: /save and close/i })).toBeInTheDocument();
expect(screen.getByRole('button', { name: /save and close/i })).toBeDisabled();
});

describe('Form submission: ', () => {
it('renders a success notification after successful submission', async () => {
const promise = Promise.resolve();
mockFetchAllergensAndAllergicReactions.mockReturnValueOnce(of(mockAllergensAndAllergicReactions));
mockSaveAllergy.mockReturnValueOnce(
Promise.resolve({ data: mockAllergyResult, status: 201, statusText: 'Created' }),
);

renderAllergyForm();

userEvent.click(screen.getByRole('radio', { name: /ace inhibitors/i }));
userEvent.click(screen.getByRole('checkbox', { name: /cough/i }));
userEvent.click(screen.getByRole('radio', { name: /moderate/i }));
userEvent.type(screen.getByRole('textbox', { name: /date of first onset/i }), '02/01/2022');
userEvent.click(screen.getByRole('button', { name: /save and close/i }));

await act(() => promise);

expect(mockShowToast).toHaveBeenCalledTimes(1);
expect(mockShowToast).toHaveBeenCalledWith({
critical: true,
kind: 'success',
title: 'Allergy saved',
description: 'It is now visible on the Allergies page',
});
});

it('renders an error notification upon an invalid submission', async () => {
const promise = Promise.resolve();
mockFetchAllergensAndAllergicReactions.mockReturnValueOnce(of(mockAllergensAndAllergicReactions));
mockSaveAllergy.mockRejectedValueOnce({
message: 'Internal Server Error',
response: {
status: 500,
statusText: 'Internal Server Error',
},
});

renderAllergyForm();

userEvent.click(screen.getByRole('radio', { name: /ace inhibitors/i }));
userEvent.click(screen.getByRole('checkbox', { name: /cough/i }));
userEvent.click(screen.getByRole('radio', { name: /moderate/i }));
userEvent.type(screen.getByRole('textbox', { name: /date of first onset/i }), '02/01/2022');
userEvent.click(screen.getByRole('button', { name: /save and close/i }));

await act(() => promise);

expect(mockShowNotification).toHaveBeenCalledTimes(1);
expect(mockShowNotification).toHaveBeenCalledWith({
critical: true,
description: 'Internal Server Error',
kind: 'error',
title: 'Error saving allergy',
});
});
});
});

const testProps = {
closeWorkspace: () => {},
isTablet: false,
patient: mockPatient,
patientUuid: mockPatient.id,
};

function renderAllergyForm() {
mockUseConfig.mockReturnValue({
concepts: {
drugAllergenUuid: '162552AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA',
environmentalAllergenUuid: '162554AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA',
foodAllergenUuid: '162553AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA',
mildReactionUuid: '1498AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA',
moderateReactionUuid: '1499AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA',
severeReactionUuid: '1500AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA',
allergyReactionUuid: '162555AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA',
otherConceptUuid: '5622AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA',
},
});

render(<AllergyForm {...testProps} />);
}
Loading