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: ensure hasInputValue is updated for combo-box-light #1045

Merged
merged 2 commits into from
May 15, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions src/vaadin-combo-box-light.html
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@
set _inputElementValue(value) {
if (this.inputElement) {
this.inputElement[this._propertyForValue] = value;
this._inputElementValueChanged(value);
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion test/test-suites.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ window.VaadinComboBoxSuites = [
'vaadin-combo-box-dropdown.html',
'lazy-loading.html',
'validation.html',
'events.html'
'events.html',
'vaadin-combo-box-light-events.html'
];

if (isPolymer2) {
Expand Down
152 changes: 152 additions & 0 deletions test/vaadin-combo-box-light-events.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
<!doctype html>
<html>

<head>
<meta charset="UTF-8">
<title>vaadin-combo-box-light events tests</title>

<script src="../../web-component-tester/browser.js"></script>
<script src="../../webcomponentsjs/webcomponents-lite.js"></script>
<link rel="import" href="../../test-fixture/test-fixture.html">
<script src="../../iron-test-helpers/mock-interactions.js"></script>
<link rel="import" href="common-imports.html">
<script src="common.js"></script>
</head>

<body>
<test-fixture id="combo-box-light">
<template>
<vaadin-combo-box-light>
<vaadin-text-field></vaadin-text-field>
</vaadin-combo-box-light>
</template>
</test-fixture>
<script>
(ios ? describe.skip : describe)('vaadin-combo-box-light events', () => {
function inputChar(target, char) {
target.value += char;
MockInteractions.keyDownOn(target, char.charCodeAt(0));
target.dispatchEvent(new CustomEvent('input', {bubbles: true, composed: true}));
}

function inputText(target, text) {
for (var i = 0; i < text.length; i++) {
inputChar(target, text[i]);
}
}

function arrowDown(target) {
MockInteractions.keyDownOn(target, 40);
}

function enter(target) {
MockInteractions.pressEnter(target);
}

function space(target) {
MockInteractions.pressSpace(target);
}

function esc(target) {
MockInteractions.keyDownOn(target, 27, null, 'Escape');
}

describe('has-input-value-changed event', () => {
let comboBox, input, clearButton, hasInputValueChangedSpy, valueChangedSpy;

beforeEach(async() => {
hasInputValueChangedSpy = sinon.spy();
valueChangedSpy = sinon.spy();
comboBox = fixture('combo-box-light');
comboBox.allowCustomValue = true;
input = comboBox.inputElement.inputElement;
comboBox.addEventListener('has-input-value-changed', hasInputValueChangedSpy);
comboBox.addEventListener('value-changed', valueChangedSpy);
input.focus();
});

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

it('should be fired when clearing the user input with Esc', async() => {
expect(input.value).to.equal('foo');
// Clear selection in the dropdown.
esc(input);
// Clear the user input.
esc(input);
expect(input.value).to.be.empty;
expect(hasInputValueChangedSpy.calledOnce).to.be.true;
});

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

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

it('should fire the event once on programmatic clear', async() => {
comboBox._clear();
expect(hasInputValueChangedSpy.calledOnce).to.be.true;
expect(hasInputValueChangedSpy.calledBefore(valueChangedSpy)).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', {bubbles: true, composed: true}));
expect(hasInputValueChangedSpy.calledOnce).to.be.true;
expect(hasInputValueChangedSpy.calledBefore(valueChangedSpy)).to.be.true;
});

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

describe('with value', () => {
beforeEach(async() => {
comboBox.clearButtonVisible = true;
clearButton = comboBox.inputElement.$.clearButton;
inputText(input, 'foo');
enter(input);
valueChangedSpy.reset();
hasInputValueChangedSpy.reset();
});

it('should be fired on clear button click', () => {
expect(input.value).to.equal('foo');
clearButton.click();
expect(input.value).to.be.empty;
expect(valueChangedSpy.calledOnce).to.be.true;
expect(hasInputValueChangedSpy.calledOnce).to.be.true;
expect(hasInputValueChangedSpy.calledBefore(valueChangedSpy)).to.be.true;
});

it('should be fired when clearing the value with Esc', async() => {
esc(input);
expect(input.value).to.be.empty;
expect(valueChangedSpy.calledOnce).to.be.true;
expect(hasInputValueChangedSpy.calledOnce).to.be.true;
expect(hasInputValueChangedSpy.calledBefore(valueChangedSpy)).to.be.true;
});
});
});
});
</script>
</body>
</html>