[AAE-10911] Fix Cardview Int validator for only spaces (#7889)

* [AAE-10911] Fix Cardview Int validator for only spaces

* [AAE-10911] added unit tests
This commit is contained in:
Tomasz Gnyp
2022-10-12 13:42:55 +02:00
committed by GitHub
parent d1805226fc
commit 508ea87621
2 changed files with 32 additions and 14 deletions

View File

@@ -735,21 +735,35 @@ describe('CardViewTextItemComponent', () => {
});
}));
it('should show validation error for empty string', fakeAsync((done) => {
it('should show validation error for only spaces string', async () => {
updateTextField(component.property.key, ' ');
await fixture.whenStable();
fixture.detectChanges();
fixture.whenStable().then(() => {
updateTextField(component.property.key, ' ');
tick(600);
fixture.detectChanges();
fixture.whenStable().then(() => {
const error = getTextFieldError(component.property.key);
expect(error).toEqual('CORE.CARDVIEW.VALIDATORS.INT_VALIDATION_ERROR');
expect(component.property.value).toBe(10);
done();
});
});
}));
const errorMessage = getTextFieldError(component.property.key);
expect(errorMessage).toEqual('CORE.CARDVIEW.VALIDATORS.INT_VALIDATION_ERROR');
});
it('should NOT show validation error for empty string', async () => {
updateTextField(component.property.key, '');
await fixture.whenStable();
fixture.detectChanges();
const errorElement = fixture.debugElement.query(By.css('[data-automation-id="card-textitem-error-textkey"]'));
expect(errorElement).toBeNull();
});
it('should NOT show validation error for null', async () => {
updateTextField(component.property.key, null);
await fixture.whenStable();
fixture.detectChanges();
const inputElement = fixture.debugElement.query(By.css('[data-automation-id="card-textitem-value-textkey"]'));
const errorElement = fixture.debugElement.query(By.css('[data-automation-id="card-textitem-error-textkey"]'));
expect(inputElement.nativeElement.value).toBe('');
expect(errorElement).toBeNull();
});
it('should show validation error for float number', fakeAsync((done) => {
fixture.detectChanges();

View File

@@ -26,11 +26,15 @@ export class CardViewItemIntValidator implements CardViewItemValidator {
return value.every(this.isIntegerNumber);
}
return value === '' || !isNaN(value) && this.isIntegerNumber(value);
return value === '' || !isNaN(value) && this.isIntegerNumber(value) && this.isNotOnlySpace(value);
}
isIntegerNumber(value: any): boolean {
const parsedNumber = Number(value);
return (parsedNumber | 0) === parsedNumber;
}
isNotOnlySpace(value: any): boolean {
return String(value).trim() !== '';
}
}