@@ -75,7 +75,8 @@ function isDocumentOrWindowEvent(scope: QwikLoaderEventScope): boolean {
7575}
7676
7777/**
78- * Trigger an event in unit tests on an element.
78+ * Trigger an event in unit tests on an element. Needs to be kept in sync with the Qwik Loader event
79+ * dispatching.
7980 *
8081 * Future deprecation candidate.
8182 *
@@ -89,6 +90,22 @@ export async function trigger(
8990 options ?: { waitForIdle ?: boolean }
9091) : Promise < void > {
9192 const waitForIdle = options ?. waitForIdle ?? true ;
93+
94+ let scope : QwikLoaderEventScope = '' ;
95+ if ( eventName . startsWith ( ':' ) ) {
96+ // :document:event or :window:event
97+ const colonIndex = eventName . substring ( 1 ) . indexOf ( ':' ) ;
98+ // we need to add `-` for event, because of scope of the qwik loader
99+ scope = ( '-' + eventName . substring ( 1 , colonIndex + 1 ) ) as '-document' | '-window' ;
100+ eventName = eventName . substring ( colonIndex + 2 ) ;
101+ // Scoped events must be queried
102+ if ( scope === '-document' ) {
103+ queryOrElement = `[on-document\\:${ fromCamelToKebabCase ( eventName ) } ]` ;
104+ } else {
105+ queryOrElement = `[on-window\\:${ fromCamelToKebabCase ( eventName ) } ]` ;
106+ }
107+ }
108+
92109 const elements =
93110 typeof queryOrElement === 'string'
94111 ? Array . from ( root . querySelectorAll ( queryOrElement ) )
@@ -102,15 +119,6 @@ export async function trigger(
102119 container = getDomContainer ( element as HTMLElement ) ;
103120 }
104121
105- let scope : QwikLoaderEventScope = '' ;
106- if ( eventName . startsWith ( ':' ) ) {
107- // :document:event or :window:event
108- const colonIndex = eventName . substring ( 1 ) . indexOf ( ':' ) ;
109- // we need to add `-` for event, because of scope of the qwik loader
110- scope = ( '-' + eventName . substring ( 1 , colonIndex + 1 ) ) as '-document' | '-window' ;
111- eventName = eventName . substring ( colonIndex + 2 ) ;
112- }
113-
114122 const event = new Event ( eventName , {
115123 bubbles : true ,
116124 cancelable : true ,
0 commit comments