Skip to content

Commit

Permalink
update: update product addon tests
Browse files Browse the repository at this point in the history
  • Loading branch information
shashwatahalder01 committed Feb 13, 2024
1 parent 68e54c7 commit 0420dd8
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 14 deletions.
32 changes: 24 additions & 8 deletions tests/pw/tests/e2e/productAddons.spec.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,56 @@
import { test, Page } from '@playwright/test';
import { test, request, Page } from '@playwright/test';
import { ProductAddonsPage } from '@pages/productAddonsPage';
import { ApiUtils } from '@utils/apiUtils';
import { data } from '@utils/testData';
import { payloads } from '@utils/payloads';
import { dbUtils } from '@utils/dbUtils';

const { VENDOR_ID } = process.env;

test.describe('Product addon functionality test', () => {
let vendor: ProductAddonsPage;
let vPage: Page;
let addonId: string;
let addonName: string;
let addonFieldTitle: string;
let apiUtils: ApiUtils;

async function createVendorProductAddon(): Promise<[string, string, string]> {
const [, addonId, addonName, addonFieldTitle] = await apiUtils.createProductAddon(payloads.createProductAddons(), payloads.adminAuth);
await dbUtils.updateCell(addonId, VENDOR_ID);
console.log(addonId, addonName, addonFieldTitle);
return [addonId, addonName, addonFieldTitle];
}

test.beforeAll(async ({ browser }) => {
const vendorContext = await browser.newContext(data.auth.vendorAuth);
vPage = await vendorContext.newPage();
vendor = new ProductAddonsPage(vPage);

addonName = data.vendor.addon.randomName();
addonFieldTitle = data.vendor.addon.randomTitle();
await vendor.addAddon({ ...data.vendor.addon, name: addonName, titleRequired: addonFieldTitle });
apiUtils = new ApiUtils(await request.newContext());
[addonId, addonName, addonFieldTitle] = await createVendorProductAddon();
});

test.afterAll(async () => {
await apiUtils.deleteAllProductAddons(payloads.adminAuth);
await vPage.close();
await apiUtils.dispose();
});

test('vendor product addons menu page is rendering properly @pro @explo', async () => {
test('vendor product addons menu page is rendering properly @pro @exp @v', async () => {
await vendor.vendorProductAddonsSettingsRenderProperly();
});

test('vendor can add addons @pro', async () => {
test('vendor can add addons @pro @v', async () => {
await vendor.addAddon({ ...data.vendor.addon, name: data.vendor.addon.randomName() });
});

test('vendor can edit addon @pro', async () => {
test('vendor can edit addon @pro @v', async () => {
await vendor.editAddon({ ...data.vendor.addon, name: addonName, titleRequired: addonFieldTitle });
});

test('vendor can delete addon @pro', async () => {
test('vendor can delete addon @pro @v', async () => {
const [, addonName] = await createVendorProductAddon();
await vendor.deleteAddon({ ...data.vendor.addon, name: addonName });
});
});
6 changes: 3 additions & 3 deletions tests/pw/utils/apiEndPoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -538,10 +538,10 @@ export const endPoints = {

productAddons: {
getAllProductAddons: `${SERVER_URL}/wc-product-add-ons/v1/product-add-ons`,
getSingleProductAddon: (productId: string) => `${SERVER_URL}/wc-product-add-ons/v1/product-add-ons${productId}`,
getSingleProductAddon: (productId: string) => `${SERVER_URL}/wc-product-add-ons/v1/product-add-ons/${productId}`,
createProductAddon: `${SERVER_URL}/wc-product-add-ons/v1/product-add-ons`,
updateProductAddon: (productId: string) => `${SERVER_URL}/wc-product-add-ons/v1/product-add-ons${productId}`,
deleteProductAddon: (productId: string) => `${SERVER_URL}/wc-product-add-ons/v1/product-add-ons${productId}`,
updateProductAddon: (productId: string) => `${SERVER_URL}/wc-product-add-ons/v1/product-add-ons/${productId}`,
deleteProductAddon: (productId: string) => `${SERVER_URL}/wc-product-add-ons/v1/product-add-ons/${productId}`,
},
},

Expand Down
38 changes: 38 additions & 0 deletions tests/pw/utils/apiUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1789,4 +1789,42 @@ export class ApiUtils {

return orderDetails;
}

/**
* woocommerce product addon api methods
*/

// get all product addons
async getAllProductAddons(auth?: auth): Promise<responseBody> {
const [, responseBody] = await this.get(endPoints.wc.productAddons.getAllProductAddons, { headers: auth });
return responseBody;
}

// create product addon
async createProductAddon(payload: object, auth?: auth): Promise<[responseBody, string, string, string]> {
const [, responseBody] = await this.post(endPoints.wc.productAddons.createProductAddon, { data: payload, headers: auth });
const productAddonId = String(responseBody?.id);
const addonName = responseBody.name;
const addonFieldTitle = responseBody.fields[0].name;
return [responseBody, productAddonId, addonName, addonFieldTitle];
}

// delete product addon
async deleteProductAddon(productAddonId: string, auth?: auth): Promise<responseBody> {
const [, responseBody] = await this.delete(endPoints.wc.productAddons.deleteProductAddon(productAddonId), { headers: auth });
return responseBody;
}

// delete all product addons
async deleteAllProductAddons(auth?: auth): Promise<responseBody> {
const allProductAddons = await this.getAllProductAddons(auth);
if (!allProductAddons?.length) {
console.log('No product addon exists');
return;
}
const allProductAddonIds = allProductAddons.map((o: { id: unknown }) => o.id);
for (const productAddonId of allProductAddonIds) {
await this.deleteProductAddon(productAddonId, auth);
}
}
}
8 changes: 8 additions & 0 deletions tests/pw/utils/dbUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,12 @@ export const dbUtils = {
// console.log(res);
return [res, refundId];
},

// update cell
async updateCell(id: any, value: any): Promise<any> {
const queryUpdate = `UPDATE ${dbPrefix}_posts SET post_author = '${value}' WHERE ID = '${id}';`;
const res = await dbUtils.dbQuery(queryUpdate);
// console.log(res);
return res;
},
};
12 changes: 9 additions & 3 deletions tests/pw/utils/payloads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -484,8 +484,8 @@ export const payloads = {
}),

createProductAddons: () => ({
name: 'Test Addons Group_1' + faker.string.nanoid(10),
priority: 1,
name: 'Test Addons Group_' + faker.string.nanoid(10),
priority: 10,
restrict_to_categories: [],
fields: [
{
Expand All @@ -507,7 +507,13 @@ export const payloads = {
options: [
{
label: 'Option 1',
price: '30',
price: '10',
image: '',
price_type: 'flat_fee',
},
{
label: 'Option 2',
price: '20',
image: '',
price_type: 'flat_fee',
},
Expand Down

0 comments on commit 0420dd8

Please sign in to comment.