File size: 2,507 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import { Page } from 'playwright';

type CancelReason = 'Another reason…';

/**
 * Cancels a purchased subscription.
 */
export async function cancelSubscriptionFlow( page: Page ) {
	await page.getByRole( 'button', { name: 'Submit' } ).click();
}

/**
 * Cancels a purchased plan.
 */
export async function cancelPurchaseFlow(
	page: Page,
	feedback: {
		reason: CancelReason;
		customReasonText: string;
	}
) {
	await page
		.getByRole( 'combobox', { name: 'Why would you like to cancel?' } )
		.selectOption( feedback.reason );

	await page
		.getByRole( 'textbox', { name: 'Can you please specify?' } )
		.fill( feedback.customReasonText );

	await page.getByRole( 'button', { name: 'Submit' } ).click();

	await Promise.all( [
		page.waitForNavigation( { timeout: 30 * 1000 } ),
		page.getByRole( 'button', { name: 'Submit' } ).click(),
	] );
}

/**
 * Cancels a purchased Atomic site.
 */
export async function cancelAtomicPurchaseFlow(
	page: Page,
	feedback: {
		reason: CancelReason;
		customReasonText: string;
	}
) {
	await page
		.getByRole( 'combobox', { name: 'Why would you like to cancel?' } )
		.selectOption( feedback.reason );

	await page
		.getByRole( 'textbox', { name: 'Can you please specify?' } )
		.fill( feedback.customReasonText );

	// Submit first step - could be "Submit" or "Continue"
	const firstButton = page
		.getByRole( 'button', { name: 'Submit' } )
		.or( page.getByRole( 'button', { name: 'Continue' } ) );
	await firstButton.waitFor( { state: 'visible' } );
	await firstButton.click();

	// Select dropdown value to enable the next button
	await page
		.getByRole( 'combobox', { name: 'Where is your next adventure taking you?' } )
		.selectOption( "I'm staying here and using the free plan." );

	// Submit second step - could be "Submit" or "Continue"
	const secondButton = page
		.getByRole( 'button', { name: 'Submit' } )
		.or( page.getByRole( 'button', { name: 'Continue' } ) );
	await secondButton.waitFor( { state: 'visible' } );
	await secondButton.click();

	const finalButton = page
		.getByRole( 'button', { name: 'Submit' } )
		.or( page.getByRole( 'button', { name: 'Continue' } ) );

	await Promise.all( [
		page.waitForNavigation( { timeout: 30 * 1000 } ),
		finalButton.waitFor( { state: 'visible' } ),
		// Wait for button to be enabled
		page.waitForFunction(
			() => {
				const button = document.querySelector( 'button[class*="is-primary"]' );
				return button && ! button.hasAttribute( 'disabled' );
			},
			{ timeout: 10000 }
		),
	] );
}