import { IonicModule } from '@ionic/angular' ; import { IonicConfig } from '@ionic/core' ; export class TestHelper { public static readonly _init = TestHelper . muteIonicReInitializeWarning ( ) ; public static muteIonicReInitializeWarning ( ) { const originalWarn = console . warn ; const patchedWarn = ( warning , ... optionalParams ) => { const suppress = `Ionic Angular was already initialized. Make sure IonicModule.forRoot() is just called once.` ; if ( warning !== suppress ) originalWarn ( warning , ... optionalParams ) ; } ; console . warn = patchedWarn ; } public static ionicModuleForTest = IonicModule . forRoot ( { mode : 'ios' , _testing : true , get : ( ) => { } , } as IonicConfig ) ; public static async whenReady ( elementOrNodeList ) { if ( elementOrNodeList === undefined || elementOrNodeList === null ) return Promise . reject ( 'TestHelper.whenReady: Element is null or undefined' ) ; if ( elementOrNodeList instanceof Element ) { await TestHelper . whenDefined ( elementOrNodeList ) ; await TestHelper . ionComponentOnReady ( elementOrNodeList ) ; } else { await Promise . all ( Array . from ( elementOrNodeList ) . map ( async ( element ) => await TestHelper . whenReady ( element ) ) ) ; } } public static async whenDefined ( element ) { await customElements . whenDefined ( element . localName ) ; } public static async ionComponentOnReady ( element ) { const componentOnReady = ( element as any ) . componentOnReady as ( ) => Promise < void > ; if ( typeof componentOnReady === 'function' ) { await componentOnReady . bind ( element ) ( ) ; } } public static async whenTrue ( pollFunc , timeoutInMs = 2000 , pollIntervalInMs = 5 ) { if ( pollFunc ( ) === true ) return Promise . resolve ( ) ; return new Promise < void > ( ( resolve , reject ) => { let timeoutId , intervalId ; const pollState = ( ) => { const result = pollFunc ( ) ; if ( result === true ) { clearTimeout ( timeoutId ) ; clearInterval ( intervalId ) ; resolve ( ) ; } } ; timeoutId = setTimeout ( ( ) => { clearInterval ( intervalId ) ; reject ( `Error: Timeout - TestHelper.whenTrue function did not complete within ${ timeoutInMs }ms` ) ; } , timeoutInMs ) ; intervalId = setInterval ( pollState , pollIntervalInMs ) ; } ) ; } public static getCssProperty ( element , propertyName ) { return window . getComputedStyle ( element ) . getPropertyValue ( propertyName ) . trim ( ) ; } public static screensize = { phonesmall : { width : '320px' , height : '568px' } , phone : { width : '375px' , height : '667px' } , phablet : { width : '575px' , height : '767px' } , 'phablet-landscape' : { width : '767px' , height : '575px' } , tablet : { width : '768px' , height : '1024px' } , desktop : { width : '1200px' , height : '900px' } , } ; public static resizeTestWindow ( size ) { return new Promise < void > ( ( resolve , reject ) => { if ( ! window . frameElement ) { console . warn ( 'window.frameElement is undefined. Cannot resize test window.' ) ; reject ( ) ; } else { const query = [ ] ; if ( size . width ) { query . push ( `(max-width: ${ size . width })` ) ; } if ( size . height ) { query . push ( `(max-height: ${ size . height })` ) ; } console . log ( `Set test window size to: ${ JSON . stringify ( size ) }` ) ; ( window . frameElement as HTMLIFrameElement ) . style . width = size . width ; ( window . frameElement as HTMLIFrameElement ) . style . height = size . height ; const mediaQuery = window . matchMedia ( query . join ( ' and ' ) ) ; if ( mediaQuery . matches ) { resolve ( ) ; } else { mediaQuery . onchange = ( ev ) => { if ( ev . matches ) { mediaQuery . onchange = null ; resolve ( ) ; } } ; console . log ( `Awaiting media query to match: ${ mediaQuery . media }` ) ; } } } ) ; } public static resetTestWindow ( ) { if ( ! window . frameElement ) { console . warn ( 'window.frameElement is undefined. Cannot resize test window.' ) ; return ; } console . log ( 'Resetting test window width' ) ; ( window . frameElement as HTMLIFrameElement ) . style . width = null ; ( window . frameElement as HTMLIFrameElement ) . style . height = null ; } public static scrollMainWindowToTop ( ) { if ( window . parent && window . parent . document && window . parent . document . documentElement && window . parent . document . documentElement . scrollTop > 0 ) { window . parent . document . documentElement . scrollTop = 0 ; } } public static waitForResizeObserver ( ) { return TestHelper . waitForTimeout ( ) ; } public static waitForTimeout ( timeoutInMs ? ) { return new Promise < void > ( ( resolve ) => setTimeout ( resolve , timeoutInMs ) ) ; } } export type ScreenSize = keyof typeof TestHelper . screensize ;