From fcc02977f55e72a74f2a7d6f965dbfda2799a862 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Albert=20Juh=C3=A9=20Lluveras?= Date: Fri, 1 Dec 2023 13:11:07 +0100 Subject: [PATCH] Create WCRestApiUtils --- tests/e2e/playwright-utils/test.ts | 3 + .../all-reviews.block_theme.spec.ts | 52 ++++++------- .../reviews-by-category.block_theme.spec.ts | 67 ++++++++-------- .../reviews-by-product.block_theme.spec.ts | 52 ++++++------- .../e2e/utils/api/get-woocommerce-rest-api.ts | 28 ------- tests/e2e/utils/api/index.ts | 2 +- .../e2e/utils/api/template-api-utils.page.ts | 1 + tests/e2e/utils/api/wc-rest-api-utils.page.ts | 76 +++++++++++++++++++ 8 files changed, 157 insertions(+), 124 deletions(-) delete mode 100644 tests/e2e/utils/api/get-woocommerce-rest-api.ts create mode 100644 tests/e2e/utils/api/wc-rest-api-utils.page.ts diff --git a/tests/e2e/playwright-utils/test.ts b/tests/e2e/playwright-utils/test.ts index 8f2c06fafec..08a06f9eae1 100644 --- a/tests/e2e/playwright-utils/test.ts +++ b/tests/e2e/playwright-utils/test.ts @@ -12,6 +12,7 @@ import { } from '@wordpress/e2e-test-utils-playwright'; import { + WCRestApiUtils, TemplateApiUtils, STORAGE_STATE_PATH, EditorUtils, @@ -106,6 +107,7 @@ const test = base.extend< admin: Admin; editor: Editor; pageUtils: PageUtils; + wcRestApiUtils: WCRestApiUtils; templateApiUtils: TemplateApiUtils; editorUtils: EditorUtils; frontendUtils: FrontendUtils; @@ -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 ) => { diff --git a/tests/e2e/tests/all-reviews/all-reviews.block_theme.spec.ts b/tests/e2e/tests/all-reviews/all-reviews.block_theme.spec.ts index 9ff908a0345..7e234b8f56e 100644 --- a/tests/e2e/tests/all-reviews/all-reviews.block_theme.spec.ts +++ b/tests/e2e/tests/all-reviews/all-reviews.block_theme.spec.ts @@ -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', @@ -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 ( { diff --git a/tests/e2e/tests/reviews-by-category/reviews-by-category.block_theme.spec.ts b/tests/e2e/tests/reviews-by-category/reviews-by-category.block_theme.spec.ts index 984a9eb526c..2d1befa2102 100644 --- a/tests/e2e/tests/reviews-by-category/reviews-by-category.block_theme.spec.ts +++ b/tests/e2e/tests/reviews-by-category/reviews-by-category.block_theme.spec.ts @@ -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', @@ -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 ( { diff --git a/tests/e2e/tests/reviews-by-product/reviews-by-product.block_theme.spec.ts b/tests/e2e/tests/reviews-by-product/reviews-by-product.block_theme.spec.ts index 178653d0edd..a3141ecfbb0 100644 --- a/tests/e2e/tests/reviews-by-product/reviews-by-product.block_theme.spec.ts +++ b/tests/e2e/tests/reviews-by-product/reviews-by-product.block_theme.spec.ts @@ -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', @@ -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 ( { diff --git a/tests/e2e/utils/api/get-woocommerce-rest-api.ts b/tests/e2e/utils/api/get-woocommerce-rest-api.ts deleted file mode 100644 index d1986ca053e..00000000000 --- a/tests/e2e/utils/api/get-woocommerce-rest-api.ts +++ /dev/null @@ -1,28 +0,0 @@ -/** - * External dependencies - */ -import WooCommerceRestApi from '@woocommerce/woocommerce-rest-api'; - -export const getWooCommerceRestApi = ( baseURL: string | undefined ) => { - if ( typeof baseURL !== 'string' || baseURL === '' ) { - throw new Error( 'baseURL is not set.' ); - } - if ( - typeof process.env.CONSUMER_KEY !== 'string' || - process.env.CONSUMER_KEY === '' - ) { - throw new Error( 'CONSUMER_KEY is not set.' ); - } - if ( - typeof process.env.CONSUMER_SECRET !== 'string' || - process.env.CONSUMER_SECRET === '' - ) { - throw new Error( 'CONSUMER_SECRET is not set.' ); - } - return new WooCommerceRestApi( { - url: baseURL || '', - consumerKey: process.env.CONSUMER_KEY || '', - consumerSecret: process.env.CONSUMER_SECRET || '', - version: 'wc/v3', - } ); -}; diff --git a/tests/e2e/utils/api/index.ts b/tests/e2e/utils/api/index.ts index 61c435e6d61..3a8067bc640 100644 --- a/tests/e2e/utils/api/index.ts +++ b/tests/e2e/utils/api/index.ts @@ -1,2 +1,2 @@ -export * from './get-woocommerce-rest-api'; +export * from './wc-rest-api-utils.page'; export * from './template-api-utils.page'; diff --git a/tests/e2e/utils/api/template-api-utils.page.ts b/tests/e2e/utils/api/template-api-utils.page.ts index f4e1ec01a93..a32bf685038 100644 --- a/tests/e2e/utils/api/template-api-utils.page.ts +++ b/tests/e2e/utils/api/template-api-utils.page.ts @@ -3,6 +3,7 @@ */ import { request as req } from '@playwright/test'; import fs from 'fs/promises'; + /** * Internal dependencies */ diff --git a/tests/e2e/utils/api/wc-rest-api-utils.page.ts b/tests/e2e/utils/api/wc-rest-api-utils.page.ts new file mode 100644 index 00000000000..c76a60351e6 --- /dev/null +++ b/tests/e2e/utils/api/wc-rest-api-utils.page.ts @@ -0,0 +1,76 @@ +/** + * External dependencies + */ +import WooCommerceRestApi from '@woocommerce/woocommerce-rest-api'; + +/** + * Internal dependencies + */ +import { BASE_URL } from '../constants'; + +export class WCRestApiUtils { + api: WooCommerceRestApi; + constructor() { + if ( + typeof process.env.CONSUMER_KEY !== 'string' || + process.env.CONSUMER_KEY === '' + ) { + throw new Error( 'CONSUMER_KEY is not set.' ); + } + if ( + typeof process.env.CONSUMER_SECRET !== 'string' || + process.env.CONSUMER_SECRET === '' + ) { + throw new Error( 'CONSUMER_SECRET is not set.' ); + } + this.api = new WooCommerceRestApi( { + url: BASE_URL || '', + consumerKey: process.env.CONSUMER_KEY || '', + consumerSecret: process.env.CONSUMER_SECRET || '', + version: 'wc/v3', + } ); + } + + async createProduct( + productDetails: object, + callback: ( response: { data: { id: number } } ) => void + ) { + await this.api.post( 'products', productDetails ).then( callback ); + } + + async createProductCategory( + categoryDetails: object, + callback: ( response: { data: { id: number } } ) => void + ) { + await this.api + .post( 'products/categories', categoryDetails ) + .then( callback ); + } + + async createProductReview( + reviewDetails: object, + callback: ( response: { data: { id: number } } ) => void + ) { + await this.api + .post( 'products/reviews', reviewDetails ) + .then( callback ); + } + + async deleteProduct( productId: number ) { + await this.api.delete( `products/${ productId }`, { + force: true, + } ); + } + + async deleteProductCategory( categoryId: number ) { + await this.api.delete( `products/categories/${ categoryId }`, { + force: true, + } ); + } + + async deleteProductReview( reviewId: number ) { + await this.api.delete( `products/reviews/${ reviewId }`, { + force: true, + } ); + } +}