Skip to content

Commit 5cddadf

Browse files
committed
fix(cdk/testing): simulate focusin/focusout events
Fixes that our fake fallback focus events weren't dispatching `focusin` and `focusout` events as well. Fixes #23757.
1 parent 5f72dbc commit 5cddadf

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

Diff for: src/cdk/testing/testbed/fake-events/element-focus.ts

+12-3
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,20 @@ function triggerFocusChange(element: HTMLElement, event: 'focus' | 'blur') {
1414
element.addEventListener(event, handler);
1515
element[event]();
1616
element.removeEventListener(event, handler);
17+
18+
// Some browsers won't move focus if the browser window is blurred while other will move it
19+
// asynchronously. If that is the case, we fake the event sequence as a fallback.
1720
if (!eventFired) {
18-
dispatchFakeEvent(element, event);
21+
simulateFocusSequence(element, event);
1922
}
2023
}
2124

25+
/** Simulates the full event sequence for a focus event. */
26+
function simulateFocusSequence(element: HTMLElement, event: 'focus' | 'blur') {
27+
dispatchFakeEvent(element, event);
28+
dispatchFakeEvent(element, event === 'focus' ? 'focusin' : 'focusout');
29+
}
30+
2231
/**
2332
* Patches an elements focus and blur methods to emit events consistently and predictably.
2433
* This is necessary, because some browsers can call the focus handlers asynchronously,
@@ -28,8 +37,8 @@ function triggerFocusChange(element: HTMLElement, event: 'focus' | 'blur') {
2837
// TODO: Check if this element focus patching is still needed for local testing,
2938
// where browser is not necessarily focused.
3039
export function patchElementFocus(element: HTMLElement) {
31-
element.focus = () => dispatchFakeEvent(element, 'focus');
32-
element.blur = () => dispatchFakeEvent(element, 'blur');
40+
element.focus = () => simulateFocusSequence(element, 'focus');
41+
element.blur = () => simulateFocusSequence(element, 'blur');
3342
}
3443

3544
/** @docs-private */

0 commit comments

Comments
 (0)