@wordpress/e2e-test-utils-playwright
Version:
End-To-End (E2E) test utils for WordPress.
8 lines (7 loc) • 3.91 kB
Source Map (JSON)
{
"version": 3,
"sources": ["../../src/request-utils/plugins.ts"],
"sourcesContent": ["/**\n * External dependencies\n */\nimport { paramCase as kebabCase } from 'change-case';\n\n/**\n * Internal dependencies\n */\nimport type { RequestUtils } from './index';\n\n/**\n * Fetch the plugins from API and cache them in memory,\n * since they are unlikely to change during testing.\n *\n * @param this\n * @param forceRefetch Force refetch the installed plugins to update the cache.\n */\nasync function getPluginsMap( this: RequestUtils, forceRefetch = false ) {\n\tif ( ! forceRefetch && this.pluginsMap ) {\n\t\treturn this.pluginsMap;\n\t}\n\n\tconst plugins = await this.rest( {\n\t\tpath: '/wp/v2/plugins',\n\t} );\n\tthis.pluginsMap = {};\n\tfor ( const plugin of plugins ) {\n\t\t// Ideally, we should be using sanitize_title() in PHP rather than kebabCase(),\n\t\t// but we don't have the exact port of it in JS.\n\t\t// This is a good approximation though.\n\t\tconst slug = kebabCase( plugin.name.toLowerCase() );\n\t\tthis.pluginsMap[ slug ] = plugin.plugin;\n\t}\n\treturn this.pluginsMap;\n}\n\n/**\n * Finds a plugin in the plugin map.\n *\n * Attempts to provide a helpful error message if not found.\n *\n * @param slug Plugin slug.\n * @param pluginsMap Plugins map.\n */\nfunction getPluginFromMap(\n\tslug: string,\n\tpluginsMap: Record< string, string >\n) {\n\tconst plugin = pluginsMap[ slug ];\n\n\tif ( ! plugin ) {\n\t\tfor ( const key of Object.keys( pluginsMap ) ) {\n\t\t\tif (\n\t\t\t\tkey.toLowerCase().replace( /-/g, '' ) ===\n\t\t\t\tslug.toLowerCase().replace( /-/g, '' )\n\t\t\t) {\n\t\t\t\tthrow new Error(\n\t\t\t\t\t`The plugin \"${ slug }\" isn't installed. Did you perhaps mean \"${ key }\"?`\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\n\t\tthrow new Error( `The plugin \"${ slug }\" isn't installed` );\n\t}\n\n\treturn plugin;\n}\n\n/**\n * Activates an installed plugin.\n *\n * @param this RequestUtils.\n * @param slug Plugin slug.\n */\nasync function activatePlugin( this: RequestUtils, slug: string ) {\n\tconst pluginsMap = await this.getPluginsMap();\n\tconst plugin = getPluginFromMap( slug, pluginsMap );\n\n\tawait this.rest( {\n\t\tmethod: 'PUT',\n\t\tpath: `/wp/v2/plugins/${ plugin }`,\n\t\tdata: { status: 'active' },\n\t} );\n}\n\n/**\n * Deactivates an active plugin.\n *\n * @param this RequestUtils.\n * @param slug Plugin slug.\n */\nasync function deactivatePlugin( this: RequestUtils, slug: string ) {\n\tconst pluginsMap = await this.getPluginsMap();\n\tconst plugin = getPluginFromMap( slug, pluginsMap );\n\n\tawait this.rest( {\n\t\tmethod: 'PUT',\n\t\tpath: `/wp/v2/plugins/${ plugin }`,\n\t\tdata: { status: 'inactive' },\n\t} );\n}\n\nexport { getPluginsMap, activatePlugin, deactivatePlugin };\n"],
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,yBAAuC;AAcvC,eAAe,cAAmC,eAAe,OAAQ;AACxE,MAAK,CAAE,gBAAgB,KAAK,YAAa;AACxC,WAAO,KAAK;AAAA,EACb;AAEA,QAAM,UAAU,MAAM,KAAK,KAAM;AAAA,IAChC,MAAM;AAAA,EACP,CAAE;AACF,OAAK,aAAa,CAAC;AACnB,aAAY,UAAU,SAAU;AAI/B,UAAM,WAAO,mBAAAA,WAAW,OAAO,KAAK,YAAY,CAAE;AAClD,SAAK,WAAY,IAAK,IAAI,OAAO;AAAA,EAClC;AACA,SAAO,KAAK;AACb;AAUA,SAAS,iBACR,MACA,YACC;AACD,QAAM,SAAS,WAAY,IAAK;AAEhC,MAAK,CAAE,QAAS;AACf,eAAY,OAAO,OAAO,KAAM,UAAW,GAAI;AAC9C,UACC,IAAI,YAAY,EAAE,QAAS,MAAM,EAAG,MACpC,KAAK,YAAY,EAAE,QAAS,MAAM,EAAG,GACpC;AACD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