blob: 5eff42da5d38d75bcd89fe9248f2fd98abb088a1 [file] [log] [blame]
Kasper Nilssonef156352018-11-14 16:35:58 -08001<!DOCTYPE html>
2<!--
3@license
4Copyright (C) 2018 The Android Open Source Project
5
6Licensed under the Apache License, Version 2.0 (the "License");
7you may not use this file except in compliance with the License.
8You may obtain a copy of the License at
9
10http://www.apache.org/licenses/LICENSE-2.0
11
12Unless required by applicable law or agreed to in writing, software
13distributed under the License is distributed on an "AS IS" BASIS,
14WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15See the License for the specific language governing permissions and
16limitations under the License.
17-->
18
19<meta name="viewport" content="width=device-width, minimum-scale=1.0, initial-scale=1.0, user-scalable=yes">
Tao Zhoud90e7f42020-04-29 17:07:14 +020020<meta charset="utf-8">
Kasper Nilssonef156352018-11-14 16:35:58 -080021<title>gr-plugin-config-array-editor</title>
Tao Zhou8ef16f72019-11-18 14:14:36 -080022
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +010023<script src="/node_modules/@webcomponents/webcomponentsjs/custom-elements-es5-adapter.js"></script>
Kasper Nilssonef156352018-11-14 16:35:58 -080024
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +010025<script src="/node_modules/@webcomponents/webcomponentsjs/webcomponents-lite.js"></script>
26<script src="/components/wct-browser-legacy/browser.js"></script>
Kasper Nilssonef156352018-11-14 16:35:58 -080027
28<test-fixture id="basic">
29 <template>
30 <gr-plugin-config-array-editor></gr-plugin-config-array-editor>
31 </template>
32</test-fixture>
33
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +010034<script type="module">
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +010035import '../../../test/common-test-setup.js';
36import './gr-plugin-config-array-editor.js';
37import {dom} from '@polymer/polymer/lib/legacy/polymer.dom.js';
38suite('gr-plugin-config-array-editor tests', () => {
39 let element;
40 let sandbox;
41 let dispatchStub;
Kasper Nilssonef156352018-11-14 16:35:58 -080042
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +010043 const getAll = str => dom(element.root).querySelectorAll(str);
Kasper Nilssonef156352018-11-14 16:35:58 -080044
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +010045 setup(() => {
46 sandbox = sinon.sandbox.create();
47 element = fixture('basic');
48 element.pluginOption = {
49 _key: 'test-key',
50 info: {
51 values: [],
52 },
53 };
54 });
55
56 teardown(() => sandbox.restore());
57
58 test('_computeShowInputRow', () => {
59 assert.equal(element._computeShowInputRow(true), 'hide');
60 assert.equal(element._computeShowInputRow(false), '');
61 });
62
63 test('_computeDisabled', () => {
64 assert.isTrue(element._computeDisabled({}));
65 assert.isTrue(element._computeDisabled({base: {}}));
66 assert.isTrue(element._computeDisabled({base: {info: {}}}));
67 assert.isTrue(
68 element._computeDisabled({base: {info: {editable: false}}}));
69 assert.isFalse(
70 element._computeDisabled({base: {info: {editable: true}}}));
71 });
72
73 suite('adding', () => {
Kasper Nilssonef156352018-11-14 16:35:58 -080074 setup(() => {
Kasper Nilssonef156352018-11-14 16:35:58 -080075 dispatchStub = sandbox.stub(element, '_dispatchChanged');
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +010076 });
Kasper Nilssonef156352018-11-14 16:35:58 -080077
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +010078 test('with enter', () => {
79 element._newValue = '';
80 MockInteractions.pressAndReleaseKeyOn(element.$.input, 13); // Enter
Kasper Nilssonef156352018-11-14 16:35:58 -080081 flushAsynchronousOperations();
82
83 assert.isFalse(dispatchStub.called);
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +010084 element._newValue = 'test';
85 MockInteractions.pressAndReleaseKeyOn(element.$.input, 13); // Enter
Kasper Nilssonef156352018-11-14 16:35:58 -080086 flushAsynchronousOperations();
87
88 assert.isTrue(dispatchStub.called);
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +010089 assert.equal(dispatchStub.lastCall.args[0], 'test');
90 assert.equal(element._newValue, '');
Kasper Nilssonef156352018-11-14 16:35:58 -080091 });
92
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +010093 test('with add btn', () => {
94 element._newValue = '';
95 MockInteractions.tap(element.$.addButton);
96 flushAsynchronousOperations();
Kasper Nilssonef156352018-11-14 16:35:58 -080097
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +010098 assert.isFalse(dispatchStub.called);
99 element._newValue = 'test';
100 MockInteractions.tap(element.$.addButton);
101 flushAsynchronousOperations();
102
103 assert.isTrue(dispatchStub.called);
104 assert.equal(dispatchStub.lastCall.args[0], 'test');
105 assert.equal(element._newValue, '');
Kasper Nilssonef156352018-11-14 16:35:58 -0800106 });
107 });
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100108
109 test('deleting', () => {
110 dispatchStub = sandbox.stub(element, '_dispatchChanged');
111 element.pluginOption = {info: {values: ['test', 'test2']}};
112 flushAsynchronousOperations();
113
114 const rows = getAll('.existingItems .row');
115 assert.equal(rows.length, 2);
116 const button = rows[0].querySelector('gr-button');
117
118 MockInteractions.tap(button);
119 flushAsynchronousOperations();
120
121 assert.isFalse(dispatchStub.called);
122 element.pluginOption.info.editable = true;
123 element.notifyPath('pluginOption.info.editable');
124 flushAsynchronousOperations();
125
126 MockInteractions.tap(button);
127 flushAsynchronousOperations();
128
129 assert.isTrue(dispatchStub.called);
130 assert.deepEqual(dispatchStub.lastCall.args[0], ['test2']);
131 });
132
133 test('_dispatchChanged', () => {
134 const eventStub = sandbox.stub(element, 'dispatchEvent');
135 element._dispatchChanged(['new-test-value']);
136
137 assert.isTrue(eventStub.called);
138 const {detail} = eventStub.lastCall.args[0];
139 assert.equal(detail._key, 'test-key');
140 assert.deepEqual(detail.info, {values: ['new-test-value']});
141 assert.equal(detail.notifyPath, 'test-key.values');
142 });
143});
Kasper Nilssonef156352018-11-14 16:35:58 -0800144</script>