File size: 4,189 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
/**
 * @group gutenberg
 */

import {
	envVariables,
	TestAccount,
	DataHelper,
	EditorPage,
	getTestAccountByFeature,
	envToFeatureKey,
} from '@automattic/calypso-e2e';
import { Page, Browser } from 'playwright';

declare const browser: Browser;

describe( DataHelper.createSuiteTitle( 'Gutenberg: Experimental Features' ), function () {
	const features = envToFeatureKey( envVariables );
	const accountName = getTestAccountByFeature( features );

	let page: Page;
	let editorPage: EditorPage;

	beforeAll( async () => {
		page = await browser.newPage();
		editorPage = new EditorPage( page );

		const testAccount = new TestAccount( accountName );
		await testAccount.authenticate( page );
	} );

	it( 'Go to the new post page', async function () {
		await editorPage.visit( 'post' );
	} );

	it.each( [
		[ 'blockEditor', '__unstableInserterMenuExtension', 'function' ],
		[ 'date', '__experimentalGetSettings', 'function' ],
		[ 'components', '__experimentalNavigationBackButton', 'object' ],
		[ 'editPost', '__experimentalMainDashboardButton', 'function' ],
	] )(
		'Experimental package %s and feature %s are available',
		async function ( packageName, feature, featureType ) {
			const editorParent = await editorPage.getEditorParent();

			const packageAvailable = await editorParent.evaluate(
				`typeof window[ "wp" ]["${ packageName }"]`
			);

			expect( packageAvailable ).not.toStrictEqual( 'undefined' );
			// It should always be an object.
			expect( packageAvailable ).toStrictEqual( 'object' );

			const featureAvailable = await editorParent.evaluate(
				`typeof window[ "wp" ]["${ packageName }"]["${ feature }"]`
			);
			expect( featureAvailable ).not.toStrictEqual( 'undefined' );
			// Feature type changes between object and function and depends on the feature itself.
			expect( featureAvailable ).toContain( featureType );
		}
	);

	it( 'Experimental data is available', async function () {
		const editorParent = await editorPage.getEditorParent();
		const blockPatterns = await editorParent.evaluate(
			"Array.isArray( window.wp.data.select( 'core/editor' ).getEditorSettings().__experimentalBlockPatterns )"
		);
		// If this test fails, please contact #team-ganon to update premium pattern highlighting.
		expect( blockPatterns ).toStrictEqual( true );
	} );

	it( 'Block pattern count should be greater than default', async function () {
		// Regression test for https://github.com/Automattic/wp-calypso/pull/48940.
		// Below are notes from the original Selenium iteration of the spec.

		// At the time I write this, the default block patterns in the test site /
		// theme used to test this (edge and non-edge) amount to 10. When activated,
		// it goes up to >100. Testing if total is > 10 would be too brittle and too
		// close to the default baseline number. 50 seems to be a good threshold,
		// also to avoid potential false-negatives. I assume it's more likely that more
		// patterns will be added than removed. This also means if we see a dramatic
		// change in the number to the lower end, then something is probably wrong.
		const expectedBlockPatternCount = 50;
		const editorParent = await editorPage.getEditorParent();
		const actualBlockPatternCount = await editorParent.evaluate(
			() =>
				/* eslint-disable @typescript-eslint/ban-ts-comment */
				new Promise( ( resolve ) => {
					let hasTimedOut = false;

					// This needs to be done in a loop until patterns request
					// returns anything as initially the data is not there yet,
					// and it returns an empty array, making this case a false
					// positive.
					const wait = setInterval( () => {
						// @ts-ignore
						const patterns = window.wp.data.select( 'core' ).getBlockPatterns();
						if ( patterns.length > 0 || hasTimedOut ) {
							clearInterval( wait );
							resolve( patterns.length );
						}
					}, 100 );

					// Timeout after 10 seconds. No need to wait for the full
					// jest (2 minutes) timeout.
					setTimeout( () => {
						hasTimedOut = true;
					}, 10000 );
				} )
			/* eslint-enable @typescript-eslint/ban-ts-comment */
		);
		expect( actualBlockPatternCount ).toBeGreaterThanOrEqual( expectedBlockPatternCount );
	} );
} );