Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
Create WCRestApiUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
Aljullu committed Dec 1, 2023
1 parent 0c0bf3f commit fcc0297
Show file tree
Hide file tree
Showing 8 changed files with 157 additions and 124 deletions.
3 changes: 3 additions & 0 deletions tests/e2e/playwright-utils/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
} from '@wordpress/e2e-test-utils-playwright';

import {
WCRestApiUtils,
TemplateApiUtils,
STORAGE_STATE_PATH,
EditorUtils,
Expand Down Expand Up @@ -106,6 +107,7 @@ const test = base.extend<
admin: Admin;
editor: Editor;
pageUtils: PageUtils;
wcRestApiUtils: WCRestApiUtils;
templateApiUtils: TemplateApiUtils;
editorUtils: EditorUtils;
frontendUtils: FrontendUtils;
Expand Down Expand Up @@ -137,6 +139,7 @@ const test = base.extend<
pageUtils: async ( { page }, use ) => {
await use( new PageUtils( { page } ) );
},
wcRestApiUtils: async ( {}, use ) => await use( new WCRestApiUtils() ),
templateApiUtils: async ( {}, use ) =>
await use( new TemplateApiUtils( baseRequest ) ),
editorUtils: async ( { editor, page }, use ) => {
Expand Down
52 changes: 23 additions & 29 deletions tests/e2e/tests/all-reviews/all-reviews.block_theme.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* External dependencies
*/
import { expect, test } from '@woocommerce/e2e-playwright-utils';
import { getWooCommerceRestApi } from 'tests/e2e/utils/api/get-woocommerce-rest-api';

const blockData = {
name: 'woocommerce/all-reviews',
Expand All @@ -22,53 +21,48 @@ test.describe( `${ blockData.name } Block`, () => {
const secondReviewContent = 'Not bad.';

// Create product and reviews.
test.beforeAll( async ( { baseURL } ) => {
const api = getWooCommerceRestApi( baseURL );
await api
.post( 'products', {
test.beforeAll( async ( { wcRestApiUtils } ) => {
await wcRestApiUtils.createProduct(
{
name: 'Product with reviews',
type: 'simple',
regular_price: '12.99',
} )
.then( ( response ) => {
},
( response ) => {
productId = response.data.id;
} );
await api
.post( 'products/reviews', {
}
);
await wcRestApiUtils.createProductReview(
{
product_id: productId,
review: firstReviewContent,
reviewer: 'John Doe',
reviewer_email: 'john.doe@example.com',
rating: 5,
} )
.then( ( response ) => {
},
( response ) => {
firstReviewId = response.data.id;
} );
await api
.post( 'products/reviews', {
}
);
await wcRestApiUtils.createProductReview(
{
product_id: productId,
review: secondReviewContent,
reviewer: 'John Doe',
reviewer_email: 'john.doe@example.com',
rating: 4,
} )
.then( ( response ) => {
},
( response ) => {
secondReviewId = response.data.id;
} );
}
);
} );

// Remove product and reviews.
test.afterAll( async ( { baseURL } ) => {
const api = getWooCommerceRestApi( baseURL );
await api.delete( `products/reviews/${ firstReviewId }`, {
force: true,
} );
await api.delete( `products/reviews/${ secondReviewId }`, {
force: true,
} );
await api.delete( `products/${ productId }`, {
force: true,
} );
test.afterAll( async ( { wcRestApiUtils } ) => {
await wcRestApiUtils.deleteProductReview( firstReviewId );
await wcRestApiUtils.deleteProductReview( secondReviewId );
await wcRestApiUtils.deleteProduct( productId );
} );

test( 'renders a review in the editor and the frontend', async ( {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* External dependencies
*/
import { expect, test } from '@woocommerce/e2e-playwright-utils';
import { getWooCommerceRestApi } from 'tests/e2e/utils/api/get-woocommerce-rest-api';

const blockData = {
name: 'woocommerce/reviews-by-category',
Expand All @@ -23,64 +22,58 @@ test.describe( `${ blockData.name } Block`, () => {
const secondReviewContent = 'Not bad.';

// Create category, product and reviews.
test.beforeAll( async ( { baseURL } ) => {
const api = getWooCommerceRestApi( baseURL );
await api
.post( 'products/categories', {
test.beforeAll( async ( { wcRestApiUtils } ) => {
await wcRestApiUtils.createProductCategory(
{
name: 'Products with reviews',
} )
.then( ( response ) => {
},
( response ) => {
categoryId = response.data.id;
} );
await api
.post( 'products', {
}
);
await wcRestApiUtils.createProduct(
{
name: 'Product with reviews',
type: 'simple',
regular_price: '12.99',
categories: [ { id: categoryId } ],
} )
.then( ( response ) => {
},
( response ) => {
productId = response.data.id;
} );
await api
.post( 'products/reviews', {
}
);
await wcRestApiUtils.createProductReview(
{
product_id: productId,
review: firstReviewContent,
reviewer: 'John Doe',
reviewer_email: 'john.doe@example.com',
rating: 5,
} )
.then( ( response ) => {
},
( response ) => {
firstReviewId = response.data.id;
} );
await api
.post( 'products/reviews', {
}
);
await wcRestApiUtils.createProductReview(
{
product_id: productId,
review: secondReviewContent,
reviewer: 'John Doe',
reviewer_email: 'john.doe@example.com',
rating: 4,
} )
.then( ( response ) => {
},
( response ) => {
secondReviewId = response.data.id;
} );
}
);
} );

// Remove category, product and reviews.
test.afterAll( async ( { baseURL } ) => {
const api = getWooCommerceRestApi( baseURL );
await api.delete( `products/reviews/${ firstReviewId }`, {
force: true,
} );
await api.delete( `products/reviews/${ secondReviewId }`, {
force: true,
} );
await api.delete( `products/${ productId }`, {
force: true,
} );
await api.delete( `products/categories/${ categoryId }`, {
force: true,
} );
test.afterAll( async ( { wcRestApiUtils } ) => {
await wcRestApiUtils.deleteProductReview( firstReviewId );
await wcRestApiUtils.deleteProductReview( secondReviewId );
await wcRestApiUtils.deleteProduct( productId );
await wcRestApiUtils.deleteProductCategory( categoryId );
} );

test( 'renders a review in the editor and the frontend', async ( {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* External dependencies
*/
import { expect, test } from '@woocommerce/e2e-playwright-utils';
import { getWooCommerceRestApi } from 'tests/e2e/utils/api/get-woocommerce-rest-api';

const blockData = {
name: 'woocommerce/reviews-by-product',
Expand All @@ -22,53 +21,48 @@ test.describe( `${ blockData.name } Block`, () => {
const secondReviewContent = 'Not bad.';

// Create product and reviews.
test.beforeAll( async ( { baseURL } ) => {
const api = getWooCommerceRestApi( baseURL );
await api
.post( 'products', {
test.beforeAll( async ( { wcRestApiUtils } ) => {
await wcRestApiUtils.createProduct(
{
name: 'Product with reviews',
type: 'simple',
regular_price: '12.99',
} )
.then( ( response ) => {
},
( response ) => {
productId = response.data.id;
} );
await api
.post( 'products/reviews', {
}
);
await wcRestApiUtils.createProductReview(
{
product_id: productId,
review: firstReviewContent,
reviewer: 'John Doe',
reviewer_email: 'john.doe@example.com',
rating: 5,
} )
.then( ( response ) => {
},
( response ) => {
firstReviewId = response.data.id;
} );
await api
.post( 'products/reviews', {
}
);
await wcRestApiUtils.createProductReview(
{
product_id: productId,
review: secondReviewContent,
reviewer: 'John Doe',
reviewer_email: 'john.doe@example.com',
rating: 4,
} )
.then( ( response ) => {
},
( response ) => {
secondReviewId = response.data.id;
} );
}
);
} );

// Remove product and reviews.
test.afterAll( async ( { baseURL } ) => {
const api = getWooCommerceRestApi( baseURL );
await api.delete( `products/reviews/${ firstReviewId }`, {
force: true,
} );
await api.delete( `products/reviews/${ secondReviewId }`, {
force: true,
} );
await api.delete( `products/${ productId }`, {
force: true,
} );
test.afterAll( async ( { wcRestApiUtils } ) => {
await wcRestApiUtils.deleteProductReview( firstReviewId );
await wcRestApiUtils.deleteProductReview( secondReviewId );
await wcRestApiUtils.deleteProduct( productId );
} );

test( 'renders a review in the editor and the frontend', async ( {
Expand Down
28 changes: 0 additions & 28 deletions tests/e2e/utils/api/get-woocommerce-rest-api.ts

This file was deleted.

2 changes: 1 addition & 1 deletion tests/e2e/utils/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './get-woocommerce-rest-api';
export * from './wc-rest-api-utils.page';
export * from './template-api-utils.page';
1 change: 1 addition & 0 deletions tests/e2e/utils/api/template-api-utils.page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
import { request as req } from '@playwright/test';
import fs from 'fs/promises';

/**
* Internal dependencies
*/
Expand Down
Loading

0 comments on commit fcc0297

Please sign in to comment.