Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/cdk/testing/testbed/fake-events/event-objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export function createMouseEvent(
bubbles: true,
cancelable: true,
composed: true, // Required for shadow DOM events.
view: window,
view: getEventView(),
detail: 1,
relatedTarget: null,
screenX,
Expand Down Expand Up @@ -85,7 +85,7 @@ export function createPointerEvent(
bubbles: true,
cancelable: true,
composed: true, // Required for shadow DOM events.
view: window,
view: getEventView(),
clientX,
clientY,
...options,
Expand Down Expand Up @@ -139,7 +139,7 @@ export function createKeyboardEvent(
bubbles: true,
cancelable: true,
composed: true, // Required for shadow DOM events.
view: window,
view: getEventView(),
keyCode,
key,
shiftKey: modifiers.shift,
Expand All @@ -165,3 +165,13 @@ export function createFakeEvent(type: string, bubbles = false, cancelable = true
function defineReadonlyEventProperty(event: Event, propertyName: string, value: any) {
Object.defineProperty(event, propertyName, {get: () => value, configurable: true});
}

/** Gets the `view` that should be passed to synthetically-created DOM events */
function getEventView(): Window | undefined {
// Passing `window` as the `view` on for events when the environment is using jsdom
// ends up throwing `member view is not of type Window` (see #32389). Leave it as
// `undefined` for such cases.
return typeof window !== 'undefined' && window && !(window as Window & {jsdom?: unknown}).jsdom
Copy link
Contributor

@alan-agius4 alan-agius4 Nov 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT, redundant check

Suggested change
return typeof window !== 'undefined' && window && !(window as Window & {jsdom?: unknown}).jsdom
return typeof window !== 'undefined' && !(window as Window & {jsdom?: unknown}).jsdom

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The check is there because technically window can be null which is typeof === 'object'.

? window
: undefined;
}
Loading