Skip to content
This repository has been archived by the owner on May 7, 2021. It is now read-only.

fix(inlineinput): float value validation for user input #2816

Merged
merged 8 commits into from
Dec 13, 2018
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/app/widgets/inlineinput/inlineinput.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,16 @@ export class InlineInputComponent implements OnInit {
}
}

isFloat(x) {
return typeof x === 'number' && Number.isFinite(x) && x >= -2147483648 && x <= 2147483648;
}

validateValue(value) {
if (this.type === 'integer') {
return /^\d+$/.test(value);
}
if (this.type === 'float') {
return /^-?\d*(\.\d+)?$/.test(value);
return this.isFloat(value);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@abhinandan13jan This method is still failing because value is of type string. value needs to be parsed first for this to work.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sahil143 I was trying not to alter any preexisting functionalities. Now, I've added a .focus() method for the validation failure case. Can you please check again if it looks ok now?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@joshuawilson I've updated using sematic release format

}
return true;
}
Expand Down
37 changes: 37 additions & 0 deletions src/app/widgets/inlineinput/inlineinput.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { async } from '@angular/core/testing';
import { InlineInputComponent } from './inlineinput.component';

describe('Unit Test :: Inline Input Float value', () => {
let comp = new InlineInputComponent();
beforeEach(async(() => {
comp.type = 'float';
}));

it('Should return true for input in float range', () => {
expect(comp.validateValue(22.5)).toBe(true);
});

it('Should return true for input in float range', () => {
expect(comp.validateValue(1.2)).toBe(true);
});

it('Should return true for input in float range', () => {
expect(comp.validateValue(223456.334)).toBe(true);
});

it('Should return true for input in float range', () => {
expect(comp.validateValue(45)).toBe(true);
});

it('Should return false for input outside float range', () => {
expect(comp.validateValue(-34567812322.5)).toBe(false);
});

it('Should return false for input outside float range', () => {
expect(comp.validateValue('stringtest')).toBe(false);
});

it('Should return false for input outside float range', () => {
expect(comp.validateValue(11111111111111111.51)).toBe(false);
});
});