2020-10-27 12:24:53 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2022-04-01 23:09:54 +00:00
|
|
|
import { Coupon, HTTPClientFactory } from '@woocommerce/api';
|
2022-02-21 11:01:56 +00:00
|
|
|
import config from 'config';
|
2021-07-26 14:05:09 +00:00
|
|
|
import {
|
2023-04-03 13:02:36 +00:00
|
|
|
canvas,
|
2022-02-21 10:01:42 +00:00
|
|
|
disableSiteEditorWelcomeGuide,
|
2023-04-03 13:02:36 +00:00
|
|
|
ensureSidebarOpened,
|
2021-07-26 14:05:09 +00:00
|
|
|
openGlobalBlockInserter,
|
2023-04-03 13:02:36 +00:00
|
|
|
switchBlockInspectorTab,
|
2022-03-02 14:49:00 +00:00
|
|
|
switchUserToAdmin,
|
2022-02-21 10:01:42 +00:00
|
|
|
visitAdminPage,
|
2022-04-01 09:23:48 +00:00
|
|
|
pressKeyWithModifier,
|
|
|
|
searchForBlock as searchForFSEBlock,
|
2023-01-25 15:41:22 +00:00
|
|
|
enterEditMode,
|
2023-04-03 13:02:36 +00:00
|
|
|
getAllBlocks,
|
2021-07-26 14:05:09 +00:00
|
|
|
} from '@wordpress/e2e-test-utils';
|
2022-03-23 13:18:00 +00:00
|
|
|
import { addQueryArgs } from '@wordpress/url';
|
2021-10-18 06:48:03 +00:00
|
|
|
import { WP_ADMIN_DASHBOARD } from '@woocommerce/e2e-utils';
|
2022-04-04 12:30:07 +00:00
|
|
|
import fs from 'fs';
|
2021-07-26 14:05:09 +00:00
|
|
|
|
2022-02-21 10:01:42 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2022-07-18 09:38:49 +00:00
|
|
|
import { elementExists, getTextContent } from './page-utils';
|
2022-04-04 12:30:07 +00:00
|
|
|
import { PERFORMANCE_REPORT_FILENAME } from '../utils/constants';
|
2022-07-18 09:38:49 +00:00
|
|
|
import { cli } from '../utils';
|
2022-02-21 10:01:42 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef {import('@types/puppeteer').ElementHandle} ElementHandle
|
|
|
|
* @typedef {import('@wordpress/blocks').Block} WPBlock
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef {{ addedBy: string, hasActions: boolean, templateTitle: string }} TemplateTableItem
|
|
|
|
*/
|
|
|
|
|
2022-02-21 11:01:56 +00:00
|
|
|
export const BASE_URL = config.get( 'url' );
|
2022-04-01 23:09:54 +00:00
|
|
|
export const adminUsername = config.get( 'users.admin.username' );
|
|
|
|
export const adminPassword = config.get( 'users.admin.password' );
|
|
|
|
export const client = HTTPClientFactory.build( BASE_URL )
|
|
|
|
.withBasicAuth( adminUsername, adminPassword )
|
|
|
|
.create();
|
2022-02-21 10:01:42 +00:00
|
|
|
export const DEFAULT_TIMEOUT = 30000;
|
2022-10-10 10:42:59 +00:00
|
|
|
export const SHOP_CHECKOUT_BLOCK_PAGE = BASE_URL + 'checkout-block/';
|
2022-02-21 10:01:42 +00:00
|
|
|
|
|
|
|
const SELECTORS = {
|
2022-11-09 01:53:26 +00:00
|
|
|
canvas: 'iframe[name="editor-canvas"],.edit-post-visual-editor',
|
2022-02-21 10:01:42 +00:00
|
|
|
inserter: {
|
2022-06-15 09:56:52 +00:00
|
|
|
search: '.components-search-control__input,.block-editor-inserter__search input,.block-editor-inserter__search-input,input.block-editor-inserter__search',
|
2022-02-21 10:01:42 +00:00
|
|
|
},
|
|
|
|
templatesListTable: {
|
|
|
|
actionsContainer: '.edit-site-list-table__actions',
|
|
|
|
cells: '.edit-site-list-table-column',
|
|
|
|
headings: 'thead th.edit-site-list-table-column',
|
|
|
|
root: '.edit-site-list-table',
|
|
|
|
rows: '.edit-site-list-table-row',
|
2022-03-28 12:32:15 +00:00
|
|
|
templateActions: '.edit-site-list-table button[aria-label="Actions"]',
|
2022-02-21 10:01:42 +00:00
|
|
|
templateTitle: '[data-wp-component="Heading"]',
|
|
|
|
},
|
2022-03-02 14:49:00 +00:00
|
|
|
themesPage: {
|
|
|
|
currentTheme: '.theme.active',
|
|
|
|
},
|
2022-02-21 10:01:42 +00:00
|
|
|
toolbar: {
|
|
|
|
confirmSave: '.editor-entities-saved-states__save-button',
|
|
|
|
saveButton: '.edit-site-save-button__button',
|
|
|
|
savePrompt: '.entities-saved-states__text-prompt',
|
|
|
|
},
|
2022-12-30 11:47:33 +00:00
|
|
|
templateEditor: {
|
|
|
|
editButton:
|
2023-01-09 16:44:53 +00:00
|
|
|
'.edit-site-site-hub__edit-button[aria-label="Open the editor"]',
|
2022-12-30 11:47:33 +00:00
|
|
|
},
|
2023-04-03 13:02:36 +00:00
|
|
|
editor: {
|
|
|
|
filterButtonToggle: '//label[text()="Show \'Apply filters\' button"]',
|
|
|
|
},
|
|
|
|
frontend: {
|
|
|
|
XPathSubmitButton:
|
|
|
|
"//*[contains(@class,'wc-block-components-filter-submit-button')]",
|
|
|
|
},
|
2022-02-21 10:01:42 +00:00
|
|
|
};
|
2021-07-26 14:05:09 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Search for block in the global inserter.
|
|
|
|
*
|
|
|
|
* @see https://github.com/WordPress/gutenberg/blob/2356b2d3165acd0af980d52bc93fb1e42748bb25/packages/e2e-test-utils/src/inserter.js#L95
|
|
|
|
*
|
|
|
|
* @param {string} searchTerm The text to search the inserter for.
|
|
|
|
*/
|
2021-10-18 06:48:03 +00:00
|
|
|
export async function searchForBlock( searchTerm ) {
|
2023-02-20 09:10:41 +00:00
|
|
|
await openGlobalBlockInserter();
|
2022-02-21 10:01:42 +00:00
|
|
|
await page.waitForSelector( SELECTORS.inserter.search );
|
|
|
|
await page.focus( SELECTORS.inserter.search );
|
2021-07-26 14:05:09 +00:00
|
|
|
await pressKeyWithModifier( 'primary', 'a' );
|
|
|
|
await page.keyboard.type( searchTerm );
|
|
|
|
}
|
2020-10-27 12:24:53 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Opens the inserter, searches for the given term, then selects the first
|
|
|
|
* result that appears.
|
|
|
|
*
|
|
|
|
* @param {string} searchTerm The text to search the inserter for.
|
|
|
|
*/
|
|
|
|
export async function insertBlockDontWaitForInsertClose( searchTerm ) {
|
|
|
|
await searchForBlock( searchTerm );
|
2022-12-30 11:47:33 +00:00
|
|
|
await page.waitForXPath( `//button//span[text()='${ searchTerm }']` );
|
2020-10-27 12:24:53 +00:00
|
|
|
const insertButton = (
|
2021-10-18 06:48:03 +00:00
|
|
|
await page.$x( `//button//span[text()='${ searchTerm }']` )
|
2020-10-27 12:24:53 +00:00
|
|
|
)[ 0 ];
|
|
|
|
await insertButton.click();
|
|
|
|
}
|
|
|
|
|
|
|
|
export const closeInserter = async () => {
|
2021-04-16 10:48:27 +00:00
|
|
|
if (
|
|
|
|
await page.evaluate( () => {
|
|
|
|
return !! document.querySelector(
|
|
|
|
'.edit-post-header [aria-label="Add block"]'
|
|
|
|
);
|
|
|
|
} )
|
|
|
|
) {
|
|
|
|
await page.click( '.edit-post-header [aria-label="Add block"]' );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
await page.click(
|
|
|
|
'.edit-post-header [aria-label="Toggle block inserter"]'
|
|
|
|
);
|
2020-10-27 12:24:53 +00:00
|
|
|
};
|
2021-10-18 06:48:03 +00:00
|
|
|
|
|
|
|
const WP_ADMIN_WIDGETS_EDITOR = WP_ADMIN_DASHBOARD + 'widgets.php';
|
|
|
|
|
|
|
|
export const openWidgetEditor = async () => {
|
|
|
|
await page.goto( WP_ADMIN_WIDGETS_EDITOR, {
|
|
|
|
waitUntil: 'networkidle0',
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
|
|
|
export const closeModalIfExists = async () => {
|
2023-02-17 11:49:38 +00:00
|
|
|
// The modal close button can have different aria-labels, depending on the version of Gutenberg/WP.
|
|
|
|
// Newer versions (WP >=6.2) use `Close`, while older versions (WP <6.1) use `Close dialog`.
|
|
|
|
const closeButton = await page.$(
|
|
|
|
'.components-modal__header [aria-label="Close"], .components-modal__header [aria-label="Close dialog"]'
|
|
|
|
);
|
|
|
|
if ( closeButton ) {
|
|
|
|
await closeButton.click();
|
2021-10-18 06:48:03 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-02-07 10:29:22 +00:00
|
|
|
export const isBlockInsertedInWidgetsArea = async ( blockName ) => {
|
|
|
|
const widgetAreaSelector = '.wp-block-widget-area';
|
|
|
|
|
|
|
|
const widgetsArea = await page.$$( widgetAreaSelector );
|
|
|
|
|
|
|
|
return widgetsArea.some(
|
|
|
|
async ( widgetArea ) =>
|
|
|
|
( await widgetArea.$$( `[data-block-title="${ blockName }"]` )
|
|
|
|
.length ) > 0
|
|
|
|
);
|
|
|
|
};
|
2022-03-08 14:50:16 +00:00
|
|
|
|
2022-02-21 10:01:42 +00:00
|
|
|
/**
|
2023-04-03 13:02:36 +00:00
|
|
|
* Visits site editor depending on used WordPress version and how Gutenberg is installed.
|
2022-02-21 10:01:42 +00:00
|
|
|
*
|
2022-04-08 13:47:19 +00:00
|
|
|
* @param {Object} params Query parameters to add to the URL.
|
|
|
|
* @param {string} [params.postId] ID of the template if we want to access template editor.
|
2022-03-28 12:32:15 +00:00
|
|
|
* @param {'wp_template' | 'wp_template_part'} [params.postType='wp_template'] Type of template.
|
2023-04-03 13:02:36 +00:00
|
|
|
* @param {string} [params.path] Navigation path.
|
2022-02-21 10:01:42 +00:00
|
|
|
*/
|
2022-08-03 20:22:37 +00:00
|
|
|
export async function goToSiteEditor( params = {} ) {
|
2022-12-30 11:47:33 +00:00
|
|
|
await visitAdminPage( 'site-editor.php', addQueryArgs( '', params ) );
|
|
|
|
|
2023-04-03 13:02:36 +00:00
|
|
|
if ( params?.postId || Object.keys( params ).length === 0 ) {
|
2023-01-25 15:41:22 +00:00
|
|
|
await enterEditMode();
|
2022-12-30 11:47:33 +00:00
|
|
|
}
|
2022-03-28 12:32:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Visits the Site Editor template edit view.
|
|
|
|
*
|
2022-04-08 13:47:19 +00:00
|
|
|
* @param {Object} params
|
2022-08-03 20:22:37 +00:00
|
|
|
* @param {string} [params.postId] ID of the template if we want to access template editor.
|
2022-03-28 12:32:15 +00:00
|
|
|
* @param {'wp_template' | 'wp_template_part'} [params.postType='wp_template'] Type of template.
|
|
|
|
*/
|
|
|
|
export async function goToTemplateEditor( {
|
|
|
|
postId,
|
|
|
|
postType = 'wp_template',
|
|
|
|
} = {} ) {
|
2022-08-03 20:22:37 +00:00
|
|
|
await goToSiteEditor( {
|
2022-03-28 12:32:15 +00:00
|
|
|
postType,
|
|
|
|
postId,
|
|
|
|
} );
|
|
|
|
|
2022-03-23 13:18:00 +00:00
|
|
|
await disableSiteEditorWelcomeGuide();
|
2022-03-28 12:32:15 +00:00
|
|
|
await waitForCanvas();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Visits the Site Editor templates list view.
|
|
|
|
*
|
2022-04-08 13:47:19 +00:00
|
|
|
* @param {Object} params
|
2022-03-28 12:32:15 +00:00
|
|
|
* @param {'wp_template' | 'wp_template_part'} [params.postType='wp_template'] Type of template.
|
2022-04-08 13:47:19 +00:00
|
|
|
* @param {'list' | 'actions'} [params.waitFor='false'] Wait for list or for actions to be present - tempalte actions can take a moment to load, we can wait for them to be present if needed.
|
2022-03-28 12:32:15 +00:00
|
|
|
*/
|
|
|
|
export async function goToTemplatesList( {
|
|
|
|
postType = 'wp_template',
|
|
|
|
waitFor = 'list',
|
|
|
|
} = {} ) {
|
2023-04-03 13:02:36 +00:00
|
|
|
await goToSiteEditor( {
|
|
|
|
postType,
|
|
|
|
// In WP 6.2, if postId is not defined, the route expects `path` instead
|
|
|
|
// of `postType`.
|
|
|
|
path: `/${ postType }/all`,
|
|
|
|
} );
|
2022-03-28 12:32:15 +00:00
|
|
|
|
|
|
|
if ( waitFor === 'actions' ) {
|
|
|
|
await page.waitForSelector(
|
|
|
|
SELECTORS.templatesListTable.templateActions
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
await page.waitForSelector( SELECTORS.templatesListTable.root );
|
|
|
|
}
|
2022-02-21 10:01:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Waits for the Gutenberg canvas to be available
|
|
|
|
*
|
|
|
|
* @param {number} [timeout=DEFAULT_TIMEOUT] The amount of ms to wait for the element
|
|
|
|
*
|
|
|
|
* @return {Promise<?ElementHandle>} The canvas element handle
|
|
|
|
*/
|
|
|
|
export function waitForCanvas( timeout = DEFAULT_TIMEOUT ) {
|
|
|
|
return page.waitForSelector( SELECTORS.canvas, { timeout } );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Saves a template
|
|
|
|
*/
|
|
|
|
export async function saveTemplate() {
|
|
|
|
const { confirmSave, saveButton, savePrompt } = SELECTORS.toolbar;
|
|
|
|
|
|
|
|
await page.click( saveButton );
|
|
|
|
await page.waitForSelector( savePrompt );
|
|
|
|
await page.click( confirmSave );
|
|
|
|
await page.waitForSelector( `${ saveButton }[aria-disabled="true"]` );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets all available templates from the template list table UI
|
|
|
|
*
|
|
|
|
* @return {Promise<TemplateTableItem[]>} A promise of an array of informations about the templates extracted from the UI
|
|
|
|
*/
|
|
|
|
export async function getAllTemplates() {
|
|
|
|
const { templatesListTable } = SELECTORS;
|
|
|
|
|
2022-03-28 12:32:15 +00:00
|
|
|
const table = await page.waitForSelector( templatesListTable.root );
|
2022-02-21 10:01:42 +00:00
|
|
|
|
|
|
|
if ( ! table ) throw new Error( 'Templates table not found' );
|
|
|
|
|
|
|
|
const rows = await table.$$( templatesListTable.rows );
|
|
|
|
|
|
|
|
return Promise.all(
|
|
|
|
rows.map( async ( row ) => ( {
|
|
|
|
addedBy: (
|
|
|
|
await getTextContent( templatesListTable.cells, row )
|
|
|
|
)[ 1 ],
|
|
|
|
hasActions: await elementExists(
|
|
|
|
templatesListTable.actionsContainer,
|
|
|
|
row
|
|
|
|
),
|
|
|
|
templateTitle: (
|
|
|
|
await getTextContent( templatesListTable.templateTitle, row )
|
|
|
|
)[ 0 ],
|
|
|
|
} ) )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets all the blocks that fulfill a given predicate
|
|
|
|
*
|
|
|
|
* @param {( block: WPBlock ) => boolean} predicate The function invoked per iteration
|
|
|
|
* @return {Promise< Partial< WPBlock >[] >} The blocks which have been found
|
|
|
|
*/
|
|
|
|
export async function filterCurrentBlocks( predicate ) {
|
|
|
|
/**
|
|
|
|
* @type {WPBlock[]}
|
|
|
|
*/
|
|
|
|
const blocks = await page.evaluate( () => {
|
|
|
|
/**
|
|
|
|
* Gets all serializeable data from a block
|
|
|
|
*
|
|
|
|
* @param {WPBlock} block A Gutenberg Block
|
|
|
|
* @return {Partial<WPBlock>} A block with unserializeable values turned to `null`
|
|
|
|
*/
|
|
|
|
function getSerializeableBlockData( block ) {
|
|
|
|
return JSON.parse( JSON.stringify( block ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
const blockEditorStore = window.wp.data.select( 'core/block-editor' );
|
|
|
|
/**
|
|
|
|
* @type {string[]}
|
|
|
|
*/
|
|
|
|
const allClientIds = blockEditorStore.getClientIdsWithDescendants();
|
|
|
|
|
|
|
|
return allClientIds.map( ( id ) =>
|
|
|
|
getSerializeableBlockData( blockEditorStore.getBlock( id ) )
|
|
|
|
);
|
|
|
|
} );
|
|
|
|
|
|
|
|
return blocks.filter( predicate );
|
|
|
|
}
|
2022-03-02 14:49:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets up a suite to use a theme without side-effects
|
|
|
|
*
|
|
|
|
* *Note:* this “hook” is supposed to be used within `describe` calls to
|
|
|
|
* make it easier to set theme dependencies for tests without messing
|
|
|
|
* the environment of any other test and to explicitly declare a
|
|
|
|
* dependency.
|
|
|
|
*
|
|
|
|
* @param {string} themeSlug The theme the test suite should use
|
|
|
|
*/
|
|
|
|
export function useTheme( themeSlug ) {
|
|
|
|
beforeAll( async () => {
|
2022-07-18 09:38:49 +00:00
|
|
|
await cli(
|
|
|
|
`npm run wp-env run tests-cli wp theme activate ${ themeSlug }`
|
2022-03-02 14:49:00 +00:00
|
|
|
);
|
2022-07-18 09:38:49 +00:00
|
|
|
await switchUserToAdmin();
|
2022-03-02 14:49:00 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
afterAll( async () => {
|
2022-07-18 09:38:49 +00:00
|
|
|
await cli(
|
|
|
|
`npm run wp-env run tests-cli wp theme activate storefront`
|
|
|
|
);
|
|
|
|
await switchUserToAdmin();
|
2022-03-02 14:49:00 +00:00
|
|
|
} );
|
|
|
|
}
|
2022-04-01 09:23:48 +00:00
|
|
|
|
|
|
|
/**
|
2022-04-04 12:30:07 +00:00
|
|
|
* Takes an average value of all items in an array.
|
|
|
|
*
|
|
|
|
* @param {Array} array An array of numbers to take an average from.
|
|
|
|
* @return {number} The average value of all members of the array.
|
|
|
|
*/
|
|
|
|
const average = ( array ) => array.reduce( ( a, b ) => a + b ) / array.length;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Writes a line to the e2e performance result for the current test containing longest, shortest, and average run times.
|
|
|
|
*
|
|
|
|
* @param {string} description Message to describe what you're logging the performance of.
|
2022-04-08 13:47:19 +00:00
|
|
|
* @param {Array} times array of times to record.
|
2022-04-04 12:30:07 +00:00
|
|
|
*/
|
|
|
|
export const logPerformanceResult = ( description, times ) => {
|
|
|
|
const roundedTimes = times.map(
|
|
|
|
( time ) => Math.round( time + Number.EPSILON * 100 ) / 100
|
|
|
|
);
|
|
|
|
fs.appendFileSync(
|
|
|
|
PERFORMANCE_REPORT_FILENAME,
|
|
|
|
JSON.stringify( {
|
|
|
|
description,
|
|
|
|
longest: Math.max( ...roundedTimes ),
|
|
|
|
shortest: Math.min( ...roundedTimes ),
|
|
|
|
average: average( roundedTimes ),
|
|
|
|
} ) + '\n'
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Add a block to Full Site Editing.
|
2022-04-01 09:23:48 +00:00
|
|
|
*
|
|
|
|
* *Note:* insertBlock function gets focused on the canvas, this could prevent some dialogs from being displayed. e.g. compatibility notice.
|
|
|
|
*
|
|
|
|
* @param {string} blockName Block name.
|
|
|
|
*/
|
|
|
|
export const addBlockToFSEArea = async ( blockName ) => {
|
|
|
|
await searchForFSEBlock( blockName );
|
|
|
|
const insertButton = await page.waitForXPath(
|
|
|
|
`//button//span[contains(text(), '${ blockName }')]`
|
|
|
|
);
|
|
|
|
await insertButton.click();
|
|
|
|
};
|
2022-04-01 23:09:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a basic coupon with the provided coupon amount. Returns the coupon code.
|
|
|
|
*
|
2022-04-08 13:47:19 +00:00
|
|
|
* @param {Object} [coupon] Coupon object. Default to fixed cart type and amount = 5.
|
|
|
|
* @param {string} [coupon.amount] Amount to be applied. Defaults to 5.
|
2022-04-01 23:09:54 +00:00
|
|
|
* @param {string} [coupon.discountType] Type of a coupon. Defaults to Fixed cart discount.
|
2022-04-08 13:47:19 +00:00
|
|
|
* @param {number} [coupon.usageLimit] How many times the coupon can be used in total. Defaults to -1.
|
2022-04-01 23:09:54 +00:00
|
|
|
*/
|
|
|
|
export const createCoupon = async ( coupon ) => {
|
|
|
|
const {
|
|
|
|
amount = '5',
|
|
|
|
discountType = 'Fixed cart discount',
|
|
|
|
usageLimit = -1,
|
|
|
|
} = coupon || { amount: '5', discountType: 'Fixed cart discount' };
|
|
|
|
|
|
|
|
let couponType;
|
|
|
|
switch ( discountType ) {
|
|
|
|
case 'Fixed cart discount':
|
|
|
|
couponType = 'fixed_cart';
|
|
|
|
break;
|
|
|
|
case 'Fixed product discount':
|
|
|
|
couponType = 'fixed_product';
|
|
|
|
break;
|
|
|
|
case 'Percentage discount':
|
|
|
|
couponType = 'percent';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
couponType = discountType;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fill in coupon code
|
|
|
|
const couponCode = 'code-' + couponType + new Date().getTime().toString();
|
|
|
|
const repository = Coupon.restRepository( client );
|
|
|
|
const createdCoupon = await repository.create( {
|
|
|
|
code: couponCode,
|
|
|
|
discountType: couponType,
|
|
|
|
amount,
|
|
|
|
usageLimit,
|
|
|
|
} );
|
|
|
|
|
|
|
|
return createdCoupon;
|
|
|
|
};
|
2022-05-12 15:15:53 +00:00
|
|
|
|
2022-05-13 13:18:51 +00:00
|
|
|
/**
|
2022-07-18 09:38:49 +00:00
|
|
|
* Wait for all Products Block is loaded completely: when the skeleton disappears, and the products are visible
|
2022-05-13 13:18:51 +00:00
|
|
|
*/
|
2022-06-13 09:15:29 +00:00
|
|
|
export const waitForAllProductsBlockLoaded = async () => {
|
2022-11-22 14:28:27 +00:00
|
|
|
/**
|
|
|
|
* We use try with empty catch block here to avoid the race condition
|
|
|
|
* between the block loading and the test execution. After user actions,
|
|
|
|
* the products may or may not finish loading at the time we try to wait for
|
|
|
|
* the loading class.
|
|
|
|
*
|
|
|
|
* We need to wait for the loading class to be added then removed because
|
|
|
|
* only waiting for the loading class to be removed could result in a false
|
|
|
|
* positive pass.
|
|
|
|
*/
|
|
|
|
try {
|
|
|
|
await page.waitForSelector(
|
|
|
|
'.wc-block-grid__products.is-loading-products'
|
|
|
|
);
|
|
|
|
} catch ( ok ) {}
|
|
|
|
|
2022-10-07 15:00:19 +00:00
|
|
|
await page.waitForSelector(
|
|
|
|
'.wc-block-grid__products:not(.is-loading-products)'
|
|
|
|
);
|
2022-05-13 13:18:51 +00:00
|
|
|
};
|
2022-08-23 16:06:17 +00:00
|
|
|
|
2023-04-03 13:02:36 +00:00
|
|
|
/**
|
|
|
|
* Get all blocks in the document that match a certain slug.
|
|
|
|
*
|
|
|
|
* @param {string} slug Slug of the blocks to get.
|
|
|
|
*
|
|
|
|
* @return {Promise<{}>} Promise resolving with an array containing all blocks in
|
|
|
|
* the document that match a certain slug.
|
|
|
|
*/
|
|
|
|
export const getBlocksBySlug = async ( slug ) => {
|
|
|
|
const blocks = await getAllBlocks();
|
|
|
|
return blocks.filter( ( { name } ) => name === slug );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Insert the All Products block using the global inserter. This util is needed
|
|
|
|
* because inserting the All Products block using the `insertBlock()` util
|
|
|
|
* causes time outs.
|
|
|
|
*/
|
|
|
|
export const insertAllProductsBlock = async () => {
|
|
|
|
const searchTerm = 'All Products';
|
|
|
|
|
|
|
|
await searchForBlock( searchTerm );
|
|
|
|
|
|
|
|
// Wait for the default block list to disappear to prevent its items from
|
|
|
|
// being considered as search results. This is needed since we're debouncing
|
|
|
|
// search request.
|
|
|
|
await page.waitForSelector( '.block-editor-inserter__block-list', {
|
|
|
|
hidden: true,
|
|
|
|
} );
|
|
|
|
|
|
|
|
const insertButton = await page.waitForXPath(
|
|
|
|
`//*[@role='option' and contains(., '${ searchTerm }')]`
|
|
|
|
);
|
|
|
|
if ( ! insertButton ) {
|
|
|
|
throw new Error( `Could not find the "${ searchTerm }" block` );
|
|
|
|
}
|
|
|
|
insertButton?.click();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clicks on the button in the header which opens Document Settings sidebar when it is closed.
|
|
|
|
* Based on https://github.com/WordPress/gutenberg/blob/trunk/packages/e2e-test-utils/src/open-document-settings-sidebar.js,
|
|
|
|
* but updates the selector so it works in WP 6.2 without GB.
|
|
|
|
*/
|
|
|
|
export async function openSettingsSidebar() {
|
|
|
|
const toggleButton = await page.waitForSelector(
|
|
|
|
'.edit-post-header__settings button[aria-label="Settings"]'
|
|
|
|
);
|
|
|
|
|
|
|
|
const isClosed = await page.evaluate(
|
|
|
|
( element ) => element.getAttribute( 'aria-expanded' ) === 'false',
|
|
|
|
toggleButton
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( isClosed ) {
|
|
|
|
await toggleButton.click();
|
|
|
|
await page.waitForSelector( '.edit-post-sidebar' );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enables the `Show 'Apply filters' button` toggle.
|
|
|
|
*/
|
|
|
|
export const enableApplyFiltersButton = async () => {
|
|
|
|
await ensureSidebarOpened();
|
|
|
|
await switchBlockInspectorTab( 'Settings' );
|
|
|
|
|
|
|
|
await page.waitForXPath( SELECTORS.editor.filterButtonToggle );
|
|
|
|
|
|
|
|
const [ filterButtonToggle ] = await page.$x(
|
|
|
|
SELECTORS.editor.filterButtonToggle
|
|
|
|
);
|
|
|
|
if ( ! filterButtonToggle ) {
|
|
|
|
throw new Error( "'Apply filters' toggle not found via XPath." );
|
|
|
|
}
|
|
|
|
await filterButtonToggle.click();
|
|
|
|
|
|
|
|
// If for some reason click didn't work (it seems to happen intermittently),
|
|
|
|
// click on the toggle via JS.
|
|
|
|
await page.evaluate( () => {
|
|
|
|
const toggle = document.querySelector(
|
|
|
|
'.components-toggle-control:last-child .components-form-toggle__input'
|
|
|
|
);
|
|
|
|
if ( ! toggle ) {
|
|
|
|
throw new Error( "'Apply filters' toggle not found via CSS." );
|
|
|
|
}
|
|
|
|
if ( ! toggle.checked ) {
|
|
|
|
toggle.click();
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
|
|
|
await canvas().waitForXPath( SELECTORS.frontend.XPathSubmitButton );
|
|
|
|
};
|