2021-03-30 22:03:52 +00:00
|
|
|
import { SimpleProduct, Coupon } from '@woocommerce/api';
|
2021-01-19 13:49:18 +00:00
|
|
|
import {
|
2020-08-04 22:55:13 +00:00
|
|
|
visitAdminPage,
|
2020-08-05 18:01:43 +00:00
|
|
|
switchUserToTest,
|
|
|
|
clearLocalStorage,
|
2021-01-19 13:49:18 +00:00
|
|
|
setBrowserViewport,
|
|
|
|
factories,
|
|
|
|
} from '@woocommerce/e2e-utils';
|
2020-08-04 22:55:13 +00:00
|
|
|
|
2021-01-19 15:02:08 +00:00
|
|
|
const { merchant } = require( '@woocommerce/e2e-utils' );
|
|
|
|
|
2020-08-04 22:55:13 +00:00
|
|
|
/**
|
|
|
|
* Navigates to the post listing screen and bulk-trashes any posts which exist.
|
|
|
|
*
|
|
|
|
* @return {Promise} Promise resolving once posts have been trashed.
|
|
|
|
*/
|
|
|
|
async function trashExistingPosts() {
|
2021-01-19 15:02:08 +00:00
|
|
|
await merchant.login();
|
2020-08-04 22:55:13 +00:00
|
|
|
// Visit `/wp-admin/edit.php` so we can see a list of posts and delete them.
|
|
|
|
await visitAdminPage( 'edit.php' );
|
|
|
|
|
|
|
|
// If this selector doesn't exist there are no posts for us to delete.
|
|
|
|
const bulkSelector = await page.$( '#bulk-action-selector-top' );
|
|
|
|
if ( ! bulkSelector ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Select all posts.
|
|
|
|
await page.waitForSelector( '#cb-select-all-1' );
|
|
|
|
await page.click( '#cb-select-all-1' );
|
|
|
|
// Select the "bulk actions" > "trash" option.
|
|
|
|
await page.select( '#bulk-action-selector-top', 'trash' );
|
|
|
|
// Submit the form to send all draft/scheduled/published posts to the trash.
|
|
|
|
await page.click( '#doaction' );
|
|
|
|
await page.waitForXPath(
|
|
|
|
'//*[contains(@class, "updated notice")]/p[contains(text(), "moved to the Trash.")]'
|
|
|
|
);
|
|
|
|
await switchUserToTest();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-01-19 13:49:18 +00:00
|
|
|
* Use api package to delete products.
|
2020-08-04 22:55:13 +00:00
|
|
|
*
|
|
|
|
* @return {Promise} Promise resolving once products have been trashed.
|
|
|
|
*/
|
2021-01-19 13:49:18 +00:00
|
|
|
async function deleteAllProducts() {
|
|
|
|
const repository = SimpleProduct.restRepository( factories.api.withDefaultPermalinks );
|
|
|
|
let products;
|
2020-08-04 22:55:13 +00:00
|
|
|
|
2021-01-19 13:49:18 +00:00
|
|
|
products = await repository.list();
|
|
|
|
while ( products.length > 0 ) {
|
|
|
|
for( let p = 0; p < products.length; p++ ) {
|
|
|
|
await repository.delete( products[ p ].id );
|
|
|
|
}
|
|
|
|
products = await repository.list();
|
2020-08-04 22:55:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-30 22:03:52 +00:00
|
|
|
/**
|
|
|
|
* Use api package to delete coupons.
|
|
|
|
*
|
|
|
|
* @return {Promise} Promise resolving once coupons have been trashed.
|
|
|
|
*/
|
|
|
|
async function deleteAllCoupons() {
|
|
|
|
const repository = Coupon.restRepository( factories.api.withDefaultPermalinks );
|
|
|
|
let coupons;
|
|
|
|
|
|
|
|
coupons = await repository.list();
|
|
|
|
|
|
|
|
while ( coupons.length > 0 ) {
|
|
|
|
for (let c = 0; c < coupons.length; c++ ) {
|
|
|
|
await repository.delete( coupons[ c ].id );
|
|
|
|
}
|
|
|
|
coupons = await repository.list();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-04 22:55:13 +00:00
|
|
|
// 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.
|
|
|
|
beforeAll( async () => {
|
|
|
|
await trashExistingPosts();
|
2021-01-19 13:49:18 +00:00
|
|
|
await deleteAllProducts();
|
2021-03-30 22:03:52 +00:00
|
|
|
await deleteAllCoupons();
|
2020-08-05 18:01:43 +00:00
|
|
|
await clearLocalStorage();
|
|
|
|
await setBrowserViewport( 'large' );
|
2020-08-04 22:55:13 +00:00
|
|
|
} );
|