blob: 1d1bce817e474d56bb59943d9b39ad19b7108c41 [file] [log] [blame]
<!DOCTYPE html>
<!--
@license
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">
<meta charset="utf-8">
<title>gr-editable-label</title>
<script src="/node_modules/@webcomponents/webcomponentsjs/custom-elements-es5-adapter.js"></script>
<script src="/node_modules/@webcomponents/webcomponentsjs/webcomponents-lite.js"></script>
<script src="/components/wct-browser-legacy/browser.js"></script>
<!-- Can't use absolute path below for mock-interaction.js.
Web component tester(wct) has a built-in http server and it serves "/components" directory (which is
actually /node_modules directory). Also, wct patches some files to load modules from /components.
With the absolute path, browser tries to load dom-module from 2 different places (/component/... and
/node_modules/...) though this is actually the same file. This leads to a run-time error.
-->
<script src="../../../node_modules/iron-test-helpers/mock-interactions.js"></script>
<test-fixture id="basic">
<template>
<gr-editable-label
value="value text"
placeholder="label text"></gr-editable-label>
</template>
</test-fixture>
<test-fixture id="no-placeholder">
<template>
<gr-editable-label value=""></gr-editable-label>
</template>
</test-fixture>
<test-fixture id="read-only">
<template>
<gr-editable-label
read-only
value="value text"
placeholder="label text"></gr-editable-label>
</template>
</test-fixture>
<script type="module">
import '../../../test/common-test-setup.js';
import './gr-editable-label.js';
import {flush as flush$0} from '@polymer/polymer/lib/legacy/polymer.dom.js';
suite('gr-editable-label tests', () => {
let element;
let elementNoPlaceholder;
let input;
let label;
let sandbox;
setup(done => {
element = fixture('basic');
elementNoPlaceholder = fixture('no-placeholder');
label = element.shadowRoot
.querySelector('label');
sandbox = sinon.sandbox.create();
flush(() => {
// In Polymer 2 inputElement isn't nativeInput anymore
input = element.$.input.$.nativeInput || element.$.input.inputElement;
done();
});
});
teardown(() => {
sandbox.restore();
});
test('element render', () => {
// The dropdown is closed and the label is visible:
assert.isFalse(element.$.dropdown.opened);
assert.isTrue(label.classList.contains('editable'));
assert.equal(label.textContent, 'value text');
const focusSpy = sandbox.spy(input, 'focus');
const showSpy = sandbox.spy(element, '_showDropdown');
MockInteractions.tap(label);
return showSpy.lastCall.returnValue.then(() => {
// The dropdown is open (which covers up the label):
assert.isTrue(element.$.dropdown.opened);
assert.isTrue(focusSpy.called);
assert.equal(input.value, 'value text');
});
});
test('title with placeholder', done => {
assert.equal(element.title, 'value text');
element.value = '';
element.async(() => {
assert.equal(element.title, 'label text');
done();
});
});
test('title without placeholder', done => {
assert.equal(elementNoPlaceholder.title, '');
element.value = 'value text';
element.async(() => {
assert.equal(element.title, 'value text');
done();
});
});
test('edit value', done => {
const editedStub = sandbox.stub();
element.addEventListener('changed', editedStub);
assert.isFalse(element.editing);
MockInteractions.tap(label);
flush$0();
assert.isTrue(element.editing);
element._inputText = 'new text';
assert.isFalse(editedStub.called);
element.async(() => {
assert.isTrue(editedStub.called);
assert.equal(input.value, 'new text');
assert.isFalse(element.editing);
done();
});
// Press enter:
MockInteractions.keyDownOn(input, 13);
});
test('save button', done => {
const editedStub = sandbox.stub();
element.addEventListener('changed', editedStub);
assert.isFalse(element.editing);
MockInteractions.tap(label);
flush$0();
assert.isTrue(element.editing);
element._inputText = 'new text';
assert.isFalse(editedStub.called);
element.async(() => {
assert.isTrue(editedStub.called);
assert.equal(input.value, 'new text');
assert.isFalse(element.editing);
done();
});
// Press enter:
MockInteractions.tap(element.$.saveBtn, 13);
});
test('edit and then escape key', done => {
const editedStub = sandbox.stub();
element.addEventListener('changed', editedStub);
assert.isFalse(element.editing);
MockInteractions.tap(label);
flush$0();
assert.isTrue(element.editing);
element._inputText = 'new text';
assert.isFalse(editedStub.called);
element.async(() => {
assert.isFalse(editedStub.called);
// Text changes should be discarded.
assert.equal(input.value, 'value text');
assert.isFalse(element.editing);
done();
});
// Press escape:
MockInteractions.keyDownOn(input, 27);
});
test('cancel button', done => {
const editedStub = sandbox.stub();
element.addEventListener('changed', editedStub);
assert.isFalse(element.editing);
MockInteractions.tap(label);
flush$0();
assert.isTrue(element.editing);
element._inputText = 'new text';
assert.isFalse(editedStub.called);
element.async(() => {
assert.isFalse(editedStub.called);
// Text changes should be discarded.
assert.equal(input.value, 'value text');
assert.isFalse(element.editing);
done();
});
// Press escape:
MockInteractions.tap(element.$.cancelBtn);
});
suite('gr-editable-label read-only tests', () => {
let element;
let label;
setup(() => {
element = fixture('read-only');
label = element.shadowRoot
.querySelector('label');
});
test('disallows edit when read-only', () => {
// The dropdown is closed.
assert.isFalse(element.$.dropdown.opened);
MockInteractions.tap(label);
flush$0();
// The dropdown is still closed.
assert.isFalse(element.$.dropdown.opened);
});
test('label is not marked as editable', () => {
assert.isFalse(label.classList.contains('editable'));
});
});
});
</script>