File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -17,6 +17,23 @@ const tick = async (ms = 0) => {
1717 await new Promise ( ( resolve ) => setTimeout ( resolve , ms ) )
1818}
1919
20+ const waitForElement = async < T extends Element > (
21+ root : ParentNode ,
22+ selector : string ,
23+ timeoutMs = 1_000 ,
24+ ) : Promise < T > => {
25+ const deadline = Date . now ( ) + timeoutMs
26+ while ( Date . now ( ) < deadline ) {
27+ const element = root . querySelector ( selector )
28+ if ( element ) {
29+ return element as T
30+ }
31+ await tick ( 10 )
32+ }
33+
34+ throw new Error ( `Timed out waiting for selector: ${ selector } ` )
35+ }
36+
2037afterEach ( ( ) => {
2138 const runtimeHost = globalThis as { __FICT_DEV__ ?: boolean }
2239 runtimeHost . __FICT_DEV__ = undefined
@@ -131,8 +148,8 @@ describe('reactify$', () => {
131148 } ) ,
132149 container ,
133150 )
134- await tick ( 30 )
135- ; ( container . querySelector ( '#action-button' ) as HTMLButtonElement ) . click ( )
151+ const actionButton = await waitForElement < HTMLButtonElement > ( container , '#action-button' )
152+ actionButton . click ( )
136153 await tick ( 30 )
137154
138155 expect ( actionHost . __FICT_REACT_ACTION_CALLS__ ) . toEqual ( [ 'clicked:run' ] )
@@ -169,8 +186,11 @@ describe('reactify$', () => {
169186 } ) ,
170187 container ,
171188 )
172- await tick ( 30 )
173- ; ( container . querySelector ( '#custom-action-button' ) as HTMLButtonElement ) . click ( )
189+ const customActionButton = await waitForElement < HTMLButtonElement > (
190+ container ,
191+ '#custom-action-button' ,
192+ )
193+ customActionButton . click ( )
174194 await tick ( 30 )
175195
176196 expect ( actionHost . __FICT_REACT_ACTION_CALLS__ ) . toEqual ( [ 'custom:option' ] )
You can’t perform that action at this time.
0 commit comments