woocommerce/packages/js/e2e-environment/utils/update-ready-page.js

37 lines
1.4 KiB
JavaScript
Raw Normal View History

2021-06-30 19:31:49 +00:00
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 )
2021-06-30 19:31:49 +00:00
.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}` );
2021-08-24 12:18:45 +00:00
if ( getPostsResponse.data && getPostsResponse.data.length > 0 ) {
2021-07-06 17:19:09 +00:00
const pageId = getPostsResponse.data[0].id;
// Update the page to the new status
await client.post( `${wpPagesEndpoint}/${pageId}`, { 'status': status } );
2021-07-06 17:19:09 +00:00
}
2021-06-30 19:31:49 +00:00
}
module.exports = updateReadyPageStatus;