diff --git a/tests/e2e/config/jest.setup.js b/tests/e2e/config/jest.setup.js index 5c872c85efe..bec62478994 100644 --- a/tests/e2e/config/jest.setup.js +++ b/tests/e2e/config/jest.setup.js @@ -7,7 +7,7 @@ import { const config = require('config'); const { HTTPClientFactory } = require('@woocommerce/api'); -const { addConsoleSuppression } = require( '@woocommerce/e2e-environment' ); +const { addConsoleSuppression, updateReadyPageStatus } = require( '@woocommerce/e2e-environment' ); // @todo: remove this once https://github.com/woocommerce/woocommerce-admin/issues/6992 has been addressed addConsoleSuppression( 'woocommerce_shared_settings' ); @@ -34,34 +34,6 @@ async function trashExistingPosts() { } } -/** - * Uses the WordPress API to update the Ready page's status. - * - * @param {string} status | Status to update the page to. One of: publish, future, draft, pending, private - */ -async function updateReadyPageStatus( status ) { - const apiUrl = config.get('url'); - const wpPagesEndpoint = '/wp/v2/pages'; - const adminUsername = config.get('users.admin.username'); - const adminPassword = config.get('users.admin.password'); - const client = HTTPClientFactory.build(apiUrl) - .withBasicAuth(adminUsername, adminPassword) - .create(); - - // As the default status filter in the API is `publish`, we need to - // filter based on the supplied status otherwise no results are returned. - let statusFilter = 'publish'; - if ( 'publish' === status ) { - // The page will be in a draft state, so we need to filter on that status - statusFilter = 'draft'; - } - const getPostsResponse = await client.get(`${wpPagesEndpoint}?search=ready&status=${statusFilter}`); - const pageId = getPostsResponse.data[0].id; - - // Update the page to the new status - await client.post(`${wpPagesEndpoint}/${pageId}`, { 'status': status }); -} - // Before every test suite run, delete all content created by the test. This ensures // other posts/comments/etc. aren't dirtying tests and tests don't depend on // each other's side-effects. diff --git a/tests/e2e/env/CHANGELOG.md b/tests/e2e/env/CHANGELOG.md index 9d49bdc3498..dee76d2481b 100644 --- a/tests/e2e/env/CHANGELOG.md +++ b/tests/e2e/env/CHANGELOG.md @@ -1,5 +1,7 @@ # Unreleased +- `updateReadyPageStatus` utility to update the status of the ready page + # 0.2.2 ## Added diff --git a/tests/e2e/env/utils/index.js b/tests/e2e/env/utils/index.js index d66733a44fd..207d00f1049 100644 --- a/tests/e2e/env/utils/index.js +++ b/tests/e2e/env/utils/index.js @@ -2,6 +2,7 @@ const getAppRoot = require( './app-root' ); const { getAppName, getAppBase } = require( './app-name' ); const { getTestConfig, getAdminConfig } = require( './test-config' ); const takeScreenshotFor = require( './take-screenshot' ); +const updateReadyPageStatus = require('./update-ready-page'); const consoleUtils = require( './filter-console' ); module.exports = { @@ -11,5 +12,6 @@ module.exports = { getTestConfig, getAdminConfig, takeScreenshotFor, + updateReadyPageStatus, ...consoleUtils, }; diff --git a/tests/e2e/env/utils/update-ready-page.js b/tests/e2e/env/utils/update-ready-page.js new file mode 100644 index 00000000000..219f2f8c6fc --- /dev/null +++ b/tests/e2e/env/utils/update-ready-page.js @@ -0,0 +1,34 @@ +const { getTestConfig } = require( './test-config' ); +const { HTTPClientFactory } = require('@woocommerce/api'); + +/** + * Uses the WordPress API to update the Ready page's status. + * + * @param {string} status | Status to update the page to. One of: publish, future, draft, pending, private + */ +const updateReadyPageStatus = async ( status ) => { + const testConfig = getTestConfig(); + + const apiUrl = testConfig.url; + const wpPagesEndpoint = '/wp/v2/pages'; + const adminUsername = testConfig.users.admin.username ? testConfig.users.admin.username : 'admin'; + const adminPassword = testConfig.users.admin.password ? testConfig.users.admin.password : 'password'; + const client = HTTPClientFactory.build(apiUrl) + .withBasicAuth(adminUsername, adminPassword) + .create(); + + // As the default status filter in the API is `publish`, we need to + // filter based on the supplied status otherwise no results are returned. + let statusFilter = 'publish'; + if ( 'publish' === status ) { + // The page will be in a draft state, so we need to filter on that status + statusFilter = 'draft'; + } + const getPostsResponse = await client.get(`${wpPagesEndpoint}?search=ready&status=${statusFilter}`); + const pageId = getPostsResponse.data[0].id; + + // Update the page to the new status + await client.post(`${wpPagesEndpoint}/${pageId}`, { 'status': status }); +} + +module.exports = updateReadyPageStatus;