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

const selectors = {
	blockInserterToggle: 'button[aria-label="Toggle block inserter"]',
	blockInserterSearch: 'input[placeholder="Search"]',
	blockInserterResultItem: '.block-editor-block-types-list__list-item',

	// First paragraph in the editor
	firstParagraph: 'p[role="document"].is-selected',

	// Publish
	postButton: 'button:text("Publish")',
};
/**
 * Represents an instance of the WPCOM's Gutenberg editor page.
 */
export class IsolatedBlockEditorComponent {
	private page: Page;

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

	/**
	 * Given a block name, insert a matching block to the editor.
	 *
	 * This method is nearly identical to the method also named `addBlock` in `EditorPage`.
	 * However, the major distinction is the use of `Page` vs `Frame`.
	 *
	 * This is because a P2 frontend (or inline) editor is not a part of an embedded iframe.
	 *
	 * @param {string} blockName Name of the block to be inserted.
	 * @param {string} blockEditorSelector Unique selector to locate the inserted block in the editor.
	 * 	Typically built into the definition file for the block being inserted.
	 * @returns {ElementHandle} Reference to the inserted block in the editor.
	 */
	async addBlock( blockName: string, blockEditorSelector: string ): Promise< ElementHandle > {
		// Click on the editor title. This has the effect of dismissing the block inserter
		// if open, and restores focus back to the editor root container, allowing insertion
		// of blocks.
		await this.page.waitForSelector( selectors.firstParagraph );
		await this.page.click( selectors.blockInserterToggle );
		await this.page.fill( selectors.blockInserterSearch, blockName );
		await this.page.click( `${ selectors.blockInserterResultItem } span:text("${ blockName }")` );
		// Confirm the block has been added to the editor body.
		return await this.page.waitForSelector( `${ blockEditorSelector }.is-selected` );
	}

	/**
	 * Click on the `Post` button to submit the contents of the editor as a new post.
	 */
	async submitPost(): Promise< void > {
		await this.page.click( selectors.postButton );
	}
}