@@ -27,36 +27,37 @@ export const waitUntil = async (
2727 try {
2828 result = await condition ( )
2929 error = undefined
30- if ( typeof result === 'boolean' ? result : result . success ) {
30+ if ( isBoolean ( result ) ? result : result . success ) {
3131 break
3232 }
3333 } catch ( err ) {
3434 error = err
3535 }
3636
3737 // No need to sleep again if time is already over
38- if ( Date . now ( ) - start < wait ) {
38+ if ( canWait ( start , wait ) ) {
3939 await sleep ( interval )
4040 }
41- } while ( Date . now ( ) - start < wait )
41+ } while ( canWait ( start , wait ) )
4242
4343 if ( error ) { throw error }
4444
45- if ( typeof result === 'boolean' ) {
45+ if ( isBoolean ( result ) ) {
4646 return result
4747 }
4848
4949 const { results } = result
5050
5151 if ( results . length === 0 ) {
52- // To fails when using .not, we need to send true so that Jest inverts it to false
52+ // To fails with .not, we need pass= true, so it s inverted later by Jest's expect framework
5353 return isNot
5454 }
5555
56- // TODO dprevost: Should we consider moving that into a strategy pattern, so that if a matching does not need the same pattern he can change it?
57- const allTrue = ( ) => results . every ( ( result ) => result )
58- const allFalse = ( ) => results . every ( ( result ) => ! result )
59-
60- // Needs to return true when all true, but with isNot, must be success=false when all false (failure=true when at least one true)
61- return isNot ? ! allFalse ( ) : allTrue ( )
56+ // With isNot to succeed with need pass=false, so it s inverted later by Jest's expect framework
57+ return isNot ? ! isAllFalse ( results ) : isAllTrue ( results )
6258}
59+ const isBoolean = ( value : unknown ) : value is boolean => typeof value === 'boolean'
60+
61+ const isAllTrue = ( results : boolean [ ] ) : boolean => results . every ( ( res ) => res === true )
62+ const isAllFalse = ( results : boolean [ ] ) : boolean => results . every ( ( res ) => res === false )
63+ const canWait = ( start : number , wait : number ) : boolean => ( Date . now ( ) - start ) < wait
0 commit comments