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: reset _hasInputValue on programmatic value clear (CP: 14) #622

Merged
merged 3 commits into from
Apr 27, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/vaadin-text-field-mixin.html
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,11 @@
},

/**
* When true, the input element has a non-empty value entered by the user.
* Whether the input element has user input.
*
* Note, the property indicates true only if the input has been entered by the user.
* In the case of programmatic changes, the property must be reset to false.
*
* @protected
*/
vursen marked this conversation as resolved.
Show resolved Hide resolved
_hasInputValue: {
Expand Down Expand Up @@ -824,6 +828,7 @@
}

clear() {
this._hasInputValue = false;
this.value = '';
}

Expand Down
71 changes: 47 additions & 24 deletions test/email-field-events.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<script src="../../webcomponentsjs/webcomponents-lite.js"></script>
<link rel="import" href="../vaadin-email-field.html">
<link rel="import" href="../../test-fixture/test-fixture.html">
<link rel="import" href="helpers.html">
</head>

<body>
Expand All @@ -18,37 +19,59 @@
</test-fixture>
<script>
describe('events', () => {
let emailField, input;
let emailField, input, hasInputValueChangedSpy, valueChangedSpy;

describe('has-input-value-changed event', () => {
beforeEach(async() => {
emailField = fixture('email-field');
input = emailField.inputElement;
});

it('should fire the event on input value presence change', async() => {
const hasInputValueChangedSpy = sinon.spy();
hasInputValueChangedSpy = sinon.spy();
valueChangedSpy = sinon.spy();
emailField.addEventListener('has-input-value-changed', hasInputValueChangedSpy);
input.value = 'foo';
input.dispatchEvent(new CustomEvent('input'));
expect(hasInputValueChangedSpy.calledOnce).to.be.true;

hasInputValueChangedSpy.reset();
input.value = '';
input.dispatchEvent(new CustomEvent('input'));
expect(hasInputValueChangedSpy.calledOnce).to.be.true;
emailField.addEventListener('value-changed', valueChangedSpy);
await nextRender();
vursen marked this conversation as resolved.
Show resolved Hide resolved
});

it('should not fire the event on input if input value presence has not changed', async() => {
const hasInputValueChangedSpy = sinon.spy();
emailField.addEventListener('has-input-value-changed', hasInputValueChangedSpy);
input.value = 'foo';
input.dispatchEvent(new CustomEvent('input'));
hasInputValueChangedSpy.reset();

input.value = 'foobar';
input.dispatchEvent(new CustomEvent('input'));
expect(hasInputValueChangedSpy.called).to.be.false;

describe('without user input', () => {
it('should fire the event once when entering input', async() => {
input.value = 'foo';
input.dispatchEvent(new CustomEvent('input'));
expect(hasInputValueChangedSpy.calledOnce).to.be.true;
expect(hasInputValueChangedSpy.calledBefore(valueChangedSpy)).to.be.true;
});

it('should not fire the event on programmatic clear', async() => {
emailField.clear();
expect(hasInputValueChangedSpy.called).to.be.false;
});
});

describe('with user input', () => {
beforeEach(async() => {
input.value = 'foo';
input.dispatchEvent(new CustomEvent('input'));
hasInputValueChangedSpy.reset();
valueChangedSpy.reset();
});

it('should not fire the event when modifying input', async() => {
input.value = 'foobar';
input.dispatchEvent(new CustomEvent('input'));
expect(hasInputValueChangedSpy.called).to.be.false;
});

it('should fire the event once when removing input', async() => {
input.value = '';
input.dispatchEvent(new CustomEvent('input'));
expect(hasInputValueChangedSpy.calledOnce).to.be.true;
expect(hasInputValueChangedSpy.calledBefore(valueChangedSpy)).to.be.true;
});

it('should fire the event once on programmatic clear', async() => {
emailField.clear();
expect(hasInputValueChangedSpy.calledOnce).to.be.true;
expect(hasInputValueChangedSpy.calledBefore(valueChangedSpy)).to.be.true;
});
});
});
});
Expand Down
63 changes: 43 additions & 20 deletions test/integer-field-events.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<script src="../../webcomponentsjs/webcomponents-lite.js"></script>
<link rel="import" href="../vaadin-integer-field.html">
<link rel="import" href="../../test-fixture/test-fixture.html">
<link rel="import" href="helpers.html">
</head>

<body>
Expand All @@ -18,37 +19,59 @@
</test-fixture>
<script>
describe('events', () => {
let integerField, input;
let integerField, input, hasInputValueChangedSpy, valueChangedSpy;

describe('has-input-value-changed event', () => {
beforeEach(async() => {
integerField = fixture('integer-field');
input = integerField.inputElement;
hasInputValueChangedSpy = sinon.spy();
valueChangedSpy = sinon.spy();
integerField.addEventListener('has-input-value-changed', hasInputValueChangedSpy);
integerField.addEventListener('value-changed', valueChangedSpy);
await nextRender();
});

it('should fire the event on input value presence change', async() => {
const hasInputValueChangedSpy = sinon.spy();
integerField.addEventListener('has-input-value-changed', hasInputValueChangedSpy);
input.value = '5';
input.dispatchEvent(new CustomEvent('input'));
expect(hasInputValueChangedSpy.calledOnce).to.be.true;
describe('without user input', () => {
it('should fire the event once when entering input', async() => {
input.value = '5';
input.dispatchEvent(new CustomEvent('input'));
expect(hasInputValueChangedSpy.calledOnce).to.be.true;
expect(hasInputValueChangedSpy.calledBefore(valueChangedSpy)).to.be.true;
});

hasInputValueChangedSpy.reset();
input.value = '';
input.dispatchEvent(new CustomEvent('input'));
expect(hasInputValueChangedSpy.calledOnce).to.be.true;
it('should not fire the event on programmatic clear', async() => {
integerField.clear();
expect(hasInputValueChangedSpy.called).to.be.false;
});
});

it('should not fire the event on input if input value presence has not changed', async() => {
const hasInputValueChangedSpy = sinon.spy();
integerField.addEventListener('has-input-value-changed', hasInputValueChangedSpy);
input.value = '5';
input.dispatchEvent(new CustomEvent('input'));
hasInputValueChangedSpy.reset();
describe('with user input', () => {
beforeEach(async() => {
input.value = '5';
input.dispatchEvent(new CustomEvent('input'));
hasInputValueChangedSpy.reset();
valueChangedSpy.reset();
});

it('should not fire the event when modifying input', async() => {
input.value = '56';
input.dispatchEvent(new CustomEvent('input'));
expect(hasInputValueChangedSpy.called).to.be.false;
});

it('should fire the event once when removing input', async() => {
input.value = '';
input.dispatchEvent(new CustomEvent('input'));
expect(hasInputValueChangedSpy.calledOnce).to.be.true;
expect(hasInputValueChangedSpy.calledBefore(valueChangedSpy)).to.be.true;
});

input.value = '56';
input.dispatchEvent(new CustomEvent('input'));
expect(hasInputValueChangedSpy.called).to.be.false;
it('should fire the event once on programmatic clear', async() => {
integerField.clear();
expect(hasInputValueChangedSpy.calledOnce).to.be.true;
expect(hasInputValueChangedSpy.calledBefore(valueChangedSpy)).to.be.true;
});
});
});
});
Expand Down
63 changes: 43 additions & 20 deletions test/number-field-events.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<script src="../../webcomponentsjs/webcomponents-lite.js"></script>
<link rel="import" href="../vaadin-number-field.html">
<link rel="import" href="../../test-fixture/test-fixture.html">
<link rel="import" href="helpers.html">
</head>

<body>
Expand All @@ -18,37 +19,59 @@
</test-fixture>
<script>
describe('events', () => {
let numberField, input;
let numberField, input, hasInputValueChangedSpy, valueChangedSpy;

describe('has-input-value-changed event', () => {
beforeEach(async() => {
numberField = fixture('number-field');
input = numberField.inputElement;
hasInputValueChangedSpy = sinon.spy();
valueChangedSpy = sinon.spy();
numberField.addEventListener('has-input-value-changed', hasInputValueChangedSpy);
numberField.addEventListener('value-changed', valueChangedSpy);
await nextRender();
});

it('should fire the event on input value presence change', async() => {
const hasInputValueChangedSpy = sinon.spy();
numberField.addEventListener('has-input-value-changed', hasInputValueChangedSpy);
input.value = '5';
input.dispatchEvent(new CustomEvent('input'));
expect(hasInputValueChangedSpy.calledOnce).to.be.true;
describe('without user input', () => {
it('should fire the event once when entering input', async() => {
input.value = '5';
input.dispatchEvent(new CustomEvent('input'));
expect(hasInputValueChangedSpy.calledOnce).to.be.true;
expect(hasInputValueChangedSpy.calledBefore(valueChangedSpy)).to.be.true;
});

hasInputValueChangedSpy.reset();
input.value = '';
input.dispatchEvent(new CustomEvent('input'));
expect(hasInputValueChangedSpy.calledOnce).to.be.true;
it('should not fire the event on programmatic clear', async() => {
numberField.clear();
expect(hasInputValueChangedSpy.called).to.be.false;
});
});

it('should not fire the event on input if input value presence has not changed', async() => {
const hasInputValueChangedSpy = sinon.spy();
numberField.addEventListener('has-input-value-changed', hasInputValueChangedSpy);
input.value = '5';
input.dispatchEvent(new CustomEvent('input'));
hasInputValueChangedSpy.reset();
describe('with user input', () => {
beforeEach(async() => {
input.value = '5';
input.dispatchEvent(new CustomEvent('input'));
hasInputValueChangedSpy.reset();
valueChangedSpy.reset();
});

it('should not fire the event when modifying input', async() => {
input.value = '56';
input.dispatchEvent(new CustomEvent('input'));
expect(hasInputValueChangedSpy.called).to.be.false;
});

it('should fire the event once when removing input', async() => {
input.value = '';
input.dispatchEvent(new CustomEvent('input'));
expect(hasInputValueChangedSpy.calledOnce).to.be.true;
expect(hasInputValueChangedSpy.calledBefore(valueChangedSpy)).to.be.true;
});

input.value = '56';
input.dispatchEvent(new CustomEvent('input'));
expect(hasInputValueChangedSpy.called).to.be.false;
it('should fire the event once on programmatic clear', async() => {
numberField.clear();
expect(hasInputValueChangedSpy.calledOnce).to.be.true;
expect(hasInputValueChangedSpy.calledBefore(valueChangedSpy)).to.be.true;
});
});
});
});
Expand Down
63 changes: 43 additions & 20 deletions test/password-field-events.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<script src="../../webcomponentsjs/webcomponents-lite.js"></script>
<link rel="import" href="../vaadin-password-field.html">
<link rel="import" href="../../test-fixture/test-fixture.html">
<link rel="import" href="helpers.html">
</head>

<body>
Expand All @@ -18,37 +19,59 @@
</test-fixture>
<script>
describe('events', () => {
let passwordField, input;
let passwordField, input, hasInputValueChangedSpy, valueChangedSpy;

describe('has-input-value-changed event', () => {
beforeEach(async() => {
passwordField = fixture('password-field');
input = passwordField.inputElement;
hasInputValueChangedSpy = sinon.spy();
valueChangedSpy = sinon.spy();
passwordField.addEventListener('has-input-value-changed', hasInputValueChangedSpy);
passwordField.addEventListener('value-changed', valueChangedSpy);
await nextRender();
});

it('should fire the event on input value presence change', async() => {
const hasInputValueChangedSpy = sinon.spy();
passwordField.addEventListener('has-input-value-changed', hasInputValueChangedSpy);
input.value = 'foo';
input.dispatchEvent(new CustomEvent('input'));
expect(hasInputValueChangedSpy.calledOnce).to.be.true;
describe('without user input', () => {
it('should fire the event once when entering input', async() => {
input.value = 'foo';
input.dispatchEvent(new CustomEvent('input'));
expect(hasInputValueChangedSpy.calledOnce).to.be.true;
expect(hasInputValueChangedSpy.calledBefore(valueChangedSpy)).to.be.true;
});

hasInputValueChangedSpy.reset();
input.value = '';
input.dispatchEvent(new CustomEvent('input'));
expect(hasInputValueChangedSpy.calledOnce).to.be.true;
it('should not fire the event on programmatic clear', async() => {
passwordField.clear();
expect(hasInputValueChangedSpy.called).to.be.false;
});
});

it('should not fire the event on input if input value presence has not changed', async() => {
const hasInputValueChangedSpy = sinon.spy();
passwordField.addEventListener('has-input-value-changed', hasInputValueChangedSpy);
input.value = 'foo';
input.dispatchEvent(new CustomEvent('input'));
hasInputValueChangedSpy.reset();
describe('with user input', () => {
beforeEach(async() => {
input.value = 'foo';
input.dispatchEvent(new CustomEvent('input'));
hasInputValueChangedSpy.reset();
valueChangedSpy.reset();
});

it('should not fire the event when modifying input', async() => {
input.value = 'foobar';
input.dispatchEvent(new CustomEvent('input'));
expect(hasInputValueChangedSpy.called).to.be.false;
});

it('should fire the event once when removing input', async() => {
input.value = '';
input.dispatchEvent(new CustomEvent('input'));
expect(hasInputValueChangedSpy.calledOnce).to.be.true;
expect(hasInputValueChangedSpy.calledBefore(valueChangedSpy)).to.be.true;
});

input.value = 'foobar';
input.dispatchEvent(new CustomEvent('input'));
expect(hasInputValueChangedSpy.called).to.be.false;
it('should fire the event once on programmatic clear', async() => {
passwordField.clear();
expect(hasInputValueChangedSpy.calledOnce).to.be.true;
expect(hasInputValueChangedSpy.calledBefore(valueChangedSpy)).to.be.true;
});
});
});
});
Expand Down