File size: 2,557 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
import { Page, Locator } from 'playwright';
import { EditorComponent } from '../components';

const coverStylesArray = [ 'Default', 'Bottom Wave', 'Top Wave' ] as const;
export type coverStyles = ( typeof coverStylesArray )[ number ];

/**
 * Represents the Cover block.
 */
export class CoverBlock {
	static blockName = 'Cover';
	static blockEditorSelector = '[aria-label="Block: Cover"]';
	static coverStyles = coverStylesArray;
	private editor: EditorComponent;
	block: Locator;

	/**
	 * Constructs an instance of this block.
	 *
	 * @param {Page} page The underlying page object.
	 * @param {Locator} block Handle referencing the block as inserted on the Gutenberg editor.
	 */
	constructor( page: Page, block: Locator ) {
		this.block = block;
		this.editor = new EditorComponent( page );
	}

	/**
	 * Uploads an image file to the Cover block.
	 *
	 * @param {string} path Path to the file on disk.
	 */
	async upload( path: string ): Promise< void > {
		await this.block.locator( 'input[type="file"]' ).setInputFiles( path );
		await this.block.locator( 'img[src^="blob:"]' ).waitFor( {
			timeout: 20 * 1000,
			state: 'detached',
		} );

		// After uploading the image the focus is switched to the inner
		// paragraph block (Cover title), so we need to switch it back outside.
		const editorParent = await this.editor.parent();
		await editorParent
			.locator( CoverBlock.blockEditorSelector )
			.click( { position: { x: 1, y: 1 } } );
	}

	/**
	 * Adds the title text over the block's image.
	 *
	 * @param text The title text.
	 */
	async addTitle( text: string ): Promise< void > {
		await this.block.locator( 'p' ).fill( text );
	}

	/**
	 * Activates the tab for the Cover block.
	 *
	 * @param {'Settings'|'Styles'} name Supported tabs.
	 */
	async activateTab( name: 'Settings' | 'Styles' ) {
		const editorParent = await this.editor.parent();
		await editorParent.locator( `[aria-label="${ name }"]` ).click();
	}

	/**
	 * Sets given Cover style. Requires Setting panel to be open.
	 *
	 * @param {coverStyles} style The title of one of the Cover style buttons
	 */
	async setCoverStyle( style: coverStyles ): Promise< void > {
		const editorParent = await this.editor.parent();
		await editorParent.locator( `button[aria-label="${ style }"]` ).click();

		const blockId = await this.block.getAttribute( 'data-block' );
		const styleSelector = `.is-style-${ style.toLowerCase().replace( ' ', '-' ) }`;
		const blockSelector = `[data-block="${ blockId }"]`;

		await editorParent.locator( blockSelector + styleSelector ).waitFor();
	}
}