1
1
import type { EnhancedPage } from './app' ;
2
2
import { common } from './common' ;
3
3
4
+ type BillingPeriod = 'monthly' | 'annually' ;
5
+
4
6
export const createPricingTablePageObject = ( testArgs : { page : EnhancedPage } ) => {
5
7
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
+
6
51
const self = {
7
52
...common ( testArgs ) ,
8
53
waitForMounted : ( selector = '.cl-pricingTable-root' ) => {
9
54
return page . waitForSelector ( selector , { state : 'attached' } ) ;
10
55
} ,
11
- // clickManageSubscription: async () => {
12
- // await page.getByText('Manage subscription').click();
13
- // },
14
56
clickResubscribe : async ( ) => {
15
57
await page . getByText ( 'Re-subscribe' ) . click ( ) ;
16
58
} ,
17
59
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' } ) ;
22
61
} ,
23
62
getPlanCardCTA : ( { planSlug } : { planSlug : string } ) => {
24
- return page . locator ( `.cl-pricingTableCard__ ${ planSlug } .cl-pricingTableCardFooter` ) . getByRole ( 'button' , {
63
+ return locators . footer ( planSlug ) . getByRole ( 'button' , {
25
64
name : / g e t | s w i t c h | s u b s c r i b e / i,
26
65
} ) ;
27
66
} ,
@@ -32,25 +71,17 @@ export const createPricingTablePageObject = (testArgs: { page: EnhancedPage }) =
32
71
} : {
33
72
planSlug : string ;
34
73
shouldSwitch ?: boolean ;
35
- period ?: 'monthly' | 'annually' ;
74
+ period ?: BillingPeriod ;
36
75
} ) => {
37
76
const targetButtonName =
38
77
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;
39
78
40
79
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 ) ;
50
81
}
51
82
52
- await page
53
- . locator ( `.cl-pricingTableCard__ ${ planSlug } .cl-pricingTableCardFooter` )
83
+ await locators
84
+ . footer ( planSlug )
54
85
. getByRole ( 'button' , {
55
86
name : targetButtonName ,
56
87
} )
0 commit comments