Skip to content

Commit

Permalink
Merge pull request #1908 from techmatters/CHI-2317-refactor_case_redux
Browse files Browse the repository at this point in the history
CHI-2317: Refactor case redux
  • Loading branch information
stephenhand authored Dec 13, 2023
2 parents 9ca4bb7 + cdff33a commit 8ea0f64
Show file tree
Hide file tree
Showing 73 changed files with 1,527 additions and 1,916 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ import { newGoBackAction } from '../../../states/routing/actions';
import { ReferralLookupStatus } from '../../../states/contacts/resourceReferral';
import { VALID_EMPTY_CONTACT } from '../../testContacts';
import { configurationBase, connectedCaseBase, contactFormsBase, namespace } from '../../../states/storeNamespaces';
import { CaseState } from '../../../states/case/types';
import { RecursivePartial } from '../../RecursivePartial';

// eslint-disable-next-line react-hooks/rules-of-hooks
const { mockFetchImplementation, mockReset, buildBaseURL } = useFetchDefinitions();
Expand All @@ -60,16 +62,16 @@ expect.extend(toHaveNoViolations);
const mockStore = configureMockStore([]);
const baselineDate = new Date(1593469560208);

const addingNewHouseholdCaseState: RootState[typeof namespace][typeof connectedCaseBase] = {
tasks: {
task1: {
const addingNewHouseholdCaseState: CaseState = {
cases: {
case1: {
caseWorkingCopy: {
sections: {
households: {
new: {
createdAt: baselineDate.toISOString(),
twilioWorkerId: 'worker1',
id: '1',
id: 'case1',
form: {
age: '',
district: '',
Expand Down Expand Up @@ -109,7 +111,7 @@ const addingNewHouseholdCaseState: RootState[typeof namespace][typeof connectedC
};

const hrmState: Partial<RootState[typeof namespace]> = {
[configurationBase]: {
configuration: {
language: '',
currentDefinitionVersion: mockV1,
definitionVersions: {},
Expand All @@ -119,7 +121,7 @@ const hrmState: Partial<RootState[typeof namespace]> = {
hash: { worker1: 'worker1 name' },
},
},
[contactFormsBase]: {
activeContacts: {
isCallTypeCaller: false,
contactDetails: { contactSearch: { detailsExpanded: {} }, caseDetails: { detailsExpanded: {} } },
existingContacts: {
Expand Down Expand Up @@ -162,32 +164,32 @@ const hrmState: Partial<RootState[typeof namespace]> = {
},
},
},
[connectedCaseBase]: addingNewHouseholdCaseState,
connectedCase: addingNewHouseholdCaseState,
routing: {
tasks: {
task1: [{ route: 'case', subroute: 'household', action: CaseItemAction.Add }],
task1: [{ route: 'case', subroute: 'household', caseId: 'case1', action: CaseItemAction.Add }],
},
isAddingOfflineContact: false,
},
};

const state1 = {
const state1: RootState = {
[namespace]: hrmState,
};

const store1 = mockStore(state1);
store1.dispatch = jest.fn();

const state2 = {
const state2: RecursivePartial<RootState> = {
...state1,
[namespace]: {
...state1[namespace],
[connectedCaseBase]: addingNewHouseholdCaseState,
connectedCase: addingNewHouseholdCaseState,
routing: {
tasks: {
task1: [
{ route: 'case', subroute: 'household', action: CaseItemAction.View },
{ route: 'case', subroute: 'household', action: CaseItemAction.Add },
{ route: 'case', caseId: 'case1', subroute: 'household', action: CaseItemAction.View },
{ route: 'case', caseId: 'case1', subroute: 'household', action: CaseItemAction.Add },
],
},
},
Expand Down
58 changes: 26 additions & 32 deletions plugin-hrm-form/src/___tests__/components/case/Case.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,13 @@ import { DefinitionVersionId, loadDefinition, useFetchDefinitions } from 'hrm-fo

import { mockGetDefinitionsResponse, mockPartialConfiguration } from '../../mockGetConfig';
import Case from '../../../components/case';
import { RootState } from '../../../states';
import { HrmState, RootState } from '../../../states';
import { getDefinitionVersions } from '../../../hrmConfig';
import { Contact, StandaloneITask } from '../../../types/types';
import { Contact, StandaloneITask, standaloneTaskSid } from '../../../types/types';
import { LOAD_CONTACT_ACTION } from '../../../states/contacts/existingContacts';
import { VALID_EMPTY_CONTACT } from '../../testContacts';
import { RecursivePartial } from '../../RecursivePartial';
import {
configurationBase,
connectedCaseBase,
contactFormsBase,
namespace,
routingBase,
} from '../../../states/storeNamespaces';
import { namespace } from '../../../states/storeNamespaces';

jest.mock('../../../services/CaseService', () => ({ getActivities: jest.fn(() => []), cancelCase: jest.fn() }));
jest.mock('../../../permissions', () => ({
Expand All @@ -58,16 +52,16 @@ const { mockFetchImplementation, mockReset, buildBaseURL } = useFetchDefinitions
expect.extend(toHaveNoViolations);
const mockStore = configureMockStore([]);

function createState(state) {
function createState(state: RecursivePartial<HrmState>): RootState {
return {
[namespace]: state,
};
} as RootState;
}

let ownProps;

let mockV1;
let initialState: RecursivePartial<RootState>;
let initialState: RootState;

beforeEach(() => {
mockReset();
Expand Down Expand Up @@ -102,38 +96,37 @@ describe('useState mocked', () => {

beforeEach(() => {
initialState = createState({
[configurationBase]: {
configuration: {
counselors: {
list: [],
hash: { worker1: 'worker1 name' },
},
definitionVersions: { v1: mockV1 },
currentDefinitionVersion: mockV1,
},
[contactFormsBase]: {
activeContacts: {
existingContacts: {},
},
[connectedCaseBase]: {
tasks: {
task1: {
taskSid: 'task1',
connectedCase: {
cases: {
case1: {
connectedCase: {
id: 123,
id: '123',
createdAt: '2020-06-29T22:26:00.208Z',
updatedAt: '2020-06-29T22:26:00.208Z',
twilioWorkerId: 'worker1',
status: 'open',
info: { definitionVersion: 'v1' },
info: { definitionVersion: DefinitionVersionId.v1 },
connectedContacts: [connectedContact],
},
},
},
},
routing: { tasks: { task1: [{ route: 'case', subroute: 'home' }] } },
routing: { tasks: { [standaloneTaskSid]: [{ route: 'case', subroute: 'home', caseId: 'case1' }] } },
});

ownProps = {
task: initialState[namespace][connectedCaseBase].tasks.task1 as StandaloneITask,
task: { taskSid: standaloneTaskSid } as StandaloneITask,
};
});

Expand All @@ -143,28 +136,29 @@ describe('useState mocked', () => {

test('Case (should return null)', async () => {
initialState = createState({
[contactFormsBase]: {
activeContacts: {
existingContacts: {
contact1: {
savedContact: {
...VALID_EMPTY_CONTACT,
rawJson: {
...VALID_EMPTY_CONTACT.rawJson,
childInformation: {
name: { firstName: 'first', lastName: 'last' },
firstName: 'first',
lastName: 'last',
},
},
taskSid: 'task1',
taskId: 'task1',
},
metadata: {},
},
},
},
[connectedCaseBase]: {
tasks: {},
connectedCase: {
cases: {},
},
[routingBase]: { tasks: { task1: [{ route: 'case', subroute: 'home' }] } },
[configurationBase]: {
routing: { tasks: { [standaloneTaskSid]: [{ route: 'case', subroute: 'home', caseId: 'case1' }] } },
configuration: {
counselors: {
list: [],
hash: { worker1: 'worker1 name' },
Expand Down Expand Up @@ -216,8 +210,8 @@ describe('useState mocked', () => {
test('Contact name should render once contact is saved to redux', async () => {
const stateWithContact = createState({
...initialState[namespace],
[contactFormsBase]: {
...initialState[namespace][contactFormsBase],
activeContacts: {
...initialState[namespace].activeContacts,
existingContacts: {
contact1: {
savedContact: connectedContact,
Expand Down Expand Up @@ -248,7 +242,7 @@ describe('useState mocked', () => {
});

test('Case (should render, after update)', async () => {
initialState[namespace][connectedCaseBase].tasks.task1.connectedCase.updatedAt = '2020-06-29T22:29:00.208Z';
initialState[namespace].connectedCase.cases.case1.connectedCase.updatedAt = '2020-06-29T22:29:00.208Z';
const store = mockStore(initialState);
store.dispatch = jest.fn();

Expand Down
Loading

0 comments on commit 8ea0f64

Please sign in to comment.