woocommerce/plugins/woocommerce-blocks/tests/utils/visit-block-page.js

121 lines
3.8 KiB
JavaScript
Raw Normal View History

/**
* 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';
/**
* This will visit a GB page or post, and will hide the welcome guide.
*
* @param {string} link the page or post you want to visit.
*/
async function visitPage( link ) {
await page.goto( link );
await page.waitForSelector( '.edit-post-layout' );
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' )
);
await page.reload();
await page.waitForSelector( '.edit-post-layout' );
}
}
/**
* 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.
*
* @param {string} title the page title, written as `BLOCK_NAME block`
*/
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 );
await page.click( '#search-submit' );
await page.waitForNavigation( { waitUntil: 'domcontentloaded' } );
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 visitPage( link );
} else {
await createNewPost( {
postType: 'page',
title,
showWelcomeGuide: false,
} );
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,
} )
);
}
}
Ensure cart changes remain after using back button to get back to the cart (https://github.com/woocommerce/woocommerce-blocks/pull/3960) * Add cartUpdate middleware * Include timestamp for when cart data was generated * Add getCartFromApi action * Check whether cart can be hydrated or needs to be fetched from API * Add cartDataIsStale action and remove getCartFromApi action * Add isCartDataStale selector * Don't load cart until staleness check is complete * Add comment to ease worry about locaStorage execution * Correct doc block and fix typographical error * Cater for lastCartUpdate or the timestamp being undefined * Include @woocommerce/api and @woocommerce/e2e-utils * Add getNormalPagePermalink test utility This will allow us to get the permalink of a "normal" page, i.e. one that is not using the block editor. * Add getBlockPagePermalink test utility This will get the permalink for a page using the Block editor. * Emit action to update cart staleness in all execution paths * Add visitPostOfType function This will allow us to visit the editor page for a post based on its name and post type. * Add functions to get permalinks from editor pages * Add front-end tests for cart * Update package-lock.json * Create local function to ensure the page permalink is visible * Change functions for getting permalinks They were often failing for unknown reasons, these _somehow_ make them more stable. * Add logging for GitHub actions * Add more logging for tests * Add more logging for tests * Add more logging for tests * Wait for networkidle on back * Remove logging except timestamp * Wait for timestamp to be added to localStorage * Split tests to make runs slightly shorter * Wait for add to cart request before continuing test * Fix formatting in cart.test.js * Rename cartDataStale to setIsCartDataStale * Create constant for localStorage timestamp name * Rename cartDataIsStale to isCartDataStale This is for consistency, the action to change this is called setIsCartDataStale - I think this reads better. * Use cleaner logic when determining if the cart should render or fetch * Change docblock for isCartDataStale selector * Remove boolean cast from SET_IS_CART_DATA_STALE reducer * Set longer timeouts for frontend cart tests * Enclose cart staleness checks in condition/prevent unnecessary execution * Remove unnecessary boolean cast from selector * Use constant for local storage key in tests * Use new localstorage key in tests. cant access constants in page context
2021-03-24 13:28:11 +00:00
/**
* This function will attempt to navigate to a page in the WordPress dashboard
*
* @param {string} title The title of the page/post you want to visit.
Ensure cart changes remain after using back button to get back to the cart (https://github.com/woocommerce/woocommerce-blocks/pull/3960) * Add cartUpdate middleware * Include timestamp for when cart data was generated * Add getCartFromApi action * Check whether cart can be hydrated or needs to be fetched from API * Add cartDataIsStale action and remove getCartFromApi action * Add isCartDataStale selector * Don't load cart until staleness check is complete * Add comment to ease worry about locaStorage execution * Correct doc block and fix typographical error * Cater for lastCartUpdate or the timestamp being undefined * Include @woocommerce/api and @woocommerce/e2e-utils * Add getNormalPagePermalink test utility This will allow us to get the permalink of a "normal" page, i.e. one that is not using the block editor. * Add getBlockPagePermalink test utility This will get the permalink for a page using the Block editor. * Emit action to update cart staleness in all execution paths * Add visitPostOfType function This will allow us to visit the editor page for a post based on its name and post type. * Add functions to get permalinks from editor pages * Add front-end tests for cart * Update package-lock.json * Create local function to ensure the page permalink is visible * Change functions for getting permalinks They were often failing for unknown reasons, these _somehow_ make them more stable. * Add logging for GitHub actions * Add more logging for tests * Add more logging for tests * Add more logging for tests * Wait for networkidle on back * Remove logging except timestamp * Wait for timestamp to be added to localStorage * Split tests to make runs slightly shorter * Wait for add to cart request before continuing test * Fix formatting in cart.test.js * Rename cartDataStale to setIsCartDataStale * Create constant for localStorage timestamp name * Rename cartDataIsStale to isCartDataStale This is for consistency, the action to change this is called setIsCartDataStale - I think this reads better. * Use cleaner logic when determining if the cart should render or fetch * Change docblock for isCartDataStale selector * Remove boolean cast from SET_IS_CART_DATA_STALE reducer * Set longer timeouts for frontend cart tests * Enclose cart staleness checks in condition/prevent unnecessary execution * Remove unnecessary boolean cast from selector * Use constant for local storage key in tests * Use new localstorage key in tests. cant access constants in page context
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' );
await page.waitForNavigation( { waitUntil: 'domcontentloaded' } );
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 }` );
}
export default visitBlockPage;