Skip to content

Commit

Permalink
fix(xform): fix MeasureField writeValue method for null values (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
lvoliveira committed Nov 23, 2018
1 parent 9248068 commit 19a0faf
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/ng-xform/measure-field/measure-field.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,11 @@ export class MeasureFieldComponent extends BaseDynamicFieldComponent<MeasureFiel
}

writeValue(obj: Measure): void {
if (!obj) {
return;
if (!!obj) {
this.quantity = math.unit(obj.value, obj.unit).to(this.field.modelUnit);
} else {
this.quantity = undefined;
}
this.quantity = math.unit(obj.value, obj.unit).to(this.field.modelUnit);
this.updateInputValue();
}
registerOnChange(fn: any): void {
Expand Down Expand Up @@ -109,8 +110,14 @@ export class MeasureFieldComponent extends BaseDynamicFieldComponent<MeasureFiel
}

private updateInputValue() {
if (this.input) {
if (!this.input) {
return;
}

if (this.quantity) {
this.input.value = this.quantity.toNumber(this.viewUnit).toString();
} else {
this.input.value = undefined;
}
}

Expand Down
33 changes: 33 additions & 0 deletions src/ng-xform/ng-xform.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,39 @@ describe('NgXformComponent - Fields setup', () => {

});

it('should reset null measure field values', () => {
const fixture = createTestingModule();
expect(fixture.componentInstance).toBeTruthy();

let component: NgXformComponent = fixture.debugElement.componentInstance.form;

fixture.componentInstance.setFields([
new MeasureField({ key: 'field1', label: 'Field 1', modelUnit: 'm' }),
]);
fixture.detectChanges();

fixture.componentInstance.setModel({field1: {value: 10, unit: 'm'}});
fixture.detectChanges();

let el = fixture.debugElement.query(By.css('#field1-div .text-muted'));
expect(el).toBeTruthy();
expect(el.nativeElement.textContent).toEqual(' 10 m ');

fixture.componentInstance.setModel({field1: null});
fixture.detectChanges();

el = fixture.debugElement.query(By.css('#field1-div .text-muted'));
expect(el).toBeTruthy();
expect(el.nativeElement.textContent).toEqual(' - ');

fixture.componentInstance.editing = true;
fixture.detectChanges();

el = fixture.debugElement.query(By.css('#field1-div input'));
expect(el).toBeTruthy();
expect(el.nativeElement.value).toEqual('');
});

});

function createTestingModule(): ComponentFixture<any> {
Expand Down

0 comments on commit 19a0faf

Please sign in to comment.