@wordpress/e2e-test-utils-playwright
Version:
End-To-End (E2E) test utils for WordPress.
90 lines (88 loc) • 3.09 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);
// packages/e2e-test-utils-playwright/src/request-utils/themes.ts
var themes_exports = {};
__export(themes_exports, {
activateTheme: () => activateTheme,
getCurrentThemeGlobalStylesPostId: () => getCurrentThemeGlobalStylesPostId,
getThemeGlobalStylesRevisions: () => getThemeGlobalStylesRevisions
});
module.exports = __toCommonJS(themes_exports);
var import_config = require("../config.cjs");
var THEMES_URL = new URL("wp-admin/themes.php", import_config.WP_BASE_URL).href;
async function activateTheme(themeSlug) {
let response = await this.request.get(THEMES_URL);
const html = await response.text();
const optionalFolder = "([a-z0-9-]+%2F)?";
let matchGroup = html.match(
`action=activate&stylesheet=${encodeURIComponent(
themeSlug
)}&_wpnonce=[a-z0-9]+`
);
if (!matchGroup) {
matchGroup = html.match(
`action=activate&stylesheet=${optionalFolder}${encodeURIComponent(
themeSlug
)}&_wpnonce=[a-z0-9]+`
);
}
if (!matchGroup) {
if (html.includes(`data-slug="${themeSlug}"`)) {
return;
}
throw new Error(`The theme "${themeSlug}" is not installed`);
}
const [activateQuery] = matchGroup;
const activateLink = THEMES_URL + `?${activateQuery}`.replace(/&/g, "&");
response = await this.request.get(activateLink);
await response.dispose();
}
async function getCurrentThemeGlobalStylesPostId() {
const themes = await this.rest({
path: "/wp/v2/themes"
});
let themeGlobalStylesId = "";
if (themes && themes.length) {
const currentTheme = themes.find(
({ status }) => status === "active"
);
const globalStylesURL = currentTheme?._links?.["wp:user-global-styles"]?.[0]?.href;
if (globalStylesURL) {
themeGlobalStylesId = globalStylesURL?.split(
"rest_route=/wp/v2/global-styles/"
)[1];
}
}
return themeGlobalStylesId;
}
async function getThemeGlobalStylesRevisions(parentId) {
return await this.rest({
path: `/wp/v2/global-styles/${parentId}/revisions`,
params: {
per_page: 100
}
});
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
activateTheme,
getCurrentThemeGlobalStylesPostId,
getThemeGlobalStylesRevisions
});
//# sourceMappingURL=themes.cjs.map