File size: 3,782 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
import { Page, Locator } from 'playwright';
import envVariables from '../../env-variables';
import { EditorComponent } from './editor-component';

const sidebarParentSelector = '.block-editor-inserter__main-area';
const selectors = {
	// This selector was updated to the capitalized label in Gutenberg v19.5.0. Once that is released, we should be able to remove the old selector ("Close block inserter").
	// See: https://github.com/WordPress/gutenberg/pull/65983
	closeBlockInserterButton:
		'button[aria-label="Close Block Inserter"], button[aria-label="Close block inserter"]',
	blockSearchInput: `${ sidebarParentSelector } input[type="search"]`,
	patternExactResultItem: ( name: string ) =>
		`${ sidebarParentSelector } div[aria-label="${ name }"]`,
	patternResultItem: ( name: string ) => `${ sidebarParentSelector } div[aria-label*="${ name }"]`,
};

/**
 * Represents the primary, sidebar block inserter in the editor.
 */
export class EditorSidebarBlockInserterComponent {
	private page: Page;
	private editor: EditorComponent;

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

	/**
	 * Closes the Block Inserter from the panel.
	 *
	 * This operation is only available for Mobile viewports where the
	 * Block Inserter panel is treated as an overlay.
	 */
	async closeBlockInserter(): Promise< void > {
		if ( envVariables.VIEWPORT_NAME !== 'mobile' ) {
			return;
		}

		const editorParent = await this.editor.parent();
		await editorParent.locator( selectors.closeBlockInserterButton ).click();

		await this.page.locator( sidebarParentSelector ).waitFor( { state: 'detached' } );
	}

	/**
	 * Searches the Block Inserter for the provided string.
	 *
	 * @param {string} text Text to enter into the search input.
	 */
	async searchBlockInserter( text: string ): Promise< void > {
		const editorParent = await this.editor.parent();
		const locator = editorParent.locator( selectors.blockSearchInput );
		await locator.fill( text );
	}

	/**
	 * Selects the matching result from the block inserter.
	 *
	 * By default, this method considers only the Block-type results
	 * (including Resuable blocks).
	 * In order to select from Pattern-type results, set the `type`
	 * optional flag in the parameter to `'pattern'`.
	 *
	 * Where multiple matches exist (eg. due to partial matching), the first result will be chosen.
	 */
	async selectBlockInserterResult(
		name: string,
		{
			type = 'block',
			blockFallBackName = '',
			exactMatch = true,
		}: { type?: 'block' | 'pattern'; blockFallBackName?: string; exactMatch?: boolean } = {}
	): Promise< Locator > {
		const editorParent = await this.editor.parent();
		let locator;

		if ( type === 'pattern' ) {
			locator = editorParent
				.locator(
					exactMatch
						? selectors.patternExactResultItem( name )
						: selectors.patternResultItem( name )
				)
				.first();
		} else {
			const optionName = blockFallBackName
				? new RegExp( `(${ name }|${ blockFallBackName })` )
				: name;
			locator = editorParent
				// The DOM structure that hold the block options changes a LOT dependent on whether there's a search.
				// This combined selector is not the slickest, but capture both cases.
				// There's not an easy way to use "getByRole" to capture two cases without a lot of promise racing.
				.locator( '.block-editor-inserter__block-list,.block-editor-block-types-list' )
				.getByRole( 'option', {
					name: optionName,
					exact: true,
				} )
				.first();
		}

		await Promise.all( [ locator.hover(), locator.focus() ] );
		await locator.click();

		return locator;
	}
}