|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2022 Google LLC |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +import 'jasmine'; |
| 8 | +import './filled-text-field.js'; |
| 9 | + |
| 10 | +import {html} from 'lit'; |
| 11 | + |
| 12 | +import {Environment} from '../testing/environment.js'; |
| 13 | +import {Harness} from '../testing/harness.js'; |
| 14 | + |
| 15 | +import {TextFieldHarness} from './harness.js'; |
| 16 | + |
| 17 | +describe('TextFieldHarness', () => { |
| 18 | + const env = new Environment(); |
| 19 | + |
| 20 | + function setupTest() { |
| 21 | + const root = |
| 22 | + env.render(html`<md-filled-text-field></md-filled-text-field>`); |
| 23 | + const instance = root.querySelector('md-filled-text-field'); |
| 24 | + if (!instance) { |
| 25 | + throw new Error('Failed to query md-filled-text-field.'); |
| 26 | + } |
| 27 | + |
| 28 | + return new TextFieldHarness(instance); |
| 29 | + } |
| 30 | + |
| 31 | + describe('inputValue()', () => { |
| 32 | + it('should emit key events for each character typed', async () => { |
| 33 | + // Setup. |
| 34 | + const harness = setupTest(); |
| 35 | + const keydownHandler = jasmine.createSpy('keydownHandler'); |
| 36 | + harness.element.addEventListener('keydown', keydownHandler); |
| 37 | + // Test case. |
| 38 | + await harness.inputValue('abc'); |
| 39 | + // Assertion. |
| 40 | + expect(keydownHandler).toHaveBeenCalledTimes(3); |
| 41 | + expect(keydownHandler).toHaveBeenCalledWith(jasmine.any(KeyboardEvent)); |
| 42 | + expect(keydownHandler.calls.allArgs()).toEqual([ |
| 43 | + [jasmine.objectContaining({key: 'a'})], |
| 44 | + [jasmine.objectContaining({key: 'b'})], |
| 45 | + [jasmine.objectContaining({key: 'c'})], |
| 46 | + ]); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should emit input events for each character typed', async () => { |
| 50 | + // Setup. |
| 51 | + const harness = setupTest(); |
| 52 | + const inputHandler = jasmine.createSpy('inputHandler'); |
| 53 | + harness.element.addEventListener('input', inputHandler); |
| 54 | + // Test case. |
| 55 | + await harness.inputValue('abc'); |
| 56 | + // Assertion. |
| 57 | + expect(inputHandler).toHaveBeenCalledTimes(3); |
| 58 | + expect(inputHandler).toHaveBeenCalledWith(jasmine.any(InputEvent)); |
| 59 | + }); |
| 60 | + }); |
| 61 | + |
| 62 | + describe('deleteValue()', () => { |
| 63 | + it('should press the Backspace key', async () => { |
| 64 | + // Setup. |
| 65 | + const harness = setupTest(); |
| 66 | + const keydownHandler = jasmine.createSpy('keydownHandler'); |
| 67 | + harness.element.addEventListener('keydown', keydownHandler); |
| 68 | + harness.element.value = 'Value'; |
| 69 | + // Test case. |
| 70 | + await harness.deleteValue(); |
| 71 | + // Assertion. |
| 72 | + expect(keydownHandler).toHaveBeenCalledTimes(1); |
| 73 | + expect(keydownHandler).toHaveBeenCalledWith(jasmine.any(KeyboardEvent)); |
| 74 | + expect(keydownHandler).toHaveBeenCalledWith(jasmine.objectContaining({ |
| 75 | + key: 'Backspace' |
| 76 | + })); |
| 77 | + }); |
| 78 | + |
| 79 | + it('should delete the entire value by default', async () => { |
| 80 | + // Setup. |
| 81 | + const harness = setupTest(); |
| 82 | + harness.element.value = 'Value'; |
| 83 | + // Test case. |
| 84 | + await harness.deleteValue(); |
| 85 | + // Assertion. |
| 86 | + expect(harness.element.value).toBe(''); |
| 87 | + }); |
| 88 | + |
| 89 | + it('should allow deleting part of the value', async () => { |
| 90 | + // Setup. |
| 91 | + const harness = setupTest(); |
| 92 | + harness.element.value = 'Value'; |
| 93 | + // Test case. |
| 94 | + await harness.deleteValue(1, 4); |
| 95 | + // Assertion. |
| 96 | + expect(harness.element.value).toBe('Ve'); |
| 97 | + }); |
| 98 | + }); |
| 99 | + |
| 100 | + describe('reset()', () => { |
| 101 | + it('should set the value to an empty string', async () => { |
| 102 | + // Setup. |
| 103 | + const harness = setupTest(); |
| 104 | + harness.element.value = 'Value'; |
| 105 | + // Test case. |
| 106 | + await harness.reset(); |
| 107 | + // Assertion. |
| 108 | + expect(harness.element.value).toBe(''); |
| 109 | + }); |
| 110 | + |
| 111 | + it('should call super.reset()', async () => { |
| 112 | + // Setup. |
| 113 | + const harness = setupTest(); |
| 114 | + spyOn(Harness.prototype, 'reset'); |
| 115 | + // Test case. |
| 116 | + await harness.reset(); |
| 117 | + // Assertion. |
| 118 | + expect(Harness.prototype.reset).toHaveBeenCalledTimes(1); |
| 119 | + }); |
| 120 | + }); |
| 121 | + |
| 122 | + describe('field harness', () => { |
| 123 | + it('should call hoverEnter() on the field harness', async () => { |
| 124 | + // Setup. |
| 125 | + const harness = setupTest(); |
| 126 | + const field = await harness.field; |
| 127 | + spyOn(field, 'hoverEnter').and.callThrough(); |
| 128 | + // Test case. |
| 129 | + await harness.hoverEnter(); |
| 130 | + // Assertion. |
| 131 | + expect(field.hoverEnter).toHaveBeenCalledTimes(1); |
| 132 | + }); |
| 133 | + |
| 134 | + it('should call hoverLeave() on the field harness', async () => { |
| 135 | + // Setup. |
| 136 | + const harness = setupTest(); |
| 137 | + const field = await harness.field; |
| 138 | + spyOn(field, 'hoverLeave').and.callThrough(); |
| 139 | + // Test case. |
| 140 | + await harness.hoverLeave(); |
| 141 | + // Assertion. |
| 142 | + expect(field.hoverLeave).toHaveBeenCalledTimes(1); |
| 143 | + }); |
| 144 | + |
| 145 | + it('should call focusWithKeyboard() on the field harness', async () => { |
| 146 | + // Setup. |
| 147 | + const harness = setupTest(); |
| 148 | + const field = await harness.field; |
| 149 | + spyOn(field, 'focusWithKeyboard').and.callThrough(); |
| 150 | + // Test case. |
| 151 | + await harness.focusWithKeyboard(); |
| 152 | + // Assertion. |
| 153 | + expect(field.focusWithKeyboard).toHaveBeenCalledTimes(1); |
| 154 | + }); |
| 155 | + |
| 156 | + it('should call focusWithPointer() on the field harness', async () => { |
| 157 | + // Setup. |
| 158 | + const harness = setupTest(); |
| 159 | + const field = await harness.field; |
| 160 | + spyOn(field, 'focusWithPointer').and.callThrough(); |
| 161 | + // Test case. |
| 162 | + await harness.focusWithPointer(); |
| 163 | + // Assertion. |
| 164 | + expect(field.focusWithPointer).toHaveBeenCalledTimes(1); |
| 165 | + }); |
| 166 | + |
| 167 | + it('should call blur() on the field harness', async () => { |
| 168 | + // Setup. |
| 169 | + const harness = setupTest(); |
| 170 | + const field = await harness.field; |
| 171 | + spyOn(field, 'blur').and.callThrough(); |
| 172 | + // Test case. |
| 173 | + await harness.blur(); |
| 174 | + // Assertion. |
| 175 | + expect(field.blur).toHaveBeenCalledTimes(1); |
| 176 | + }); |
| 177 | + }); |
| 178 | + |
| 179 | + describe('simulating change events', () => { |
| 180 | + it('should dispatch change if value changes after focus and blur', |
| 181 | + async () => { |
| 182 | + // Setup. |
| 183 | + const harness = setupTest(); |
| 184 | + const changeHandler = jasmine.createSpy('changeHandler'); |
| 185 | + harness.element.addEventListener('change', changeHandler); |
| 186 | + // Test case. |
| 187 | + await harness.focusWithKeyboard(); |
| 188 | + await harness.inputValue('value'); |
| 189 | + await harness.blur(); |
| 190 | + // Assertion. |
| 191 | + expect(changeHandler).toHaveBeenCalledTimes(1); |
| 192 | + }); |
| 193 | + |
| 194 | + it('should not dispatch change if value does not change', async () => { |
| 195 | + // Setup. |
| 196 | + const harness = setupTest(); |
| 197 | + const changeHandler = jasmine.createSpy('changeHandler'); |
| 198 | + harness.element.value = 'value'; |
| 199 | + harness.element.addEventListener('change', changeHandler); |
| 200 | + // Test case. |
| 201 | + await harness.focusWithKeyboard(); |
| 202 | + await harness.blur(); |
| 203 | + // Assertion. |
| 204 | + expect(changeHandler).not.toHaveBeenCalled(); |
| 205 | + }); |
| 206 | + |
| 207 | + it('should not dispatch change if reset', async () => { |
| 208 | + // Setup. |
| 209 | + const harness = setupTest(); |
| 210 | + const changeHandler = jasmine.createSpy('changeHandler'); |
| 211 | + await harness.focusWithKeyboard(); |
| 212 | + await harness.inputValue('value'); |
| 213 | + harness.element.addEventListener('change', changeHandler); |
| 214 | + // Test case. |
| 215 | + await harness.reset(); |
| 216 | + await harness.blur(); |
| 217 | + // Assertion. |
| 218 | + expect(changeHandler).not.toHaveBeenCalled(); |
| 219 | + }); |
| 220 | + }); |
| 221 | +}); |
0 commit comments