blob: f8b16b7b41183a6aac3ff5c991c8fcddc210a6c8 [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.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">
<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);
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.text = 'blah';
promise.then(function() {
assert.isFalse(element.$.suggestions.hasAttribute('hidden'));
var cancelHandler = sinon.spy();
element.addEventListener('cancel', cancelHandler);
MockInteractions.pressAndReleaseKeyOn(element.$.input, 27); // 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.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); // Down
assert.equal(element.$.cursor.index, 1);
MockInteractions.pressAndReleaseKeyOn(element.$.input, 40); // Down
assert.equal(element.$.cursor.index, 2);
MockInteractions.pressAndReleaseKeyOn(element.$.input, 38); // Up
assert.equal(element.$.cursor.index, 1);
MockInteractions.pressAndReleaseKeyOn(element.$.input, 13); // 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); // 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); // 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); // Enter
assert.isTrue(commitHandler.called);
assert.equal(element.text, 'blah 0');
done();
});
});
});
</script>