File size: 4,687 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import { Locator } from 'playwright';
import { EditorComponent, EditorSettingsSidebarComponent } from '../../..';
import { BlockFlow, EditorContext, PublishedPostContext } from '.';

interface ConfigurationData {
	phoneNumber: number | string;
	buttonText?: string;
}

const selectors = {
	// Editor
	phoneNumberInput: 'input[placeholder="Your phone number…"]',
	buttonLabel: 'a.whatsapp-block__button',
	editorSettingsButton:
		'.editor-header__settings button[aria-label="Settings"], .edit-post-header__settings button[aria-label="Settings"]',

	// Published
	// 'main' needs to be specified due to the debug elements
	block: 'main div.wp-block-jetpack-whatsapp-button',
};

/**
 * Class representing the flow of using an block in the editor.
 */
export class WhatsAppButtonFlow implements BlockFlow {
	private configurationData: ConfigurationData;
	blockEditorSelector: string;

	/**
	 * Constructs an instance of this block flow with data to be used when configuring and validating the block.
	 *
	 * @param {ConfigurationData} configurationData data with which to configure and validate the block
	 */
	constructor( configurationData: ConfigurationData ) {
		this.configurationData = configurationData;
		this.blockEditorSelector = 'div[aria-label="Block: WhatsApp Button"]';
	}

	blockSidebarName = 'WhatsApp Button';

	/**
	 * Given a Locator, determines whether the target button/toggle is
	 * in an expanded state.
	 *
	 * If the toggle is in the on state or otherwise in an expanded
	 * state, this method will return true. Otherwise, false.
	 *
	 * @param {Locator} target Target button.
	 * @returns {Promise<boolean>} True if target is in an expanded state. False otherwise.
	 */
	private async targetIsOpen( target: Locator ): Promise< boolean > {
		const pressed = await target.getAttribute( 'aria-pressed' );
		const expanded = await target.getAttribute( 'aria-expanded' );
		return pressed === 'true' || expanded === 'true';
	}

	/**
	 * Configure the block in the editor with the configuration data from the constructor
	 *
	 * @param {EditorContext} context The current context for the editor at the point of test execution
	 */
	async configure( context: EditorContext ): Promise< void > {
		const editorCanvas = await context.editorPage.getEditorCanvas();

		// Update the button text if config data is present.
		if ( this.configurationData.buttonText ) {
			const buttonLabelLocator = editorCanvas.locator( selectors.buttonLabel );
			// First clear the text.
			await buttonLabelLocator.fill( '' );
			await buttonLabelLocator.fill( this.configurationData.buttonText );
		}

		const editorParent = await context.editorPage.getEditorParent();

		const editorSettingsButton = editorParent.locator( selectors.editorSettingsButton );

		// This is especially needed in mobile viewport where the settings panel takes up the screen
		// and needs to be open deliberately
		if ( ! ( await this.targetIsOpen( editorSettingsButton ) ) ) {
			await editorSettingsButton.click();
		}

		// We select the button by text content because `.getByRole('button')` has the potential to select multiple
		// elements here. WhatsApp block also has a similarly themed settings button
		const whatsAppSettingsButton = editorParent.locator(
			'button >> text="WhatsApp Button Settings"'
		);

		// Open the block settings dialog.
		// Check if the settings dialog is already open, otherwise clicking closes it, as the button works as a toggle
		if ( ! ( await this.targetIsOpen( whatsAppSettingsButton ) ) ) {
			await whatsAppSettingsButton.click();
		}

		// Enter phone number.
		const phoneInputLocator = editorParent.locator( selectors.phoneNumberInput );
		await phoneInputLocator.fill( this.configurationData.phoneNumber.toString() );

		// Dismiss the block settings dialog.
		await whatsAppSettingsButton.click();

		await phoneInputLocator.waitFor( { state: 'detached' } );

		const editorSettingsSidebarComponent = new EditorSettingsSidebarComponent(
			context.page,
			new EditorComponent( context.page )
		);
		await editorSettingsSidebarComponent.closeSidebarForMobile();
	}

	/**
	 * Validate the block in the published post
	 *
	 * @param {PublishedPostContext} context The current context for the published post at the point of test execution
	 */
	async validateAfterPublish( context: PublishedPostContext ): Promise< void > {
		const blockLocator = context.page.locator( selectors.block );
		await blockLocator.waitFor();

		if ( this.configurationData.buttonText ) {
			const expectedButtonTextLocator = context.page.locator(
				`${ selectors.block } :text("${ this.configurationData.buttonText }")`
			);
			await expectedButtonTextLocator.waitFor();
		}
	}
}