Merge "Fix backspace doesn't work in Polymer 2"

This commit is contained in:
Ben Rohlfs
2019-09-25 07:30:14 +00:00
committed by Gerrit Code Review
2 changed files with 43 additions and 30 deletions

View File

@@ -253,8 +253,13 @@
console.warn('received remove event for missing account', toRemove);
},
_getNativeInput(paperInput) {
// In Polymer 2 inputElement isn't nativeInput anymore
return paperInput.$.nativeInput || paperInput.inputElement;
},
_handleInputKeydown(e) {
const input = e.detail.input.inputElement;
const input = this._getNativeInput(e.detail.input);
if (input.selectionStart !== input.selectionEnd ||
input.selectionStart !== 0) {
return;

View File

@@ -417,43 +417,51 @@ limitations under the License.
});
suite('keyboard interactions', () => {
test('backspace at text input start removes last account', () => {
test('backspace at text input start removes last account', done => {
const input = element.$.entry.$.input;
sandbox.stub(input, '_updateSuggestions');
sandbox.stub(element, '_computeRemovable').returns(true);
// Next line is a workaround for Firefix not moving cursor
// on input field update
assert.equal(input.$.input.inputElement.selectionStart, 0);
input.text = 'test';
MockInteractions.focus(input.$.input);
flushAsynchronousOperations();
assert.equal(element.accounts.length, 2);
MockInteractions.pressAndReleaseKeyOn(
input.$.input.inputElement, 8); // Backspace
assert.equal(element.accounts.length, 2);
input.text = '';
MockInteractions.pressAndReleaseKeyOn(
input.$.input.inputElement, 8); // Backspace
assert.equal(element.accounts.length, 1);
flush(() => {
// Next line is a workaround for Firefix not moving cursor
// on input field update
assert.equal(
element._getNativeInput(input.$.input).selectionStart, 0);
input.text = 'test';
MockInteractions.focus(input.$.input);
flushAsynchronousOperations();
assert.equal(element.accounts.length, 2);
MockInteractions.pressAndReleaseKeyOn(
element._getNativeInput(input.$.input), 8); // Backspace
assert.equal(element.accounts.length, 2);
input.text = '';
MockInteractions.pressAndReleaseKeyOn(
element._getNativeInput(input.$.input), 8); // Backspace
flushAsynchronousOperations();
assert.equal(element.accounts.length, 1);
done();
});
});
test('arrow key navigation', () => {
test('arrow key navigation', done => {
const input = element.$.entry.$.input;
input.text = '';
element.accounts = [makeAccount(), makeAccount()];
MockInteractions.focus(input.$.input);
flushAsynchronousOperations();
const chips = element.accountChips;
const chipsOneSpy = sandbox.spy(chips[1], 'focus');
MockInteractions.pressAndReleaseKeyOn(input.$.input, 37); // Left
assert.isTrue(chipsOneSpy.called);
const chipsZeroSpy = sandbox.spy(chips[0], 'focus');
MockInteractions.pressAndReleaseKeyOn(chips[1], 37); // Left
assert.isTrue(chipsZeroSpy.called);
MockInteractions.pressAndReleaseKeyOn(chips[0], 37); // Left
assert.isTrue(chipsZeroSpy.calledOnce);
MockInteractions.pressAndReleaseKeyOn(chips[0], 39); // Right
assert.isTrue(chipsOneSpy.calledTwice);
flush(() => {
MockInteractions.focus(input.$.input);
flushAsynchronousOperations();
const chips = element.accountChips;
const chipsOneSpy = sandbox.spy(chips[1], 'focus');
MockInteractions.pressAndReleaseKeyOn(input.$.input, 37); // Left
assert.isTrue(chipsOneSpy.called);
const chipsZeroSpy = sandbox.spy(chips[0], 'focus');
MockInteractions.pressAndReleaseKeyOn(chips[1], 37); // Left
assert.isTrue(chipsZeroSpy.called);
MockInteractions.pressAndReleaseKeyOn(chips[0], 37); // Left
assert.isTrue(chipsZeroSpy.calledOnce);
MockInteractions.pressAndReleaseKeyOn(chips[0], 39); // Right
assert.isTrue(chipsOneSpy.calledTwice);
done();
});
});
test('delete', done => {