File size: 2,970 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
import { Page, Locator } from 'playwright';

type Sections =
	| 'Composing'
	| 'Media'
	| 'Content types'
	| 'Feed settings'
	| 'Theme enhancements'
	| 'Widgets'
	| 'Publishing Tools'
	| 'WordPress.com toolbar';
type ToggleLevel = 'parent' | 'child';

const selectors = {
	// Section
	sectionHeader: ( name: Sections ) => `.section-header :text("${ name }")`,

	// Toggles
	parentToggle: ( text: string ) => `span:has(:text("${ text }")) .components-form-toggle`,
	childToggle: ( text: string ) =>
		`div.site-settings__child-settings:has(label:text("${ text }")) .components-form-toggle`,

	// Media section
	carouselBackgroundColorSelect: 'select[id="carousel_background_color"]',
};

/**
 * Represents the Settings > General Settings page.
 */
export class WritingSettingsPage {
	private page: Page;

	/**
	 * Constructs an instance of the component.
	 *
	 * @param {Page} page The underlying page.
	 */
	constructor( page: Page ) {
		this.page = page;
	}

	/**
	 * Validate the presence of a section.
	 */
	async validateSection( name: Sections ): Promise< void > {
		const locator = this.page.locator( selectors.sectionHeader( name ) );
		await locator.waitFor();
	}

	/* Media (Carousel) */

	/**
	 * Toggle the input to on.
	 *
	 * @param {ToggleLevel} level Whether the toggle is a parent or child level.
	 * @param {string} text Exact text on the toggle.
	 */
	async toggleOn( level: 'parent' | 'child', text: string ): Promise< void > {
		let locator: Locator;
		if ( level === 'parent' ) {
			locator = this.page.locator( selectors.parentToggle( text ) );
		} else {
			locator = this.page.locator( selectors.childToggle( text ) );
		}

		if ( await this.isToggled( locator ) ) {
			return;
		}

		await locator.click();
	}

	/**
	 * Toggle the input to off.
	 *
	 * @param {ToggleLevel} level Whether the toggle is a parent or child level.
	 * @param {string} text Exact text on the toggle.
	 */
	async toggleOff( level: ToggleLevel, text: string ): Promise< void > {
		let locator: Locator;
		if ( level === 'parent' ) {
			locator = this.page.locator( selectors.parentToggle( text ) );
		} else {
			locator = this.page.locator( selectors.childToggle( text ) );
		}

		if ( ! ( await this.isToggled( locator ) ) ) {
			return;
		}

		await locator.click();
	}

	/**
	 * Returns whether the toggle at `locator` is toggled
	 * in the On state.
	 *
	 * @returns {Promise<boolean>} True if toggle is on. False otherwise.
	 */
	private async isToggled( locator: Locator ): Promise< boolean > {
		await locator.waitFor();

		const classes = await locator.getAttribute( 'class' );
		return !! classes?.includes( 'is-checked' );
	}

	/**
	 * Selects the carousel background color.
	 *
	 * @param {'Black'|'White'} color Color to select.
	 */
	async selectCarouselBackgroundColor( color: 'Black' | 'White' ): Promise< void > {
		const locator = this.page.locator( selectors.carouselBackgroundColorSelect );
		await locator.selectOption( color.toLowerCase() );
	}
}