2022-09-02 02:00:48 +00:00
import type { Page } from "@playwright/test" ;
export const createEmbedsFixture = ( page : Page ) = > {
2023-10-10 03:10:04 +00:00
return {
/ * *
* @deprecated Use gotoPlayground instead
* /
async addEmbedListeners ( calNamespace : string ) {
await page . addInitScript (
( { calNamespace } : { calNamespace : string } ) = > {
console . log ( "PlaywrightTest:" , "Adding listener for __iframeReady on namespace:" , calNamespace ) ;
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore
window . eventsFiredStoreForPlaywright = window . eventsFiredStoreForPlaywright || { } ;
document . addEventListener ( "DOMContentLoaded" , function tryAddingListener() {
if ( parent !== window ) {
// Firefox seems to execute this snippet for iframe as well. Avoid that. It must be executed only for parent frame.
2022-09-02 02:00:48 +00:00
2023-10-10 03:10:04 +00:00
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore
window . initialBodyVisibility = document . body . style . visibility ;
2022-09-02 02:00:48 +00:00
2023-10-10 03:10:04 +00:00
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore
window . initialBodyBackground = document . body . style . background ;
2022-09-02 02:00:48 +00:00
2023-10-10 03:10:04 +00:00
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore
window . initialValuesSet = true ;
return ;
}
2022-09-02 02:00:48 +00:00
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore
2023-10-10 03:10:04 +00:00
let api = window . Cal ;
2022-09-02 02:00:48 +00:00
2023-10-10 03:10:04 +00:00
if ( ! api ) {
console . log ( "PlaywrightTest:" , "window.Cal not available yet, trying again" ) ;
setTimeout ( tryAddingListener , 500 ) ;
return ;
}
if ( calNamespace ) {
2022-09-02 02:00:48 +00:00
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
2023-10-10 03:10:04 +00:00
//@ts-ignore
api = window . Cal . ns [ calNamespace ] ;
}
console . log ( "PlaywrightTest:" , ` Adding listener for __iframeReady on namespace: ${ calNamespace } ` ) ;
if ( ! api ) {
throw new Error ( ` namespace " ${ calNamespace } " not found ` ) ;
}
api ( "on" , {
action : "*" ,
callback : ( e ) = > {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
window . iframeReady = true ; // Technically if there are multiple cal embeds, it can be set due to some other iframe. But it works for now. Improve it when it doesn't work
2022-09-02 02:00:48 +00:00
2023-10-10 03:10:04 +00:00
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const store = window . eventsFiredStoreForPlaywright ;
const eventStore = ( store [ ` ${ e . detail . type } - ${ e . detail . namespace } ` ] =
store [ ` ${ e . detail . type } - ${ e . detail . namespace } ` ] || [ ] ) ;
eventStore . push ( e . detail ) ;
} ,
} ) ;
} ) ;
2022-09-02 02:00:48 +00:00
} ,
2023-10-10 03:10:04 +00:00
{ calNamespace }
2022-09-02 02:00:48 +00:00
) ;
2023-10-10 03:10:04 +00:00
} ,
async getActionFiredDetails ( { calNamespace , actionType } : { calNamespace : string ; actionType : string } ) {
if ( ! page . isClosed ( ) ) {
return await page . evaluate (
( { actionType , calNamespace } ) = > {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore
return window . eventsFiredStoreForPlaywright [ ` ${ actionType } - ${ calNamespace } ` ] ;
} ,
{ actionType , calNamespace }
) ;
}
} ,
async gotoPlayground ( { calNamespace , url } : { calNamespace : string ; url : string } ) {
await this . addEmbedListeners ( calNamespace ) ;
await page . goto ( url ) ;
} ,
2022-09-02 02:00:48 +00:00
} ;
} ;