Skip to content

Commit

Permalink
refactor and add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vursen committed Oct 11, 2023
1 parent 0759495 commit 18a3b09
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 27 deletions.
2 changes: 1 addition & 1 deletion packages/field-base/src/clear-button-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export const ClearButtonMixin = (superclass) =>
* @protected
*/
_onClearAction() {
this.clear();
this._inputElementValue = '';
// Note, according to the HTML spec, the native change event isn't composed
// while the input event is composed.
this.inputElement.dispatchEvent(new Event('input', { bubbles: true, composed: true }));
Expand Down
2 changes: 0 additions & 2 deletions packages/number-field/src/vaadin-number-field-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -436,9 +436,7 @@ export const NumberFieldMixin = (superClass) =>
* @protected
*/
_onClearAction(event) {
this.__keepCommittedValue = true;
super._onClearAction(event);
this.__keepCommittedValue = false;
this.__commitValueChange();
}

Expand Down
86 changes: 62 additions & 24 deletions packages/number-field/test/value-commit.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,25 @@ describe('value commit', () => {
expectValueCommit('');
});
});

describe('value set programmatically', () => {
beforeEach(async () => {
numberField.value = '1234';
await nextUpdate(numberField);
valueChangedSpy.resetHistory();
validateSpy.resetHistory();
});

it('should not commit but validate on blur', () => {
numberField.blur();
expectValidationOnly();
});

it('should not commit on Enter', async () => {
await sendKeys({ press: 'Enter' });
expectNoValueCommit();
});
});
});

describe('unparsable input entered', () => {
Expand Down Expand Up @@ -284,27 +303,27 @@ describe('value commit', () => {
await nextUpdate(numberField);
expectValueCommit('-1');
});
});

describe('arrow key committed', () => {
beforeEach(async () => {
await sendKeys({ press: 'ArrowUp' });
await nextUpdate(numberField);
valueChangedSpy.resetHistory();
validateSpy.resetHistory();
changeSpy.resetHistory();
});
describe('keyboard stepping committed', () => {
beforeEach(async () => {
await sendKeys({ press: 'ArrowUp' });
await nextUpdate(numberField);
valueChangedSpy.resetHistory();
validateSpy.resetHistory();
changeSpy.resetHistory();
});

it('should not commit but validate on blur', async () => {
numberField.blur();
await nextUpdate(numberField);
expectValidationOnly();
});
it('should not commit but validate on blur', async () => {
numberField.blur();
await nextUpdate(numberField);
expectValidationOnly();
});

it('should not commit on Enter', async () => {
await sendKeys({ press: 'Enter' });
await nextUpdate(numberField);
expectNoValueCommit();
});
it('should not commit on Enter', async () => {
await sendKeys({ press: 'Enter' });
await nextUpdate(numberField);
expectNoValueCommit();
});
});

Expand All @@ -327,25 +346,44 @@ describe('value commit', () => {
await nextUpdate(numberField);
expectValueCommit('-1');
});
});

describe('click committed', () => {
describe('value control button click committed', () => {
beforeEach(async () => {
numberField.shadowRoot.querySelector('[part=increase-button]').click();
await nextUpdate(numberField);
valueChangedSpy.resetHistory();
validateSpy.resetHistory();
changeSpy.resetHistory();
});

it('should not commit but validate on blur', async () => {
numberField.blur();
await nextUpdate(numberField);
expectValidationOnly();
});

it('should not commit on Enter', async () => {
await sendKeys({ press: 'Enter' });
await nextUpdate(numberField);
expectNoValueCommit();
});

describe('value set programmatically', () => {
beforeEach(async () => {
increaseButton.click();
numberField.value = '1234';
await nextUpdate(numberField);
valueChangedSpy.resetHistory();
validateSpy.resetHistory();
changeSpy.resetHistory();
});

it('should not commit but validate on blur', async () => {
it('should not commit but validate on blur', () => {
numberField.blur();
await nextUpdate(numberField);
expectValidationOnly();
});

it('should not commit on Enter', async () => {
await sendKeys({ press: 'Enter' });
await nextUpdate(numberField);
expectNoValueCommit();
});
});
Expand Down

0 comments on commit 18a3b09

Please sign in to comment.