File size: 1,981 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, Response } from 'playwright';

const selectors = {
	block: '.wp-block-file',
	fileInput: '.components-form-file-upload input[type="file"]',
	loadingAnimation: '.components-animate__loading.is-transient',
};

/**
 * Represents the File block.
 */
export class FileBlock {
	static blockName = 'File';
	static blockEditorSelector = '[aria-label="Block: File"]';
	private page: Page;
	block: ElementHandle;

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

	/**
	 * Uplaods the target file at the supplied path to WPCOM.
	 *
	 * @param {string} path Path to the file on disk.
	 */
	async upload( path: string ): Promise< void > {
		const input = await this.block.waitForSelector( selectors.fileInput, { state: 'attached' } );
		// Wait for the request complete instead of looking for the spinner and/or loading animation.
		// Waiting on the animation class to be detached is not a reliable method for this block.
		// It can lead to the filename placeholder text not being replaced with the uploaded file name.
		await Promise.all( [
			this.page.waitForResponse(
				( response: Response ) => response.url().includes( 'media?' ) && response.ok()
			),
			input.setInputFiles( path ),
		] );
	}

	/**
	 * Validates block on the page.
	 *
	 * @param {Page} page Page on which to verify the presence of the block.
	 * @param {(string|number)} contents Contents used to validate the block.
	 * @returns {Promise<void>} No return value.
	 */
	static async validatePublishedContent( page: Page, contents: string[] ): Promise< void > {
		await page.waitForSelector( 'a:text("Download")' );
		for await ( const content of contents ) {
			await page.waitForSelector( `a:has-text("${ content }")` );
		}
	}
}