Adrià Vilanova MartÃnez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 1 | // Copyright 2019 The Chromium Authors |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | import {assert} from 'chai'; |
| 6 | import {isTextInput, findDeepEventTarget} from './dom-helpers.js'; |
| 7 | |
| 8 | describe('isTextInput', () => { |
| 9 | it('returns true for select', () => { |
| 10 | const element = document.createElement('select'); |
| 11 | assert.isTrue(isTextInput(element)); |
| 12 | }); |
| 13 | |
| 14 | it('returns true for input tags that take text input', () => { |
| 15 | const element = document.createElement('input'); |
| 16 | assert.isTrue(isTextInput(element)); |
| 17 | |
| 18 | element.type = 'text'; |
| 19 | assert.isTrue(isTextInput(element)); |
| 20 | |
| 21 | element.type = 'password'; |
| 22 | assert.isTrue(isTextInput(element)); |
| 23 | |
| 24 | element.type = 'number'; |
| 25 | assert.isTrue(isTextInput(element)); |
| 26 | |
| 27 | element.type = 'date'; |
| 28 | assert.isTrue(isTextInput(element)); |
| 29 | }); |
| 30 | |
| 31 | it('returns false for input tags without text input', () => { |
| 32 | const element = document.createElement('input'); |
| 33 | |
| 34 | element.type = 'button'; |
| 35 | assert.isFalse(isTextInput(element)); |
| 36 | |
| 37 | element.type = 'submit'; |
| 38 | assert.isFalse(isTextInput(element)); |
| 39 | |
| 40 | element.type = 'checkbox'; |
| 41 | assert.isFalse(isTextInput(element)); |
| 42 | |
| 43 | element.type = 'radio'; |
| 44 | assert.isFalse(isTextInput(element)); |
| 45 | }); |
| 46 | |
| 47 | it('returns true for textarea', () => { |
| 48 | const element = document.createElement('textarea'); |
| 49 | assert.isTrue(isTextInput(element)); |
| 50 | }); |
| 51 | |
| 52 | it('returns true for contenteditable', () => { |
| 53 | const element = document.createElement('div'); |
| 54 | element.contentEditable = 'true'; |
| 55 | assert.isTrue(isTextInput(element)); |
| 56 | |
| 57 | element.contentEditable = 'false'; |
| 58 | assert.isFalse(isTextInput(element)); |
| 59 | }); |
| 60 | |
| 61 | it('returns false for non-input', () => { |
| 62 | assert.isFalse(isTextInput(document.createElement('div'))); |
| 63 | assert.isFalse(isTextInput(document.createElement('table'))); |
| 64 | assert.isFalse(isTextInput(document.createElement('tr'))); |
| 65 | assert.isFalse(isTextInput(document.createElement('td'))); |
| 66 | assert.isFalse(isTextInput(document.createElement('href'))); |
| 67 | assert.isFalse(isTextInput(document.createElement('random-elment'))); |
| 68 | assert.isFalse(isTextInput(document.createElement('p'))); |
| 69 | }); |
| 70 | }); |
| 71 | |
| 72 | describe('findDeepEventTarget', () => { |
| 73 | it('returns empty for event without target', () => { |
| 74 | const event = new Event('whatsup'); |
| 75 | assert.isUndefined(findDeepEventTarget(event)); |
| 76 | }); |
| 77 | |
| 78 | it('returns target for event with target', (done) => { |
| 79 | const element = document.createElement('div'); |
| 80 | element.addEventListener('hello', (e) => { |
| 81 | assert.deepEqual(findDeepEventTarget(e), element); |
| 82 | done(); |
| 83 | }); |
| 84 | element.dispatchEvent(new Event('hello')); |
| 85 | }); |
| 86 | |
| 87 | it('returns target for event coming from shadowRoot', (done) => { |
| 88 | const target = document.createElement('button'); |
| 89 | const parent = document.createElement('div'); |
| 90 | parent.appendChild(target); |
| 91 | parent.attachShadow({mode: 'open'}); |
| 92 | |
| 93 | parent.addEventListener('shadow-root', (e) => { |
| 94 | assert.deepEqual(findDeepEventTarget(e), target); |
| 95 | done(); |
| 96 | }); |
| 97 | |
| 98 | target.dispatchEvent(new Event('shadow-root', {bubbles: true})); |
| 99 | }); |
| 100 | }); |