blob: f010fb32343e316d5fbdd26f74b7c828cb67ecb7 [file] [log] [blame]
<!DOCTYPE html>
<!--
@license
Copyright (C) 2017 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-textarea</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="../../../test/common-test-setup.html"/>
<link rel="import" href="gr-textarea.html">
<script>void(0);</script>
<test-fixture id="basic">
<template>
<gr-textarea></gr-textarea>
</template>
</test-fixture>
<script>
suite('gr-textarea tests', () => {
let element;
let sandbox;
setup(() => {
sandbox = sinon.sandbox.create();
element = fixture('basic');
});
teardown(() => {
sandbox.restore();
});
test('monospace is set properly', () => {
assert.isFalse(element.classList.contains('monospace'));
element.monospace = true;
element.ready();
assert.isTrue(element.classList.contains('monospace'));
});
test('hideBorder is set properly', () => {
assert.isFalse(element.$.textarea.classList.contains('noBorder'));
element.hideBorder = true;
element.ready();
assert.isTrue(element.$.textarea.classList.contains('noBorder'));
});
test('emoji selector is not open with the textarea lacks focus', () => {
element.$.textarea.selectionStart = 1;
element.$.textarea.selectionEnd = 1;
element.text = ':';
assert.isFalse(!element.$.emojiSuggestions.isHidden);
});
test('emoji selector is not open when a general text is entered', () => {
MockInteractions.focus(element.$.textarea);
element.$.textarea.selectionStart = 9;
element.$.textarea.selectionEnd = 9;
element.text = 'some text';
assert.isFalse(!element.$.emojiSuggestions.isHidden);
});
test('emoji selector opens when a colon is typed & the textarea has focus',
() => {
MockInteractions.focus(element.$.textarea);
// Needed for Safari tests. selectionStart is not updated when text is
// updated.
element.$.textarea.selectionStart = 1;
element.$.textarea.selectionEnd = 1;
element.text = ':';
element.$.textarea.selectionStart = 2;
element.$.textarea.selectionEnd = 2;
element.text = ':t';
flushAsynchronousOperations();
assert.isFalse(element.$.emojiSuggestions.isHidden);
assert.equal(element._colonIndex, 0);
assert.isFalse(element._hideAutocomplete);
assert.equal(element._currentSearchString, 't');
});
test('emoji selector closes when text changes before the colon', () => {
const resetStub = sandbox.stub(element, '_resetEmojiDropdown');
MockInteractions.focus(element.$.textarea);
flushAsynchronousOperations();
element.$.textarea.selectionStart = 10;
element.$.textarea.selectionEnd = 10;
element.text = 'test test ';
element.$.textarea.selectionStart = 12;
element.$.textarea.selectionEnd = 12;
element.text = 'test test :';
element.$.textarea.selectionStart = 15;
element.$.textarea.selectionEnd = 15;
element.text = 'test test :smi';
assert.equal(element._currentSearchString, 'smi');
assert.isFalse(resetStub.called);
element.text = 'test test test :smi';
assert.isTrue(resetStub.called);
});
test('_resetEmojiDropdown', () => {
const closeSpy = sandbox.spy(element, 'closeDropdown');
element._resetEmojiDropdown();
assert.equal(element._currentSearchString, '');
assert.isTrue(element._hideAutocomplete);
assert.equal(element._colonIndex, null);
element.$.emojiSuggestions.open();
flushAsynchronousOperations();
element._resetEmojiDropdown();
assert.isTrue(closeSpy.called);
});
test('_determineSuggestions', () => {
const emojiText = 'tear';
const formatSpy = sandbox.spy(element, '_formatSuggestions');
element._determineSuggestions(emojiText);
assert.isTrue(formatSpy.called);
assert.isTrue(formatSpy.lastCall.calledWithExactly(
[{dataValue: '😢', value: '😢', match: 'tear', text: '😢 tear'},
{dataValue: '😂', value: '😂', match: 'tears', text: '😂 tears'}]));
});
test('_formatSuggestions', () => {
const matchedSuggestions = [{value: '😢', match: 'tear'},
{value: '😂', match: 'tears'}];
element._formatSuggestions(matchedSuggestions);
assert.deepEqual(
[{value: '😢', dataValue: '😢', match: 'tear', text: '😢 tear'},
{value: '😂', dataValue: '😂', match: 'tears', text: '😂 tears'}],
element._suggestions);
});
test('_handleEmojiSelect', () => {
element.$.textarea.selectionStart = 16;
element.$.textarea.selectionEnd = 16;
element.text = 'test test :tears';
element._colonIndex = 10;
const selectedItem = {dataset: {value: '😂'}};
const event = {detail: {selected: selectedItem}};
element._handleEmojiSelect(event);
assert.equal(element.text, 'test test 😂 ');
});
test('_updateCaratPosition', () => {
element.$.textarea.selectionStart = 4;
element.$.textarea.selectionEnd = 4;
element.text = 'test';
element._updateCaratPosition();
assert.deepEqual(element.$.hiddenText.innerHTML, element.text +
element.$.caratSpan.outerHTML);
});
test('emoji dropdown is closed when iron-overlay-closed is fired', () => {
const resetSpy = sandbox.spy(element, '_resetEmojiDropdown');
element.$.emojiSuggestions.fire('dropdown-closed');
assert.isTrue(resetSpy.called);
});
test('_onValueChanged fires bind-value-changed', () => {
const listenerStub = sinon.stub();
const eventObject = {currentTarget: {focused: false}};
element.addEventListener('bind-value-changed', listenerStub);
element._onValueChanged(eventObject);
assert.isTrue(listenerStub.called);
});
suite('keyboard shortcuts', () => {
function setupDropdown(callback) {
MockInteractions.focus(element.$.textarea);
element.$.textarea.selectionStart = 1;
element.$.textarea.selectionEnd = 1;
element.text = ':';
element.$.textarea.selectionStart = 1;
element.$.textarea.selectionEnd = 2;
element.text = ':1';
flushAsynchronousOperations();
}
test('escape key', () => {
const resetSpy = sandbox.spy(element, '_resetEmojiDropdown');
MockInteractions.pressAndReleaseKeyOn(element.$.textarea, 27);
assert.isFalse(resetSpy.called);
setupDropdown();
MockInteractions.pressAndReleaseKeyOn(element.$.textarea, 27);
assert.isTrue(resetSpy.called);
assert.isFalse(!element.$.emojiSuggestions.isHidden);
});
test('up key', () => {
const upSpy = sandbox.spy(element.$.emojiSuggestions, 'cursorUp');
MockInteractions.pressAndReleaseKeyOn(element.$.textarea, 38);
assert.isFalse(upSpy.called);
setupDropdown();
MockInteractions.pressAndReleaseKeyOn(element.$.textarea, 38);
assert.isTrue(upSpy.called);
});
test('down key', () => {
const downSpy = sandbox.spy(element.$.emojiSuggestions, 'cursorDown');
MockInteractions.pressAndReleaseKeyOn(element.$.textarea, 40);
assert.isFalse(downSpy.called);
setupDropdown();
MockInteractions.pressAndReleaseKeyOn(element.$.textarea, 40);
assert.isTrue(downSpy.called);
});
test('enter key', () => {
const enterSpy = sandbox.spy(element.$.emojiSuggestions,
'getCursorTarget');
MockInteractions.pressAndReleaseKeyOn(element.$.textarea, 13);
assert.isFalse(enterSpy.called);
setupDropdown();
MockInteractions.pressAndReleaseKeyOn(element.$.textarea, 13);
assert.isTrue(enterSpy.called);
flushAsynchronousOperations();
// A space is automatically added at the end.
assert.equal(element.text, '💯 ');
});
});
});
</script>