Skip to content

Commit

Permalink
fix: validate on value change triggered by arrow key (#6476) (#6478)
Browse files Browse the repository at this point in the history
Co-authored-by: Sergey Vinogradov <[email protected]>
  • Loading branch information
vaadin-bot and vursen authored Sep 13, 2023
1 parent cff15c8 commit a6e60ac
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/number-field/src/vaadin-number-field-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ export const NumberFieldMixin = (superClass) =>
/** @private */
_setValue(value) {
this.value = this.inputElement.value = String(parseFloat(value));
this.validate();
this.dispatchEvent(new CustomEvent('change', { bubbles: true }));
}

Expand Down
25 changes: 23 additions & 2 deletions packages/number-field/test/validation.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@ describe('validation', () => {
let field, input;

describe('basic', () => {
let validateSpy, changeSpy;

beforeEach(async () => {
field = fixtureSync('<vaadin-number-field></vaadin-number-field>');
await nextRender();
input = field.inputElement;
validateSpy = sinon.spy(field, 'validate');
changeSpy = sinon.spy().named('changeSpy');
field.addEventListener('change', changeSpy);
});

it('should pass validation by default', () => {
Expand All @@ -36,6 +41,24 @@ describe('validation', () => {
expect(field.invalid).to.be.false;
});

it('should validate before change event on ArrowDown', async () => {
field.focus();
await sendKeys({ press: 'ArrowDown' });
await nextFrame();
expect(validateSpy.calledOnce).to.be.true;
expect(changeSpy.calledOnce).to.be.true;
expect(changeSpy.calledAfter(validateSpy)).to.be.true;
});

it('should validate before change event on ArrowUp', async () => {
field.focus();
await sendKeys({ press: 'ArrowUp' });
await nextFrame();
expect(validateSpy.calledOnce).to.be.true;
expect(changeSpy.calledOnce).to.be.true;
expect(changeSpy.calledAfter(validateSpy)).to.be.true;
});

it('should be valid with numeric values', async () => {
field.value = '1';
await nextFrame();
Expand Down Expand Up @@ -112,8 +135,6 @@ describe('validation', () => {
});

it('should dispatch change event after validation', async () => {
const validateSpy = sinon.spy(field, 'validate');
const changeSpy = sinon.spy();
field.required = true;
field.addEventListener('change', changeSpy);
input.value = '123';
Expand Down

0 comments on commit a6e60ac

Please sign in to comment.