Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: force the validation to check for bad user input #4268

Merged
merged 1 commit into from
Jul 23, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
30 changes: 30 additions & 0 deletions packages/integer-field/test/validation.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { expect } from '@esm-bundle/chai';
import { fixtureSync, nextRender } from '@vaadin/testing-helpers';
import { sendKeys } from '@web/test-runner-commands';
import sinon from 'sinon';
import '../src/vaadin-integer-field.js';

Expand Down Expand Up @@ -66,4 +67,33 @@ describe('validation', () => {
expect(event.detail.valid).to.be.false;
});
});

describe('bad input', () => {
let input;

beforeEach(() => {
integerField = fixtureSync('<vaadin-integer-field></vaadin-integer-field>');
input = integerField.inputElement;
input.focus();
});

it('should be valid when committing a valid number', async () => {
await sendKeys({ type: '1' });
Copy link
Contributor Author

@vursen vursen Jul 22, 2022

Choose a reason for hiding this comment

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

I had no choice other than to use sendKeys here because programmatically setting an invalid value on number inputs is prevented by the browser.

input.blur();
expect(integerField.invalid).to.be.false;
});

it('should be invalid when trying to commit a not valid number', async () => {
await sendKeys({ type: '1--' });
input.blur();
expect(integerField.invalid).to.be.true;
});

it('should set an empty value when trying to commit a not valid number', async () => {
integerField.value = '1';
await sendKeys({ type: '1--' });
await sendKeys({ type: 'Enter' });
expect(integerField.value).to.equal('');
});
});
});
14 changes: 14 additions & 0 deletions packages/number-field/src/vaadin-number-field.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,20 @@ export class NumberField extends InputFieldMixin(SlotStylesMixin(ThemableMixin(E
this.addController(new LabelledInputController(this.inputElement, this._labelController));
}

/**
* Override a method from `InputConstraintsMixin`
* to additionally check for bad user input.
*
* @override
*/
checkValidity() {
if (this.inputElement && this.inputElement.validity.badInput) {
return false;
}

return super.checkValidity();
}

/** @private */
_decreaseButtonTouchend(e) {
// Cancel the following click and focus events
Expand Down
28 changes: 28 additions & 0 deletions packages/number-field/test/validation.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { expect } from '@esm-bundle/chai';
import { fixtureSync, nextRender } from '@vaadin/testing-helpers';
import { sendKeys } from '@web/test-runner-commands';
import sinon from 'sinon';
import '../src/vaadin-number-field.js';

Expand Down Expand Up @@ -137,6 +138,33 @@ describe('validation', () => {
});
});

describe('bad input', () => {
beforeEach(() => {
field = fixtureSync('<vaadin-number-field></vaadin-number-field>');
input = field.inputElement;
input.focus();
});

it('should be valid when committing a valid number', async () => {
await sendKeys({ type: '1' });
input.blur();
expect(field.invalid).to.be.false;
});

it('should be invalid when trying to commit a not valid number', async () => {
await sendKeys({ type: '1--' });
input.blur();
expect(field.invalid).to.be.true;
});

it('should set an empty value when trying to commit a not valid number', async () => {
field.value = '1';
await sendKeys({ type: '1--' });
await sendKeys({ type: 'Enter' });
expect(field.value).to.equal('');
});
});

describe('initial', () => {
let validateSpy;

Expand Down