Code review feedback

This commit is contained in:
Greg 2021-06-30 13:31:49 -06:00
parent 89d166e8b4
commit e08313d203
4 changed files with 39 additions and 29 deletions

View File

@ -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.

View File

@ -1,5 +1,7 @@
# Unreleased
- `updateReadyPageStatus` utility to update the status of the ready page
# 0.2.2
## Added

View File

@ -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,
};

View File

@ -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;