Kasper Nilsson | ef15635 | 2018-11-14 16:35:58 -0800 | [diff] [blame] | 1 | <!DOCTYPE html> |
| 2 | <!-- |
| 3 | @license |
| 4 | Copyright (C) 2018 The Android Open Source Project |
| 5 | |
| 6 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | you may not use this file except in compliance with the License. |
| 8 | You may obtain a copy of the License at |
| 9 | |
| 10 | http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | |
| 12 | Unless required by applicable law or agreed to in writing, software |
| 13 | distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | See the License for the specific language governing permissions and |
| 16 | limitations 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 Zhou | d90e7f4 | 2020-04-29 17:07:14 +0200 | [diff] [blame] | 20 | <meta charset="utf-8"> |
Kasper Nilsson | ef15635 | 2018-11-14 16:35:58 -0800 | [diff] [blame] | 21 | <title>gr-plugin-config-array-editor</title> |
Tao Zhou | 8ef16f7 | 2019-11-18 14:14:36 -0800 | [diff] [blame] | 22 | |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 23 | <script src="/node_modules/@webcomponents/webcomponentsjs/custom-elements-es5-adapter.js"></script> |
Kasper Nilsson | ef15635 | 2018-11-14 16:35:58 -0800 | [diff] [blame] | 24 | |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 25 | <script src="/node_modules/@webcomponents/webcomponentsjs/webcomponents-lite.js"></script> |
| 26 | <script src="/components/wct-browser-legacy/browser.js"></script> |
Kasper Nilsson | ef15635 | 2018-11-14 16:35:58 -0800 | [diff] [blame] | 27 | |
| 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 Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 34 | <script type="module"> |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 35 | import '../../../test/common-test-setup.js'; |
| 36 | import './gr-plugin-config-array-editor.js'; |
| 37 | import {dom} from '@polymer/polymer/lib/legacy/polymer.dom.js'; |
| 38 | suite('gr-plugin-config-array-editor tests', () => { |
| 39 | let element; |
| 40 | let sandbox; |
| 41 | let dispatchStub; |
Kasper Nilsson | ef15635 | 2018-11-14 16:35:58 -0800 | [diff] [blame] | 42 | |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 43 | const getAll = str => dom(element.root).querySelectorAll(str); |
Kasper Nilsson | ef15635 | 2018-11-14 16:35:58 -0800 | [diff] [blame] | 44 | |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 45 | 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 Nilsson | ef15635 | 2018-11-14 16:35:58 -0800 | [diff] [blame] | 74 | setup(() => { |
Kasper Nilsson | ef15635 | 2018-11-14 16:35:58 -0800 | [diff] [blame] | 75 | dispatchStub = sandbox.stub(element, '_dispatchChanged'); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 76 | }); |
Kasper Nilsson | ef15635 | 2018-11-14 16:35:58 -0800 | [diff] [blame] | 77 | |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 78 | test('with enter', () => { |
| 79 | element._newValue = ''; |
| 80 | MockInteractions.pressAndReleaseKeyOn(element.$.input, 13); // Enter |
Kasper Nilsson | ef15635 | 2018-11-14 16:35:58 -0800 | [diff] [blame] | 81 | flushAsynchronousOperations(); |
| 82 | |
| 83 | assert.isFalse(dispatchStub.called); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 84 | element._newValue = 'test'; |
| 85 | MockInteractions.pressAndReleaseKeyOn(element.$.input, 13); // Enter |
Kasper Nilsson | ef15635 | 2018-11-14 16:35:58 -0800 | [diff] [blame] | 86 | flushAsynchronousOperations(); |
| 87 | |
| 88 | assert.isTrue(dispatchStub.called); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 89 | assert.equal(dispatchStub.lastCall.args[0], 'test'); |
| 90 | assert.equal(element._newValue, ''); |
Kasper Nilsson | ef15635 | 2018-11-14 16:35:58 -0800 | [diff] [blame] | 91 | }); |
| 92 | |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 93 | test('with add btn', () => { |
| 94 | element._newValue = ''; |
| 95 | MockInteractions.tap(element.$.addButton); |
| 96 | flushAsynchronousOperations(); |
Kasper Nilsson | ef15635 | 2018-11-14 16:35:58 -0800 | [diff] [blame] | 97 | |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 98 | 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 Nilsson | ef15635 | 2018-11-14 16:35:58 -0800 | [diff] [blame] | 106 | }); |
| 107 | }); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 108 | |
| 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 Nilsson | ef15635 | 2018-11-14 16:35:58 -0800 | [diff] [blame] | 144 | </script> |