File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 66 * found in the LICENSE file at https://angular.dev/license
77 */
88
9- import { BrowserViewportScroller , ViewportScroller } from '../src/viewport_scroller' ;
109import { isNode , waitFor } from '@angular/private/testing' ;
10+ import { BrowserViewportScroller , ViewportScroller } from '../src/viewport_scroller' ;
1111
1212describe ( 'BrowserViewportScroller' , ( ) => {
1313 describe ( 'setHistoryScrollRestoration' , ( ) => {
Original file line number Diff line number Diff line change @@ -209,3 +209,50 @@ export function useAutoTick() {
209209 jasmine . clock ( ) . uninstall ( ) ;
210210 } ) ;
211211}
212+
213+ export interface WaitForOptions {
214+ timeout ?: number ;
215+ interval ?: number ;
216+ }
217+
218+ // Intentionally does not participate in fake clocks.
219+ const realNow = performance . now . bind ( performance ) ;
220+ const realSetTimeout = setTimeout ;
221+
222+ export async function waitFor < T > (
223+ callback : ( ) => Promise < T > | T ,
224+ options : WaitForOptions = { } ,
225+ ) : Promise < T > {
226+ const waitTime = options . timeout ?? 100 ;
227+ const interval = options . interval ?? 0 ;
228+ const stack = new Error ( ) . stack ;
229+
230+ const deadline = realNow ( ) + waitTime ;
231+ let i = 0 ;
232+ let lastError : any | undefined ;
233+
234+ while ( true ) {
235+ try {
236+ return await callback ( ) ;
237+ } catch ( cause ) {
238+ lastError = cause ;
239+ }
240+
241+ i ++ ;
242+
243+ if ( deadline < realNow ( ) ) {
244+ throw Object . assign (
245+ new Error (
246+ `Timed out after ${ waitTime } ms and ${ i } attempts. ` +
247+ `Last error: ${ lastError ?. message ?? 'condition returned false' } ` ,
248+ ) ,
249+ {
250+ stack : stack + `Last error: ${ lastError ?. stack ?? 'condition returned false' } ` ,
251+ } ,
252+ ) ;
253+ }
254+
255+ // Guarantee a macro-task between retries.
256+ await new Promise ( ( resolve ) => void realSetTimeout ( resolve , interval ) ) ;
257+ }
258+ }
You can’t perform that action at this time.
0 commit comments