11import type { EnhancedPage } from './app' ;
22import { common } from './common' ;
33
4+ type BillingPeriod = 'monthly' | 'annually' ;
5+
46export const createPricingTablePageObject = ( testArgs : { page : EnhancedPage } ) => {
57 const { page } = testArgs ;
8+
9+ const locators = {
10+ toggle : ( planSlug : string ) => page . locator ( `.cl-pricingTableCard__${ planSlug } .cl-pricingTableCardPeriodToggle` ) ,
11+ indicator : ( planSlug : string ) => page . locator ( `.cl-pricingTableCard__${ planSlug } .cl-switchIndicator` ) ,
12+ badge : ( planSlug : string ) => page . locator ( `.cl-pricingTableCard__${ planSlug } .cl-badge` ) ,
13+ footer : ( planSlug : string ) => page . locator ( `.cl-pricingTableCard__${ planSlug } .cl-pricingTableCardFooter` ) ,
14+ } ;
15+
16+ const ensurePricingPeriod = async ( planSlug : string , period : BillingPeriod ) : Promise < void > => {
17+ async function waitForAttribute ( selector : string , attribute : string , value : string , timeout = 5000 ) {
18+ return page
19+ . waitForFunction (
20+ ( { sel, attr, val } ) => {
21+ const element = document . querySelector ( sel ) ;
22+ return element ?. getAttribute ( attr ) === val ;
23+ } ,
24+ { sel : selector , attr : attribute , val : value } ,
25+ { timeout } ,
26+ )
27+ . then ( ( ) => {
28+ return true ;
29+ } )
30+ . catch ( ( ) => {
31+ return false ;
32+ } ) ;
33+ }
34+
35+ const isAnnually = await waitForAttribute (
36+ `.cl-pricingTableCard__${ planSlug } .cl-switchIndicator` ,
37+ 'data-checked' ,
38+ 'true' ,
39+ 500 ,
40+ ) ;
41+
42+ if ( isAnnually && period === 'monthly' ) {
43+ await locators . toggle ( planSlug ) . click ( ) ;
44+ }
45+
46+ if ( ! isAnnually && period === 'annually' ) {
47+ await locators . toggle ( planSlug ) . click ( ) ;
48+ }
49+ } ;
50+
651 const self = {
752 ...common ( testArgs ) ,
853 waitForMounted : ( selector = '.cl-pricingTable-root' ) => {
954 return page . waitForSelector ( selector , { state : 'attached' } ) ;
1055 } ,
11- // clickManageSubscription: async () => {
12- // await page.getByText('Manage subscription').click();
13- // },
1456 clickResubscribe : async ( ) => {
1557 await page . getByText ( 'Re-subscribe' ) . click ( ) ;
1658 } ,
1759 waitToBeActive : async ( { planSlug } : { planSlug : string } ) => {
18- return page
19- . locator ( `.cl-pricingTableCard__${ planSlug } .cl-badge` )
20- . getByText ( 'Active' )
21- . waitFor ( { state : 'visible' } ) ;
60+ return locators . badge ( planSlug ) . getByText ( 'Active' ) . waitFor ( { state : 'visible' } ) ;
2261 } ,
2362 getPlanCardCTA : ( { planSlug } : { planSlug : string } ) => {
24- return page . locator ( `.cl-pricingTableCard__ ${ planSlug } .cl-pricingTableCardFooter` ) . getByRole ( 'button' , {
63+ return locators . footer ( planSlug ) . getByRole ( 'button' , {
2564 name : / g e t | s w i t c h | s u b s c r i b e / i,
2665 } ) ;
2766 } ,
@@ -32,25 +71,17 @@ export const createPricingTablePageObject = (testArgs: { page: EnhancedPage }) =
3271 } : {
3372 planSlug : string ;
3473 shouldSwitch ?: boolean ;
35- period ?: 'monthly' | 'annually' ;
74+ period ?: BillingPeriod ;
3675 } ) => {
3776 const targetButtonName =
3877 shouldSwitch === true ? 'Switch to this plan' : shouldSwitch === false ? / s u b s c r i b e / i : / g e t | s w i t c h | s u b s c r i b e / i;
3978
4079 if ( period ) {
41- await page . locator ( `.cl-pricingTableCard__${ planSlug } .cl-pricingTableCardPeriodToggle` ) . click ( ) ;
42-
43- const billedAnnuallyChecked = await page
44- . locator ( `.cl-pricingTableCard__${ planSlug } .cl-switchIndicator` )
45- . getAttribute ( 'data-checked' ) ;
46-
47- if ( billedAnnuallyChecked === 'true' && period === 'monthly' ) {
48- await page . locator ( `.cl-pricingTableCard__${ planSlug } .cl-pricingTableCardPeriodToggle` ) . click ( ) ;
49- }
80+ await ensurePricingPeriod ( planSlug , period ) ;
5081 }
5182
52- await page
53- . locator ( `.cl-pricingTableCard__ ${ planSlug } .cl-pricingTableCardFooter` )
83+ await locators
84+ . footer ( planSlug )
5485 . getByRole ( 'button' , {
5586 name : targetButtonName ,
5687 } )
0 commit comments