Skip to content

Commit fef1ec8

Browse files
committed
liast round of feedback
1 parent a666f34 commit fef1ec8

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

src/cdk-experimental/testing/testbed.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,10 @@ class UnitTestElement implements TestElement {
106106

107107
async clear(): Promise<void> {
108108
await this._stabilize();
109-
if (!(this.element instanceof HTMLInputElement ||
110-
this.element instanceof HTMLTextAreaElement)) {
109+
if (!this._isTextInput(this.element)) {
111110
throw Error('Attempting to clear an invalid element');
112111
}
113-
triggerFocus(this.element);
112+
triggerFocus(this.element as HTMLElement);
114113
this.element.value = '';
115114
dispatchFakeEvent(this.element, 'input');
116115
await this._stabilize();
@@ -148,9 +147,13 @@ class UnitTestElement implements TestElement {
148147
const keyCode = key.charCodeAt(0);
149148
dispatchKeyboardEvent(this.element, 'keydown', keyCode);
150149
dispatchKeyboardEvent(this.element, 'keypress', keyCode);
151-
(this.element as HTMLInputElement).value += key;
150+
if (this._isTextInput(this.element)) {
151+
this.element.value += key;
152+
}
152153
dispatchKeyboardEvent(this.element, 'keyup', keyCode);
153-
dispatchFakeEvent(this.element, 'input');
154+
if (this._isTextInput(this.element)) {
155+
dispatchFakeEvent(this.element, 'input');
156+
}
154157
}
155158
await this._stabilize();
156159
}
@@ -171,6 +174,11 @@ class UnitTestElement implements TestElement {
171174
}
172175
return value;
173176
}
177+
178+
private _isTextInput(element: Element): element is HTMLInputElement | HTMLTextAreaElement {
179+
return element.nodeName.toLowerCase() === 'input' ||
180+
element.nodeName.toLowerCase() === 'textarea' ;
181+
}
174182
}
175183

176184

0 commit comments

Comments
 (0)