|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2019 Google Inc. All Rights Reserved. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +import {TextArea} from '@material/mwc-textarea'; |
| 19 | +import {html} from 'lit-html'; |
| 20 | + |
| 21 | +import {fixture, TestFixture} from '../../../../test/src/util/helpers'; |
| 22 | + |
| 23 | + |
| 24 | +const basic = html` |
| 25 | + <mwc-textarea></mwc-textarea> |
| 26 | +`; |
| 27 | + |
| 28 | +suite('mwc-textarea:', () => { |
| 29 | + let fixt: TestFixture; |
| 30 | + |
| 31 | + suite('basic', () => { |
| 32 | + let element: TextArea; |
| 33 | + setup(async () => { |
| 34 | + fixt = await fixture(basic); |
| 35 | + |
| 36 | + element = fixt.root.querySelector('mwc-textarea')!; |
| 37 | + }); |
| 38 | + |
| 39 | + test('initializes as an mwc-textarea', () => { |
| 40 | + assert.instanceOf(element, TextArea); |
| 41 | + }); |
| 42 | + |
| 43 | + test('setting value sets on textarea', async () => { |
| 44 | + element.value = 'my test value'; |
| 45 | + |
| 46 | + const inputElement = element.shadowRoot!.querySelector('textarea'); |
| 47 | + assert(inputElement, 'my test value'); |
| 48 | + }); |
| 49 | + |
| 50 | + teardown(() => { |
| 51 | + if (fixt) { |
| 52 | + fixt.remove(); |
| 53 | + } |
| 54 | + }); |
| 55 | + }); |
| 56 | + |
| 57 | + suite('helper and char counter rendering', () => { |
| 58 | + let fixt: TestFixture; |
| 59 | + |
| 60 | + setup(async () => { |
| 61 | + fixt = await fixture(basic); |
| 62 | + }); |
| 63 | + |
| 64 | + test( |
| 65 | + 'createFoundation called an appropriate amount of times & render interactions', |
| 66 | + async () => { |
| 67 | + const element = fixt.root.querySelector('mwc-textarea')!; |
| 68 | + element.helperPersistent = true; |
| 69 | + |
| 70 | + const oldCreateFoundation = |
| 71 | + (element as any).createFoundation.bind(element) as () => void; |
| 72 | + let numTimesCreateFoundationCalled = 0; |
| 73 | + |
| 74 | + ((element as any).createFoundation as () => void) = () => { |
| 75 | + numTimesCreateFoundationCalled = numTimesCreateFoundationCalled + 1; |
| 76 | + oldCreateFoundation(); |
| 77 | + }; |
| 78 | + |
| 79 | + const charCounters = element.shadowRoot!.querySelectorAll( |
| 80 | + '.mdc-text-field-character-counter'); |
| 81 | + |
| 82 | + assert.strictEqual(charCounters.length, 1, 'only one char counter'); |
| 83 | + |
| 84 | + const charCounter = charCounters[0] as HTMLElement; |
| 85 | + const helperText = element.shadowRoot!.querySelector( |
| 86 | + '.mdc-text-field-helper-text') as HTMLElement; |
| 87 | + |
| 88 | + |
| 89 | + assert.strictEqual( |
| 90 | + charCounter.offsetWidth, 0, 'char counter initially hidden'); |
| 91 | + assert.strictEqual( |
| 92 | + helperText.offsetWidth, 0, 'helper line initially hidden'); |
| 93 | + |
| 94 | + element.helper = 'my helper'; |
| 95 | + await element.requestUpdate(); |
| 96 | + |
| 97 | + assert.strictEqual( |
| 98 | + numTimesCreateFoundationCalled, |
| 99 | + 0, |
| 100 | + 'foundation not recreated due to helper change'); |
| 101 | + assert.strictEqual( |
| 102 | + charCounter.offsetWidth, |
| 103 | + 0, |
| 104 | + 'char counter hidden when only helper defined'); |
| 105 | + assert.isTrue( |
| 106 | + helperText.offsetWidth > 0, 'helper text shown when defined'); |
| 107 | + |
| 108 | + element.helper = ''; |
| 109 | + await element.requestUpdate(); |
| 110 | + |
| 111 | + assert.strictEqual( |
| 112 | + numTimesCreateFoundationCalled, |
| 113 | + 0, |
| 114 | + 'foundation not recreated due to helper change'); |
| 115 | + assert.strictEqual( |
| 116 | + charCounter.offsetWidth, |
| 117 | + 0, |
| 118 | + 'char counter does not render on helper change'); |
| 119 | + assert.strictEqual( |
| 120 | + helperText.offsetWidth, |
| 121 | + 0, |
| 122 | + 'helper line hides when reset to empty'); |
| 123 | + |
| 124 | + element.maxLength = 10; |
| 125 | + await element.requestUpdate(); |
| 126 | + |
| 127 | + assert.strictEqual( |
| 128 | + numTimesCreateFoundationCalled, |
| 129 | + 1, |
| 130 | + 'foundation created when maxlength changed from -1'); |
| 131 | + assert.strictEqual( |
| 132 | + charCounter.offsetWidth, |
| 133 | + 0, |
| 134 | + 'char counter does not render without charCounter set'); |
| 135 | + assert.strictEqual( |
| 136 | + helperText.offsetWidth, |
| 137 | + 0, |
| 138 | + 'helper line does not render on maxLength change'); |
| 139 | + |
| 140 | + numTimesCreateFoundationCalled = 0; |
| 141 | + element.maxLength = -1; |
| 142 | + await element.requestUpdate(); |
| 143 | + |
| 144 | + assert.strictEqual( |
| 145 | + numTimesCreateFoundationCalled, |
| 146 | + 1, |
| 147 | + 'foundation created when maxlength changed to -1'); |
| 148 | + |
| 149 | + numTimesCreateFoundationCalled = 0; |
| 150 | + element.charCounter = true; |
| 151 | + await element.requestUpdate(); |
| 152 | + |
| 153 | + assert.strictEqual( |
| 154 | + numTimesCreateFoundationCalled, |
| 155 | + 0, |
| 156 | + 'foundation not updated when charCounter changed'); |
| 157 | + assert.strictEqual( |
| 158 | + charCounter.offsetWidth, |
| 159 | + 0, |
| 160 | + 'char counter does not render without maxLength set'); |
| 161 | + assert.strictEqual( |
| 162 | + helperText.offsetWidth, |
| 163 | + 0, |
| 164 | + 'helper line does not render on charCounter change'); |
| 165 | + |
| 166 | + element.maxLength = 20; |
| 167 | + await element.requestUpdate(); |
| 168 | + |
| 169 | + assert.strictEqual( |
| 170 | + numTimesCreateFoundationCalled, |
| 171 | + 1, |
| 172 | + 'foundation created when maxlength changed from -1'); |
| 173 | + assert.isTrue( |
| 174 | + charCounter.offsetWidth > 0, |
| 175 | + 'char counter renders when both charCounter and maxLength set'); |
| 176 | + |
| 177 | + numTimesCreateFoundationCalled = 0; |
| 178 | + element.maxLength = 15; |
| 179 | + await element.requestUpdate(); |
| 180 | + |
| 181 | + assert.strictEqual( |
| 182 | + numTimesCreateFoundationCalled, |
| 183 | + 0, |
| 184 | + 'foundation not recreated when maxLength not changed to or from -1'); |
| 185 | + assert.isTrue( |
| 186 | + charCounter.offsetWidth > 0, |
| 187 | + 'char counter still visible on maxLength change'); |
| 188 | + }); |
| 189 | + |
| 190 | + teardown(() => { |
| 191 | + if (fixt) { |
| 192 | + fixt.remove(); |
| 193 | + } |
| 194 | + }); |
| 195 | + }); |
| 196 | +}); |
0 commit comments