Skip to content

Commit

Permalink
[Parvathy, Rahul] | BAH-3582 | Fix. Inconsistent Error Handling In Ob…
Browse files Browse the repository at this point in the history
…sControl

Co-authored-by: Parvathy Babu <[email protected]>
  • Loading branch information
rahu1ramesh and parvathy00 committed Apr 9, 2024
1 parent fe0e1bc commit 0a09df7
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 7 deletions.
26 changes: 19 additions & 7 deletions src/components/ObsControl.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ export class ObsControl extends addMoreDecorator(Component) {

onChange({ value, errors, calledOnMount, triggerControlEvent }) {
const { metadata: { properties } } = this.props;

const isAbnormalPropertyEnabled = find(properties, (val, key) => (key === 'abnormal' && val));
const isAbnormal = find(errors, (err) => err.type === constants.errorTypes.warning
&& err.message === constants.validations.allowRange);
Expand All @@ -46,6 +45,9 @@ export class ObsControl extends addMoreDecorator(Component) {
interpretation = this.props.value.interpretation;
}
const obsValue = { value, comment: this.props.value.comment, interpretation };
this.setState({
errors,
});
this.props.onValueChanged(
this.props.formFieldPath,
obsValue,
Expand Down Expand Up @@ -196,14 +198,24 @@ export class ObsControl extends addMoreDecorator(Component) {
}

setAbnormal() {
const { value } = this.props;
const { value, onValueChanged, formFieldPath } = this.props;
const { errors } = this.state;
if (value.value) {
const interpretation = value.interpretation === 'ABNORMAL' ? null : 'ABNORMAL';
this.props.onValueChanged(
this.props.formFieldPath,
{ value: this.props.value.value, comment: this.props.value.comment, interpretation },
undefined
);
if (!errors || errors.length === 0) {
onValueChanged(
formFieldPath,
{ value: this.props.value.value, comment: this.props.value.comment, interpretation },
undefined
);
} else {
const hasCriticalError = errors.some(error => error.type === 'error');
onValueChanged(
formFieldPath,
{ value: this.props.value.value, comment: this.props.value.comment, interpretation },
hasCriticalError ? errors : undefined
);
}
}
}

Expand Down
51 changes: 51 additions & 0 deletions test/components/ObsControl.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -692,4 +692,55 @@ describe('ObsControl', () => {
expect(wrapper.find('.details')).text().to.eql('test value Heading goes here');
});
});

it('should set error in ValueChanged if the state has errors in it', () => {
const metadata = {
id: '100',
type: 'obsControl',
units: null,
hiNormal: 100,
lowNormal: 10,
hiAbsolute: 1000,
lowAbsolute: 0,
concept: getConcept('text'),
label,
properties: {
mandatory: false,
notes: false,
addMore: false,
hideLabel: false,
controlEvent: false,
location: {
column: 0,
row: 1,
},
abnormal: true,
},
};
const value = { value: 2000, comment: 'someComment', interpretation: null };
const wrapper = shallow(
<ObsControl
metadata={metadata}
onValueChanged={onChangeSpy}
showNotification={() => {}}
validate={false}
validateForm={false}
value={value}
/>
);
const errors = [{ type: 'error', message: 'Sample error message' }];
wrapper.setState({ errors });

expect(wrapper.find('button').text()).to.eql('Abnormal');

wrapper.find('button').simulate('click');
wrapper.instance().onChange({ value, errors });
sinon.assert.calledOnce(
onChangeSpy.withArgs(
undefined,
{ value: 2000, comment: 'someComment', interpretation: 'ABNORMAL' },
errors
)
);
});
});
35 changes: 35 additions & 0 deletions test/helpers/ObsList.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,39 @@ describe('ObsList', () => {

expect(obsList.isVoided()).to.deep.eql(false);
});

it('should clone for add more', () => {
const obs = new Obs({ concept, formFieldPath });
const nextFormFieldPath = 'nextFormFieldPath';
const formNameSpace = 'Bahmni';
const observationList = List.of(obs);
const obsList = new ObsList({ formFieldPath, obs, obsList: observationList, formNameSpace });
const updatedObsList = obsList.cloneForAddMore(nextFormFieldPath);
const expectedObsList = new ObsList({
formFieldPath: nextFormFieldPath,
formNameSpace,
obs: new Obs({ concept, formFieldPath: nextFormFieldPath }),
obsList: new List(),
});
expect(updatedObsList.toJS()).to.deep.eql(expectedObsList.toJS());
});
it('should void obsList', () => {
const obs = new Obs({ concept, formFieldPath });
const obs1 = new Obs({ concept, formFieldPath, value: 2 });
const obs2 = new Obs({ concept, formFieldPath });
const observationList = List.of(obs1, obs2);
const obsList = new ObsList({ formFieldPath, obs, obsList: observationList });
const voidedObsList = obsList.void();
expect(voidedObsList.obsList.get(0).voided).to.deep.eql(true);
expect(voidedObsList.obsList.get(0).value).to.deep.eql(undefined);
expect(voidedObsList.obsList.get(1).voided).to.deep.eql(true);
expect(voidedObsList.obsList.get(1).value).to.deep.eql(undefined);
});
it('should return flattened observation list', () => {
const obs = new Obs({ concept, formFieldPath });
const observationList = List.of(obs);
const obsList = new ObsList({ formFieldPath, obs, obsList: observationList });
const flattenedList = obsList.getObject(obsList);
expect(flattenedList).to.deep.eql([obs.toObject()]);
});
});

0 comments on commit 0a09df7

Please sign in to comment.