Skip to content

Commit

Permalink
Fix Replication of participants every time care team is edited. (#1175)
Browse files Browse the repository at this point in the history
* Fix Bug where practitioner names were rendered as objects

* Style tweak on view details page

* Fix bug that replicates practitioners on every careteam edit

* Remove surplus form.resetFields useEffect

* Fix bug where removed practitioners and orgs still show up

* Deprecate and remove care team util test file
  • Loading branch information
peterMuriuki authored Feb 16, 2023
1 parent 28e363b commit a65b768
Show file tree
Hide file tree
Showing 10 changed files with 901 additions and 240 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,7 @@ const CareTeamForm: React.FC<CareTeamFormProps> = (props: CareTeamFormProps) =>
initialValues={initialValues}
onFinish={(values: FormFields) => {
setIsSubmitting(true);
submitForm(
{ ...initialValues, ...values },
fhirBaseURL,
organizations,
practitioners,
t,
initialValues.id,
initialValues.uuid
)
submitForm(values, initialValues, fhirBaseURL, organizations, practitioners, t)
.catch(() => sendErrorNotification(t('An error occurred')))
.finally(() => setIsSubmitting(false));
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ const CreateEditCareTeam: React.FC<CreateEditCareTeamProps> = (props: CreateEdit
onError: () => sendErrorNotification(t('An error occurred')),
select: (res) => res,
enabled: !!careTeamId,
cacheTime: 0,
staleTime: 0,
}
);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { ICareTeam } from '@smile-cdr/fhirts/dist/FHIR-R4/interfaces/ICareTeam';

export const careTeam1 = {
resourceType: 'CareTeam',
id: '308',
Expand Down Expand Up @@ -114,7 +116,7 @@ export const careTeam4201 = {
reference: 'Organization/4190',
},
],
};
} as unknown as ICareTeam;

export const careTeam4201Edited = {
resourceType: 'CareTeam',
Expand Down Expand Up @@ -146,6 +148,155 @@ export const careTeam4201Edited = {
identifier: [{ use: 'official', value: '0b3a3311-6f5a-40dd-95e5-008001acebe1' }],
};

export const careTeam4201alternative = {
resourceType: 'CareTeam',
id: '4201',
meta: {
versionId: '2',
lastUpdated: '2021-11-01T10:33:47.409+00:00',
source: '#3671f89bf6e8bf20',
},
text: {
status: 'generated',
div: '<div xmlns="http://www.w3.org/1999/xhtml">Care Team</div>',
},
contained: [
{
resourceType: 'Practitioner',
id: '4193',
name: [
{
family: 'Careful',
given: ['Adam'],
prefix: ['Dr'],
},
],
},
],
status: 'active',
category: [
{
coding: [
{
system: 'http://loinc.org',
code: 'LA27976-2',
display: 'Encounter-focused care team',
},
],
},
],
name: 'Peter Charlmers Care team',
subject: {
reference: 'Patient/4195',
display: 'Peter James Chalmers',
},
encounter: {
reference: 'Encounter/4197',
},
period: {
end: '2013-01-01',
},
participant: [
{
role: [
{
text: 'responsiblePerson',
},
],
member: {
reference: 'Patient/4195',
display: 'Peter James Chalmers',
},
},
{
role: [
{
text: 'responsiblePerson',
},
],
member: {
reference: '#pr1',
display: 'Dorothy Dietition',
},
onBehalfOf: {
reference: 'Organization/f001',
},
period: {
end: '2013-01-01',
},
},
{ member: { reference: 'Practitioner/102', display: 'Ward N 2 Williams MD' } },
{
role: [
{
coding: [
{
system: 'http://snomed.info/sct',
code: '394730007',
display: 'Healthcare related organization',
},
],
},
],
member: { display: 'Test Team 70', reference: 'Organization/368' },
},
],
managingOrganization: [{ display: 'Test Team 70', reference: 'Organization/368' }],
} as unknown as ICareTeam;

export const careTeam4201alternativeEdited = {
resourceType: 'CareTeam',
id: '4201',
contained: [
{
resourceType: 'Practitioner',
id: '4193',
name: [{ family: 'Careful', given: ['Adam'], prefix: ['Dr'] }],
},
],
status: 'inactive',
category: [
{
coding: [
{ system: 'http://loinc.org', code: 'LA27976-2', display: 'Encounter-focused care team' },
],
},
],
name: 'Peter Charlmers Care teamcare team',
subject: { reference: 'Patient/4195', display: 'Peter James Chalmers' },
encounter: { reference: 'Encounter/4197' },
period: { end: '2013-01-01' },
managingOrganization: [{ display: 'testing ash123', reference: 'Organization/319' }],
identifier: [{ use: 'official', value: '9b782015-8392-4847-b48c-50c11638656b' }],
participant: [
{
role: [{ text: 'responsiblePerson' }],
member: { reference: 'Patient/4195', display: 'Peter James Chalmers' },
},
{
role: [{ text: 'responsiblePerson' }],
member: { reference: '#pr1', display: 'Dorothy Dietition' },
onBehalfOf: { reference: 'Organization/f001' },
period: { end: '2013-01-01' },
},
{ member: { reference: 'Practitioner/103', display: 'Ward N 1 Williams MD' } },
{
role: [
{
coding: [
{
system: 'http://snomed.info/sct',
code: '394730007',
display: 'Healthcare related organization',
},
],
},
],
member: { display: 'testing ash123', reference: 'Organization/319' },
},
],
};

export const practitioners = {
resourceType: 'Bundle',
id: '5db4aa7e-eca6-432d-a544-d0a8279d8103',
Expand Down Expand Up @@ -1513,6 +1664,7 @@ export const createdCareTeam = {
name: 'Care team 1',
status: 'active',
participant: [
{ member: { reference: 'Practitioner/102', display: 'Ward N 2 Williams MD' } },
{
role: [
{
Expand All @@ -1525,8 +1677,34 @@ export const createdCareTeam = {
],
},
],
member: { reference: 'Organization/368', display: 'Test Team 70' },
member: { display: 'Test Team 70', reference: 'Organization/368' },
},
],
managingOrganization: [{ display: 'Test Team 70', reference: 'Organization/368' }],
};

export const createdCareTeam2 = {
resourceType: 'CareTeam',
identifier: [{ use: 'official', value: '9b782015-8392-4847-b48c-50c11638656b' }],
id: '9b782015-8392-4847-b48c-50c11638656b',
name: 'care team',
status: 'inactive',
participant: [
{ member: { reference: 'Practitioner/102', display: 'Ward N 2 Williams MD' } },
{
role: [
{
coding: [
{
system: 'http://snomed.info/sct',
code: '394730007',
display: 'Healthcare related organization',
},
],
},
],
member: { reference: 'Organization/368', display: 'Test Team 70' },
},
],
managingOrganization: [{ reference: 'Organization/368', display: 'Test Team 70' }],
};
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
import React from 'react';
import { mount } from 'enzyme';
import flushPromises from 'flush-promises';
import * as fixtures from './fixtures';
import { act } from 'react-dom/test-utils';
import { CareTeamForm } from '../Form';
import { defaultInitialValues } from '../utils';
import { defaultInitialValues, getCareTeamFormFields } from '../utils';
import { getResourcesFromBundle } from '@opensrp/react-utils';
import { cleanup, fireEvent, waitFor } from '@testing-library/react';
import { cleanup, fireEvent, waitFor, render, screen } from '@testing-library/react';
import userEvents from '@testing-library/user-event';
import * as notifications from '@opensrp/notifications';
import nock from 'nock';
import { careTeamResourceType } from '../../../constants';
import { createdCareTeam } from './fixtures';
import {
createdCareTeam,
createdCareTeam2,
careTeam4201alternativeEdited,
organizations,
practitioners,
careTeam4201alternative,
} from './fixtures';
import { store } from '@opensrp/store';
import { authenticateUser } from '@onaio/session-reducer';

Expand Down Expand Up @@ -60,8 +66,8 @@ afterEach(() => {
const props = {
initialValues: defaultInitialValues,
fhirBaseURL: 'https://r4.smarthealthit.org/',
practitioners: getResourcesFromBundle(fixtures.practitioners),
organizations: getResourcesFromBundle(fixtures.organizations),
practitioners: getResourcesFromBundle(practitioners),
organizations: getResourcesFromBundle(organizations),
};

test('filter select by text able to create new careteam', async () => {
Expand Down Expand Up @@ -193,9 +199,111 @@ test('filter select by text able to create new careteam', async () => {
wrapper.find('form').simulate('submit');

await waitFor(() => {
expect(successNoticeMock.mock.calls).toEqual([['Successfully Added Care Teams']]);
expect(successNoticeMock.mock.calls).toEqual([['Successfully added CareTeams']]);
});

expect(nock.isDone()).toBeTruthy();
wrapper.unmount();
});

test('1157 - Create care team works corectly', async () => {
const successNoticeMock = jest
.spyOn(notifications, 'sendSuccessNotification')
.mockImplementation(() => undefined);

nock(props.fhirBaseURL)
.put(`/${careTeamResourceType}/${createdCareTeam2.id}`, createdCareTeam2)
.reply(200)
.persist();

render(<CareTeamForm {...props} />);

await waitFor(() => {
expect(screen.getByText(/Create Care Team/)).toBeInTheDocument();
});

const nameInput = screen.getByLabelText('Name') as Element;
userEvents.type(nameInput, 'care team');

const activeStatusRadio = screen.getByLabelText('Active');
expect(activeStatusRadio).toBeChecked();

const inactiveStatusRadio = screen.getByLabelText('Inactive');
expect(inactiveStatusRadio).not.toBeChecked();
userEvents.click(inactiveStatusRadio);

const practitionersInput = screen.getByLabelText('Practitioner Participant');
userEvents.click(practitionersInput);
fireEvent.click(screen.getByTitle('Ward N 2 Williams MD'));

const managingOrgsSelect = screen.getByLabelText('Managing organizations');
userEvents.click(managingOrgsSelect);
fireEvent.click(screen.getByTitle('Test Team 70'));

const saveBtn = screen.getByRole('button', { name: 'Save' });
userEvents.click(saveBtn);

await waitFor(() => {
expect(successNoticeMock.mock.calls).toEqual([['Successfully added CareTeams']]);
});

expect(nock.isDone()).toBeTruthy();
});

test('1157 - editing care team works corectly', async () => {
const thisProps = {
...props,
initialValues: getCareTeamFormFields(careTeam4201alternative),
};
const successNoticeMock = jest
.spyOn(notifications, 'sendSuccessNotification')
.mockImplementation(() => undefined);

nock(props.fhirBaseURL)
.put(
`/${careTeamResourceType}/${careTeam4201alternativeEdited.id}`,
careTeam4201alternativeEdited
)
.reply(200)
.persist();

render(<CareTeamForm {...thisProps} />);

await waitFor(() => {
expect(screen.getByText(/Edit Care Team /)).toBeInTheDocument();
});

const nameInput = screen.getByLabelText('Name') as Element;
userEvents.type(nameInput, 'care team');

const activeStatusRadio = screen.getByLabelText('Active');
expect(activeStatusRadio).toBeChecked();

const inactiveStatusRadio = screen.getByLabelText('Inactive');
expect(inactiveStatusRadio).not.toBeChecked();
userEvents.click(inactiveStatusRadio);

// remove assigned
const selectClear = [...document.querySelectorAll('.ant-select-selection-item-remove')];
expect(selectClear).toHaveLength(2);
selectClear.forEach((clear) => {
fireEvent.click(clear);
});

const practitionersInput = screen.getByLabelText('Practitioner Participant');
userEvents.click(practitionersInput);
fireEvent.click(screen.getByTitle('Ward N 1 Williams MD'));

const managingOrgsSelect = screen.getByLabelText('Managing organizations');
userEvents.click(managingOrgsSelect);
fireEvent.click(screen.getByTitle('testing ash123'));

const saveBtn = screen.getByRole('button', { name: 'Save' });
userEvents.click(saveBtn);

await waitFor(() => {
expect(successNoticeMock.mock.calls).toEqual([['Successfully updated CareTeams']]);
});

expect(nock.isDone()).toBeTruthy();
});
Loading

0 comments on commit a65b768

Please sign in to comment.