2020-06-15 14:59:18 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import {
|
|
|
|
createNewPost,
|
|
|
|
visitAdminPage,
|
|
|
|
insertBlock,
|
|
|
|
getEditedPostContent,
|
|
|
|
} from '@wordpress/e2e-test-utils';
|
|
|
|
import { outputFile } from 'fs-extra';
|
|
|
|
import { dirname } from 'path';
|
|
|
|
import kebabCase from 'lodash/kebabCase';
|
|
|
|
|
|
|
|
/**
|
2022-04-08 13:47:19 +00:00
|
|
|
* This will visit a GB page or post, and will hide the welcome guide.
|
2020-06-15 14:59:18 +00:00
|
|
|
*
|
|
|
|
* @param {string} link the page or post you want to visit.
|
|
|
|
*/
|
|
|
|
async function visitPage( link ) {
|
|
|
|
await page.goto( link );
|
2021-11-09 13:18:55 +00:00
|
|
|
await page.waitForSelector( '.edit-post-layout' );
|
2020-06-15 14:59:18 +00:00
|
|
|
const isWelcomeGuideActive = await page.evaluate( () =>
|
|
|
|
wp.data.select( 'core/edit-post' ).isFeatureActive( 'welcomeGuide' )
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( isWelcomeGuideActive ) {
|
|
|
|
await page.evaluate( () =>
|
|
|
|
wp.data.dispatch( 'core/edit-post' ).toggleFeature( 'welcomeGuide' )
|
|
|
|
);
|
2021-11-09 13:18:55 +00:00
|
|
|
await page.reload();
|
|
|
|
await page.waitForSelector( '.edit-post-layout' );
|
2020-06-15 14:59:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function will attempt to search for a page with the `title`
|
|
|
|
* if that block is found, it will open it, if it's not found, it will open
|
|
|
|
* a new page, insert the block, save the page content and title as a fixture file.
|
|
|
|
* In both cases, this page will end up with a page open with the block inserted.
|
2022-04-08 13:47:19 +00:00
|
|
|
*
|
|
|
|
* @param {string} title the page title, written as `BLOCK_NAME block`
|
2020-06-15 14:59:18 +00:00
|
|
|
*/
|
|
|
|
export async function visitBlockPage( title ) {
|
|
|
|
let link = '';
|
|
|
|
// Visit Import Products page.
|
|
|
|
await visitAdminPage( 'edit.php', 'post_type=page' );
|
|
|
|
// If the website has no pages, `#post-search-input` will not render.
|
|
|
|
if ( await page.$( '#post-search-input' ) ) {
|
|
|
|
// search for the page.
|
|
|
|
await page.type( '#post-search-input', title );
|
2022-05-01 11:33:58 +00:00
|
|
|
await Promise.all( [
|
|
|
|
page.waitForNavigation( { waitUntil: 'networkidle0' } ),
|
|
|
|
page.click( '#search-submit' ),
|
|
|
|
] );
|
2020-06-15 14:59:18 +00:00
|
|
|
const pageLink = await page.$x( `//a[contains(text(), '${ title }')]` );
|
2020-06-23 13:22:18 +00:00
|
|
|
if ( pageLink.length > 0 ) {
|
2020-06-15 14:59:18 +00:00
|
|
|
// clicking the link directly caused racing issues, so I used goto.
|
|
|
|
link = await page.evaluate(
|
|
|
|
( a ) => a.getAttribute( 'href' ),
|
|
|
|
pageLink[ 0 ]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( link ) {
|
|
|
|
await visitPage( link );
|
|
|
|
} else {
|
2021-11-09 13:18:55 +00:00
|
|
|
await createNewPost( {
|
|
|
|
postType: 'page',
|
|
|
|
title,
|
|
|
|
showWelcomeGuide: false,
|
|
|
|
} );
|
2020-06-15 14:59:18 +00:00
|
|
|
await insertBlock( title.replace( /block/i, '' ).trim() );
|
|
|
|
const pageContent = await getEditedPostContent();
|
|
|
|
await outputFile(
|
|
|
|
`${ dirname(
|
|
|
|
// we want to fetch the path of the test file who triggered this function
|
|
|
|
// this could be two levels up, or one level up, depending on how you launch the test.
|
|
|
|
module.parent.parent.filename ||
|
|
|
|
module.parent.filename ||
|
|
|
|
module.filename
|
|
|
|
) }/__fixtures__/${ kebabCase(
|
|
|
|
title.replace( /block/i, '' ).trim()
|
|
|
|
) }.fixture.json`,
|
|
|
|
JSON.stringify( {
|
|
|
|
title,
|
|
|
|
pageContent,
|
|
|
|
} )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-24 13:28:11 +00:00
|
|
|
/**
|
|
|
|
* This function will attempt to navigate to a page in the WordPress dashboard
|
|
|
|
*
|
2022-04-08 13:47:19 +00:00
|
|
|
* @param {string} title The title of the page/post you want to visit.
|
2021-03-24 13:28:11 +00:00
|
|
|
* @param {string} postType The post type of the entity you want to visit.
|
|
|
|
*/
|
|
|
|
export async function visitPostOfType( title, postType ) {
|
|
|
|
let link = '';
|
|
|
|
// Visit Import Products page.
|
|
|
|
await visitAdminPage( 'edit.php', `post_type=${ postType }` );
|
|
|
|
// If the website has no pages, `#post-search-input` will not render.
|
|
|
|
if ( await page.$( '#post-search-input' ) ) {
|
|
|
|
// search for the page.
|
|
|
|
await page.type( '#post-search-input', title );
|
|
|
|
await page.click( '#search-submit' );
|
2022-05-01 11:33:58 +00:00
|
|
|
await page.waitForNavigation( { waitUntil: 'networkidle0' } );
|
2021-03-24 13:28:11 +00:00
|
|
|
const pageLink = await page.$x( `//a[contains(text(), '${ title }')]` );
|
|
|
|
if ( pageLink.length > 0 ) {
|
|
|
|
// clicking the link directly caused racing issues, so I used goto.
|
|
|
|
link = await page.evaluate(
|
|
|
|
( a ) => a.getAttribute( 'href' ),
|
|
|
|
pageLink[ 0 ]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( link ) {
|
|
|
|
await page.goto( link );
|
|
|
|
} else throw new Error( `Unable to find page with name ${ title }` );
|
|
|
|
}
|
|
|
|
|
2020-06-15 14:59:18 +00:00
|
|
|
export default visitBlockPage;
|