Skip to content

Commit

Permalink
Merge pull request #610 from opensrp/608-fix-team-unassigning-bug
Browse files Browse the repository at this point in the history
608 fix team unassigning bug
  • Loading branch information
KipSigei authored Apr 27, 2021
2 parents a9174a7 + f2735ac commit 7d90f95
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 15 deletions.
2 changes: 1 addition & 1 deletion packages/team-assignment/src/components/ListView/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ const TeamAssignmentView = (props: TeamAssignmentViewProps) => {
.list({ plan: defaultPlanId })
.then((response: RawAssignment[]) => {
const parsedAssignments = processRawAssignments(response);
dispatch(fetchAssignments(parsedAssignments));
dispatch(fetchAssignments(parsedAssignments, assignmentsList.length > 0));
})
.catch(() => sendErrorNotification(lang.ERROR_OCCURED));

Expand Down
141 changes: 138 additions & 3 deletions packages/team-assignment/src/components/ListView/tests/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,13 @@ import {
reducerName as teamsReducerName,
} from '@opensrp/team-management';
import { OPENSRP_API_BASE_URL } from '../../../constants';
import { assignments, sampleHierarchy, sampleHierarchy2, samplePlan } from './fixtures';
import { organizations } from '@opensrp/team-management/src/ducks/tests/fixtures';
import {
assignments,
sampleHierarchy,
sampleHierarchy2,
samplePlan,
organizations,
} from './fixtures';
import toJson from 'enzyme-to-json';
import { Dictionary } from '@onaio/utils';

Expand Down Expand Up @@ -318,7 +323,7 @@ describe('List view Page', () => {
// modal text: save button, cancel button, placeholder and header text
expect(wrapper.find('Modal').text()).toMatchInlineSnapshot(
// eslint-disable-next-line no-irregular-whitespace
`"Assign/Unassign Teams | KenyaTeams Enter a Team nameSaveCancel"`
`"Assign/Unassign Teams | KenyaTeamsTeam 1Team 2ANC Test Team SaveCancel"`
);

// mock multi select on change event
Expand Down Expand Up @@ -356,6 +361,136 @@ describe('List view Page', () => {
wrapper.unmount();
});

it('correctly updates unassigning/assigning teams on table', async () => {
fetch.mockResponses(
/** Get plan */
[JSON.stringify(samplePlan), { status: 200 }],
/** These calls are made by TeamAssignment */
[JSON.stringify(assignments), { status: 200 }],
[JSON.stringify(organizations), { status: 200 }]
);

store.dispatch(fetchAssignments(assignments));
store.dispatch(fetchOrganizationsAction(organizations));
const hierarchy = generateJurisdictionTree(sampleHierarchy);
store.dispatch(fetchAllHierarchies([hierarchy.model]));

const props = {
history,
opensrpBaseURL: OPENSRP_API_BASE_URL,
defaultPlanId: '27362060-0309-411a-910c-64f55ede3758',
location: {
hash: '',
pathname: `${TEAM_ASSIGNMENT_LIST_VIEW_URL}`,
search: '',
state: {},
},
match: {
isExact: true,
params: {},
path: `${TEAM_ASSIGNMENT_LIST_VIEW_URL}`,
url: `${TEAM_ASSIGNMENT_LIST_VIEW_URL}`,
},
};

const wrapper = mount(
<Provider store={store}>
<Router history={history}>
<TeamAssignmentView {...props} />
</Router>
</Provider>
);

await act(async () => {
await flushPromises();
wrapper.update();
});
// simulate to open modal
wrapper.find('button').at(0).simulate('click');
wrapper.update();
// modal text: save button, cancel button, placeholder and header text
expect(wrapper.find('Modal').text()).toMatchInlineSnapshot(
// eslint-disable-next-line no-irregular-whitespace
`"Assign/Unassign Teams | KenyaTeamsTeam 1Team 2ANC Test Team SaveCancel"`
);

// mock multi select on change event
(wrapper.find('Modal Select').props() as Dictionary).onChange(
['c53900dd-cb8e-4f9f-befc-5b21742612a1'],
[{ label: 'Team 1', value: 'c53900dd-cb8e-4f9f-befc-5b21742612a1' }]
);
wrapper.update();
// filter props works correctly
expect(
(wrapper.find('Modal Select').props() as Dictionary).filterOption('Team 1', {
label: 'Team 1',
value: 'c53900dd-cb8e-4f9f-befc-5b21742612a1',
})
).toBeTruthy();

expect(
(wrapper.find('Modal Select').props() as Dictionary).filterOption('TseT', {
label: 'Team 1',
value: 'c53900dd-cb8e-4f9f-befc-5b21742612a1',
})
).toBeFalsy();

// save assignment successfully
expect(wrapper.find('button').at(1).text()).toEqual('Save');
(wrapper.find('Modal Select').props() as Dictionary).onChange(
['e740e6b8-98dc-4d99-af34-ab2eb602da00'],
[{ label: 'Team 2', value: 'e740e6b8-98dc-4d99-af34-ab2eb602da00' }]
);
wrapper.update();
wrapper.find('button').at(1).simulate('click');
wrapper.find('form').simulate('submit');
// check that the othe 2 teams have been unassigned
expect(wrapper.find('Modal').text()).toMatchInlineSnapshot(
// eslint-disable-next-line no-irregular-whitespace
`"Assign/Unassign Teams | KenyaTeamsTeam 2 SaveCancel"`
);
await flushPromises();
wrapper.update();
const payload = [
{
fromDate: '2021-01-19T21:00:00+00:00',
jurisdiction: 'b652b2f4-a95d-489b-9e28-4629746db96a',
organization: 'e740e6b8-98dc-4d99-af34-ab2eb602da00',
plan: '27362060-0309-411a-910c-64f55ede3758',
toDate: '2027-07-13T19:31:00+00:00',
},
{
fromDate: '2021-01-19T21:00:00+00:00',
jurisdiction: 'b652b2f4-a95d-489b-9e28-4629746db96a',
organization: 'c53900dd-cb8e-4f9f-befc-5b21742612a1',
plan: '27362060-0309-411a-910c-64f55ede3758',
toDate: '2017-07-13T19:31:00+00:00',
},
{
fromDate: '2021-01-19T21:00:00+00:00',
jurisdiction: 'b652b2f4-a95d-489b-9e28-4629746db96a',
organization: '2ea3733c-04fa-4136-b091-726ec3205422',
plan: '27362060-0309-411a-910c-64f55ede3758',
toDate: '2017-07-13T19:31:00+00:00',
},
];
expect(fetch.mock.calls[3]).toEqual([
'https://opensrp-staging.smartregister.org/opensrp/rest/organization/assignLocationsAndPlans',
{
'Cache-Control': 'no-cache',
Pragma: 'no-cache',
body: JSON.stringify(payload),
headers: {
accept: 'application/json',
authorization: 'Bearer sometoken',
'content-type': 'application/json;charset=UTF-8',
},
method: 'POST',
},
]);
wrapper.unmount();
});

it('closes modal on cancel button click', async () => {
fetch.mockResponses(
/** Get plan */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ describe('Assignment/helpers', () => {
jurisdiction: 'b652b2f4-a95d-489b-9e28-4629746db96a',
organization: '2',
plan: '27362060-0309-411a-910c-64f55ede3758',
toDate: '2040-12-30T00:00:00+00:00',
toDate: '2030-12-30T00:00:00+00:00',
},
{
fromDate: '2020-12-30T00:00:00+00:00',
jurisdiction: 'b652b2f4-a95d-489b-9e28-4629746db96a',
organization: '3',
plan: '27362060-0309-411a-910c-64f55ede3758',
toDate: '2050-12-30T00:00:00+00:00',
toDate: '2030-12-30T00:00:00+00:00',
},
]);
});
Expand Down Expand Up @@ -65,14 +65,14 @@ describe('Assignment/helpers', () => {
jurisdiction: 'b652b2f4-a95d-489b-9e28-4629746db96a',
organization: '2',
plan: '27362060-0309-411a-910c-64f55ede3758',
toDate: '2030-12-30T00:00:00+00:00',
toDate: '2020-12-30T00:00:00+00:00',
},
{
fromDate: '2020-12-30T00:00:00+00:00',
jurisdiction: 'b652b2f4-a95d-489b-9e28-4629746db96a',
organization: '3',
plan: '27362060-0309-411a-910c-64f55ede3758',
toDate: '2030-12-30T00:00:00+00:00',
toDate: '2020-12-30T00:00:00+00:00',
},
]);
});
Expand Down Expand Up @@ -108,21 +108,21 @@ describe('Assignment/helpers', () => {
jurisdiction: 'b652b2f4-a95d-489b-9e28-4629746db96a',
organization: 'eb6257cb-821c-46e9-bcee-14cb0101cc42',
plan: '27362060-0309-411a-910c-64f55ede3758',
toDate: '2040-12-30T00:00:00+00:00',
toDate: '2030-12-30T00:00:00+00:00',
},
{
fromDate: '2021-02-04T21:00:00+00:00',
jurisdiction: 'b652b2f4-a95d-489b-9e28-4629746db96a',
organization: '676bd889-e9ee-4f2b-94c3-0509466ad9be',
plan: '27362060-0309-411a-910c-64f55ede3758',
toDate: '2040-12-30T00:00:00+00:00',
toDate: '2020-12-30T00:00:00+00:00',
},
{
fromDate: '2021-01-19T21:00:00+00:00',
jurisdiction: 'b652b2f4-a95d-489b-9e28-4629746db96a',
organization: 'e740e6b8-98dc-4d99-af34-ab2eb602da00',
plan: '27362060-0309-411a-910c-64f55ede3758',
toDate: '2040-12-30T00:00:00+00:00',
toDate: '2020-12-30T00:00:00+00:00',
},
]);
});
Expand Down Expand Up @@ -151,14 +151,14 @@ describe('Assignment/helpers', () => {
jurisdiction: 'b652b2f4-a95d-489b-9e28-4629746db96a',
organization: '2',
plan: '27362060-0309-411a-910c-64f55ede3758',
toDate: '2030-12-30T00:00:00+00:00',
toDate: '2020-12-30T00:00:00+00:00',
},
{
fromDate: '2020-12-30T00:00:00+00:00',
jurisdiction: 'b652b2f4-a95d-489b-9e28-4629746db96a',
organization: '3',
plan: '27362060-0309-411a-910c-64f55ede3758',
toDate: '2030-12-30T00:00:00+00:00',
toDate: '2020-12-30T00:00:00+00:00',
},
]);
});
Expand Down
5 changes: 3 additions & 2 deletions packages/team-assignment/src/components/ListView/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ export const getPayload = (
): Assignment[] => {
const now = moment(new Date());
let startDate = now.format();
const endDate = now.add(10, 'year').format();

const payload: Assignment[] = [];
const assignmentsByOrgId = keyBy(existingAssignments, 'organization');
Expand All @@ -118,13 +119,13 @@ export const getPayload = (
jurisdiction: selectedJurisdictionId,
organization: orgId,
plan: selectedPlanId,
toDate: now.add(10, 'year').format(), // set a future date of 10 years
toDate: endDate, // set a future date of 10 years
});
}
}

// turns out if you put it in the loop it keeps subtracting a day for every iteration
const retireDate = now.format();
const retireDate = moment(new Date()).format();

for (const retiredOrgId of initialOrgs.filter((orgId) => !selectedOrgs.includes(orgId))) {
if (!payload.map((obj) => obj.organization).includes(retiredOrgId)) {
Expand Down

0 comments on commit 7d90f95

Please sign in to comment.