blob: 423438819332c88431eb146849c06f564fae70ce [file] [log] [blame]
<!DOCTYPE html>
<!--
Copyright (C) 2016 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<meta name="viewport" content="width=device-width, minimum-scale=1.0, initial-scale=1.0, user-scalable=yes">
<title>gr-reviewer-list</title>
<script src="../../../bower_components/webcomponentsjs/webcomponents-lite.min.js"></script>
<script src="../../../bower_components/web-component-tester/browser.js"></script>
<link rel="import" href="../../../bower_components/iron-test-helpers/iron-test-helpers.html">
<link rel="import" href="gr-autocomplete.html">
<script>void(0);</script>
<test-fixture id="basic">
<template>
<gr-autocomplete></gr-autocomplete>
</template>
</test-fixture>
<script>
suite('gr-autocomplete tests', function() {
var element;
setup(function() {
element = fixture('basic');
});
test('renders', function(done) {
var promise;
var queryStub = sinon.spy(function(input) {
return promise = Promise.resolve([
{name: input + ' 0', value: 0},
{name: input + ' 1', value: 1},
{name: input + ' 2', value: 2},
{name: input + ' 3', value: 3},
{name: input + ' 4', value: 4},
]);
});
element.query = queryStub;
assert.isTrue(element.$.suggestions.hasAttribute('hidden'));
assert.equal(element.$.cursor.index, -1);
element.text = 'blah';
assert.isTrue(queryStub.called);
element._focused = true;
promise.then(function() {
assert.isFalse(element.$.suggestions.hasAttribute('hidden'));
var suggestions = element.$.suggestions.querySelectorAll('li');
assert.equal(suggestions.length, 5);
for (var i = 0; i < 5; i++) {
assert.equal(suggestions[i].textContent, 'blah ' + i);
}
assert.notEqual(element.$.cursor.index, -1);
done();
});
});
test('emits cancel', function(done) {
var promise;
var queryStub = sinon.spy(function() {
return promise = Promise.resolve([
{name: 'blah', value: 123},
]);
});
element.query = queryStub;
assert.isTrue(element.$.suggestions.hasAttribute('hidden'));
element._focused = true;
element.text = 'blah';
promise.then(function() {
assert.isFalse(element.$.suggestions.hasAttribute('hidden'));
var cancelHandler = sinon.spy();
element.addEventListener('cancel', cancelHandler);
MockInteractions.pressAndReleaseKeyOn(element.$.input, 27, null, 'esc');
assert.isTrue(cancelHandler.called);
assert.isTrue(element.$.suggestions.hasAttribute('hidden'));
done();
});
});
test('emits commit and handles cursor movement', function(done) {
var promise;
var queryStub = sinon.spy(function(input) {
return promise = Promise.resolve([
{name: input + ' 0', value: 0},
{name: input + ' 1', value: 1},
{name: input + ' 2', value: 2},
{name: input + ' 3', value: 3},
{name: input + ' 4', value: 4},
]);
});
element.query = queryStub;
assert.isTrue(element.$.suggestions.hasAttribute('hidden'));
assert.equal(element.$.cursor.index, -1);
element._focused = true;
element.text = 'blah';
promise.then(function() {
assert.isFalse(element.$.suggestions.hasAttribute('hidden'));
var commitHandler = sinon.spy();
element.addEventListener('commit', commitHandler);
assert.equal(element.$.cursor.index, 0);
MockInteractions.pressAndReleaseKeyOn(element.$.input, 40, null,
'down');
assert.equal(element.$.cursor.index, 1);
MockInteractions.pressAndReleaseKeyOn(element.$.input, 40, null,
'down');
assert.equal(element.$.cursor.index, 2);
MockInteractions.pressAndReleaseKeyOn(element.$.input, 38, null, 'up');
assert.equal(element.$.cursor.index, 1);
MockInteractions.pressAndReleaseKeyOn(element.$.input, 13, null,
'enter');
assert.equal(element.value, 1);
assert.isTrue(commitHandler.called);
assert.equal(commitHandler.getCall(0).args[0].detail.value, 1);
assert.isTrue(element.$.suggestions.hasAttribute('hidden'));
done();
});
});
test('clear-on-commit behavior (off)', function(done) {
var promise;
var queryStub = sinon.spy(function() {
return promise = Promise.resolve([{name: 'suggestion', value: 0}]);
});
element.query = queryStub;
element.text = 'blah';
promise.then(function() {
var commitHandler = sinon.spy();
element.addEventListener('commit', commitHandler);
MockInteractions.pressAndReleaseKeyOn(element.$.input, 13, null,
'enter');
assert.isTrue(commitHandler.called);
assert.equal(element.text, 'suggestion');
done();
});
});
test('clear-on-commit behavior (on)', function(done) {
var promise;
var queryStub = sinon.spy(function() {
return promise = Promise.resolve([{name: 'suggestion', value: 0}]);
});
element.query = queryStub;
element.text = 'blah';
element.clearOnCommit = true;
promise.then(function() {
var commitHandler = sinon.spy();
element.addEventListener('commit', commitHandler);
MockInteractions.pressAndReleaseKeyOn(element.$.input, 13, null,
'enter');
assert.isTrue(commitHandler.called);
assert.equal(element.text, '');
done();
});
});
test('threshold guards the query', function() {
var queryStub = sinon.spy(function() {
return Promise.resolve([]);
});
element.query = queryStub;
element.threshold = 2;
element.text = 'a';
assert.isFalse(queryStub.called);
element.text = 'ab';
assert.isTrue(queryStub.called);
});
test('_computeClass respects border property', function() {
assert.equal(element._computeClass(), '');
assert.equal(element._computeClass(false), '');
assert.equal(element._computeClass(true), 'borderless');
});
test('undefined or empty text results in no suggestions', function() {
sinon.spy(element, '_updateSuggestions');
element.text = undefined;
assert(element._updateSuggestions.calledOnce);
assert.equal(element._suggestions.length, 0);
});
test('multi completes only the last part of the query', function(done) {
var promise;
var queryStub = sinon.stub()
.returns(promise = Promise.resolve([{name: 'suggestion', value: 0}]));
element.query = queryStub;
element.text = 'blah blah';
element.multi = true;
promise.then(function() {
var commitHandler = sinon.spy();
element.addEventListener('commit', commitHandler);
MockInteractions.pressAndReleaseKeyOn(element.$.input, 13, null,
'enter');
assert.isTrue(commitHandler.called);
assert.equal(element.text, 'blah 0');
done();
});
});
test('tab key completes only when suggestions exist', function() {
var commitStub = sinon.stub(element, '_commit');
element._suggestions = [];
MockInteractions.pressAndReleaseKeyOn(element.$.input, 9, null, 'tab');
assert.isFalse(commitStub.called);
element._suggestions = ['tunnel snakes rule!'];
MockInteractions.pressAndReleaseKeyOn(element.$.input, 9, null, 'tab');
assert.isTrue(commitStub.called);
commitStub.restore();
});
test('tabCompleteWithoutCommit flag functions', function() {
var commitHandler = sinon.spy();
element.addEventListener('commit', commitHandler);
element._suggestions = ['tunnel snakes rule!'];
element.tabCompleteWithoutCommit = true;
MockInteractions.pressAndReleaseKeyOn(element.$.input, 9, null, 'tab');
assert.isFalse(commitHandler.called);
element.tabCompleteWithoutCommit = false;
element._suggestions = ['tunnel snakes rule!'];
MockInteractions.pressAndReleaseKeyOn(element.$.input, 9, null, 'tab');
assert.isTrue(commitHandler.called);
});
test('_focused flag properly triggered', function(done) {
flush(function() {
assert.isFalse(element._focused);
var input = element.$$('input');
MockInteractions.focus(input);
assert.isTrue(element._focused);
done();
});
});
test('_focused flag shows/hides the suggestions', function() {
var suggestions = ['hello', 'its me'];
assert.isTrue(element._computeSuggestionsHidden(suggestions, false));
assert.isFalse(element._computeSuggestionsHidden(suggestions, true));
});
test('tap on suggestion commits and refocuses on input', function() {
var focusSpy = sinon.spy(element, 'focus');
var commitSpy = sinon.spy(element, '_commit');
element._focused = true;
element._suggestions = [{name: 'first suggestion'}];
assert.isFalse(element.$.suggestions.hasAttribute('hidden'));
MockInteractions.tap(element.$$('#suggestions li:first-child'));
flushAsynchronousOperations();
assert.isTrue(focusSpy.called);
assert.isTrue(commitSpy.called);
assert.isTrue(element.$.suggestions.hasAttribute('hidden'));
assert.isTrue(element._focused);
focusSpy.restore();
commitSpy.restore();
});
test('input-keydown event fired', function() {
var listener = sinon.spy();
element.addEventListener('input-keydown', listener);
MockInteractions.pressAndReleaseKeyOn(element.$.input, 9, null, 'tab');
flushAsynchronousOperations();
assert.isTrue(listener.called);
});
});
</script>