@wordpress/e2e-test-utils
Version:
End-To-End (E2E) test utils for WordPress.
226 lines (225 loc) • 8.35 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var inserter_exports = {};
__export(inserter_exports, {
closeGlobalBlockInserter: () => closeGlobalBlockInserter,
insertBlock: () => insertBlock,
insertBlockDirectoryBlock: () => insertBlockDirectoryBlock,
insertFromGlobalInserter: () => insertFromGlobalInserter,
insertPattern: () => insertPattern,
openGlobalBlockInserter: () => openGlobalBlockInserter,
searchForBlock: () => searchForBlock,
searchForBlockDirectoryBlock: () => searchForBlockDirectoryBlock,
searchForPattern: () => searchForPattern,
searchForReusableBlock: () => searchForReusableBlock,
searchGlobalInserter: () => searchGlobalInserter,
selectGlobalInserterTab: () => selectGlobalInserterTab,
toggleGlobalBlockInserter: () => toggleGlobalBlockInserter
});
module.exports = __toCommonJS(inserter_exports);
var import_puppeteer_core = require("puppeteer-core");
var import_press_key_with_modifier = require("./press-key-with-modifier");
var import_canvas = require("./canvas");
const INSERTER_SEARCH_SELECTOR = ".block-editor-inserter__search input,.block-editor-inserter__search-input,input.block-editor-inserter__search";
async function openGlobalBlockInserter() {
if (!await isGlobalInserterOpen()) {
await toggleGlobalBlockInserter();
await page.waitForSelector(".block-editor-inserter__menu");
}
}
async function closeGlobalBlockInserter() {
if (await isGlobalInserterOpen()) {
await toggleGlobalBlockInserter();
}
}
async function isGlobalInserterOpen() {
return await page.evaluate(() => {
return !!document.querySelector(
'.edit-post-header [aria-label="Add block"].is-pressed,.edit-site-header-edit-mode [aria-label="Add block"].is-pressed,.edit-post-header [aria-label="Block Inserter"].is-pressed,.edit-site-header [aria-label="Block Inserter"].is-pressed,.edit-widgets-header [aria-label="Block Inserter"].is-pressed,.edit-widgets-header [aria-label="Add block"].is-pressed,.edit-site-header-edit-mode__inserter-toggle.is-pressed,.editor-header [aria-label="Block Inserter"].is-pressed'
);
});
}
async function toggleGlobalBlockInserter() {
await page.click(
'.editor-document-tools__inserter-toggle,.edit-post-header [aria-label="Add block"],.edit-site-header [aria-label="Add block"],.edit-post-header [aria-label="Block Inserter"],.edit-site-header [aria-label="Block Inserter"],.edit-widgets-header [aria-label="Add block"],.edit-widgets-header [aria-label="Block Inserter"],.edit-site-header-edit-mode__inserter-toggle'
);
}
async function selectGlobalInserterTab(label) {
const tabs = await page.$(".block-editor-inserter__tabs");
if (!tabs) {
return;
}
const activeTab = await page.waitForSelector(
// Targeting a class name is necessary here, because there are likely
// two implementations of the `Tabs` component visible to this test, and
// we want to confirm that it's waiting for the correct one.
'.block-editor-inserter__tabs [role="tab"][aria-selected="true"]'
);
const activeTabLabel = await page.evaluate(
(el) => el.innerText,
activeTab
);
if (activeTabLabel === label) {
return;
}
let labelSelector;
switch (label) {
case "Blocks":
case "Patterns":
case "Media":
labelSelector = `. = "${label}"`;
break;
case "Synced patterns":
labelSelector = `@aria-label = "${label}"`;
break;
}
const targetTab = await page.waitForXPath(
`//div[contains(@class, "block-editor-inserter__tabs")]//button[${labelSelector}]`
);
await targetTab.click();
}
async function focusSelectedBlock() {
await page.evaluate(() => {
wp.data.dispatch("core/block-editor").selectBlock(
wp.data.select("core/block-editor").getSelectedBlockClientId(),
0
);
});
}
async function waitForInserterCloseAndContentFocus() {
await (0, import_canvas.canvas)().waitForFunction(
() => document.activeElement.closest(
".block-editor-block-list__layout"
) !== null
);
}
async function searchGlobalInserter(category, searchTerm) {
await openGlobalBlockInserter();
await page.waitForSelector(INSERTER_SEARCH_SELECTOR);
await page.focus(INSERTER_SEARCH_SELECTOR);
await (0, import_press_key_with_modifier.pressKeyWithModifier)("primary", "a");
await page.keyboard.type(searchTerm);
await page.waitForSelector(".block-editor-inserter__block-list", {
hidden: true
});
let waitForInsertElement;
let waitForNoResults;
switch (category) {
case "Blocks":
case "Patterns":
case "Synced patterns": {
waitForInsertElement = async () => {
return await page.waitForXPath(
`//*[@role='option' and contains(., '${searchTerm}')]`
);
};
waitForNoResults = async () => {
await page.waitForSelector(
".block-editor-inserter__no-results"
);
return null;
};
break;
}
case "Block Directory": {
waitForInsertElement = async () => {
return await page.waitForSelector(
".block-directory-downloadable-blocks-list button:first-child"
);
};
waitForNoResults = async () => {
return await new Promise(
(resolve) => setTimeout(() => resolve(null), 5e3)
);
};
}
}
return await Promise.race([waitForInsertElement(), waitForNoResults()]);
}
async function insertFromGlobalInserter(category, searchTerm) {
await openGlobalBlockInserter();
await selectGlobalInserterTab(category);
let insertButton;
if (["Blocks", "Synced patterns"].includes(category)) {
try {
insertButton = (await page.$x(
`//*[@role='option' and contains(., '${searchTerm}')]`
))[0];
} catch (error) {
}
}
if (!insertButton) {
insertButton = await searchGlobalInserter(category, searchTerm);
}
if (!insertButton) {
throw new Error(
`Couldn't find "${searchTerm}" in the ${category} category.`
);
}
await insertButton.click();
if (category === "Synced patterns") {
await (0, import_canvas.canvas)().waitForSelector(
".block-library-block__reusable-block-container"
);
}
if (category === "Block Directory") {
await page.waitForSelector(
".block-directory-downloadable-blocks-list button:first-child:not(.is-busy)"
);
}
await focusSelectedBlock();
await waitForInserterCloseAndContentFocus();
}
async function searchForBlock(searchTerm) {
return await searchGlobalInserter("Blocks", searchTerm);
}
async function searchForPattern(searchTerm) {
return await searchGlobalInserter("Patterns", searchTerm);
}
async function searchForReusableBlock(searchTerm) {
return await searchGlobalInserter("Reusable", searchTerm);
}
async function searchForBlockDirectoryBlock(searchTerm) {
return await searchGlobalInserter("Block Directory", searchTerm);
}
async function insertBlock(searchTerm) {
await insertFromGlobalInserter("Blocks", searchTerm);
}
async function insertPattern(searchTerm) {
await insertFromGlobalInserter("Patterns", searchTerm);
}
async function insertBlockDirectoryBlock(searchTerm) {
return await insertFromGlobalInserter("Block Directory", searchTerm);
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
closeGlobalBlockInserter,
insertBlock,
insertBlockDirectoryBlock,
insertFromGlobalInserter,
insertPattern,
openGlobalBlockInserter,
searchForBlock,
searchForBlockDirectoryBlock,
searchForPattern,
searchForReusableBlock,
searchGlobalInserter,
selectGlobalInserterTab,
toggleGlobalBlockInserter
});
//# sourceMappingURL=inserter.js.map