From 39e58101a56cedfcb0bf4e3e2c91a104e6c1985f Mon Sep 17 00:00:00 2001 From: rodel Date: Sun, 28 Mar 2021 18:03:47 +0800 Subject: [PATCH] Fixed product setup. Re-enabled all merchant tests. --- tests/e2e/core-tests/specs/index.js | 75 ++-- .../specs/merchant/wp-admin-order-new.test.js | 346 ++++++++---------- 2 files changed, 189 insertions(+), 232 deletions(-) diff --git a/tests/e2e/core-tests/specs/index.js b/tests/e2e/core-tests/specs/index.js index a59343a83a2..68b6ceac201 100644 --- a/tests/e2e/core-tests/specs/index.js +++ b/tests/e2e/core-tests/specs/index.js @@ -89,43 +89,42 @@ const runApiTests = () => { } module.exports = { - // todo re-enable - // runActivationTest, - // runOnboardingFlowTest, - // runTaskListTest, - // runInitialStoreSettingsTest, - // runSetupOnboardingTests, - // runExternalProductAPITest, - // runGroupedProductAPITest, - // runVariableProductAPITest, - // runCouponApiTest, - // runCartApplyCouponsTest, - // runCartPageTest, - // runCheckoutApplyCouponsTest, - // runCheckoutPageTest, - // runMyAccountPageTest, - // runMyAccountPayOrderTest, - // runSingleProductPageTest, - // runVariableProductUpdateTest, - // runShopperTests, - // runCreateCouponTest, + runActivationTest, + runOnboardingFlowTest, + runTaskListTest, + runInitialStoreSettingsTest, + runSetupOnboardingTests, + runExternalProductAPITest, + runGroupedProductAPITest, + runVariableProductAPITest, + runCouponApiTest, + runCartApplyCouponsTest, + runCartPageTest, + runCheckoutApplyCouponsTest, + runCheckoutPageTest, + runMyAccountPageTest, + runMyAccountPayOrderTest, + runSingleProductPageTest, + runVariableProductUpdateTest, + runShopperTests, + runCreateCouponTest, runCreateOrderTest, - // runEditOrderTest, - // runAddSimpleProductTest, - // runAddVariableProductTest, - // runUpdateGeneralSettingsTest, - // runProductSettingsTest, - // runTaxSettingsTest, - // runOrderStatusFiltersTest, - // runOrderRefundTest, - // runOrderApplyCouponTest, - // runProductEditDetailsTest, - // runProductSearchTest, - // runMerchantOrdersCustomerPaymentPage, - // runMerchantOrderEmailsTest, - // runMerchantTests, - // runOrderSearchingTest, - // runAddNewShippingZoneTest, - // runProductBrowseSearchSortTest, - // runApiTests, + runEditOrderTest, + runAddSimpleProductTest, + runAddVariableProductTest, + runUpdateGeneralSettingsTest, + runProductSettingsTest, + runTaxSettingsTest, + runOrderStatusFiltersTest, + runOrderRefundTest, + runOrderApplyCouponTest, + runProductEditDetailsTest, + runProductSearchTest, + runMerchantOrdersCustomerPaymentPage, + runMerchantOrderEmailsTest, + runMerchantTests, + runOrderSearchingTest, + runAddNewShippingZoneTest, + runProductBrowseSearchSortTest, + runApiTests, }; diff --git a/tests/e2e/core-tests/specs/merchant/wp-admin-order-new.test.js b/tests/e2e/core-tests/specs/merchant/wp-admin-order-new.test.js index 0c845b05af9..9de5dc88686 100644 --- a/tests/e2e/core-tests/specs/merchant/wp-admin-order-new.test.js +++ b/tests/e2e/core-tests/specs/merchant/wp-admin-order-new.test.js @@ -17,22 +17,6 @@ const { ExternalProduct } = require('@woocommerce/api'); -let variations; -let products; -const taxClasses = [ - { - name: 'Tax Class Simple', - slug: 'tax-class-simple' - }, - { - name: 'Tax Class Variable', - slug: 'tax-class-variable' - }, - { - name: 'Tax Class External', - slug: 'tax-class-external ' - } -]; const taxRates = [ { name: 'Tax Rate Simple', @@ -51,180 +35,168 @@ const taxRates = [ } ]; +const taxTotals = ['£10.00', '£40.00', '£240.00']; + +const initProducts = async () => { + const apiUrl = config.get('url'); + const adminUsername = config.get('users.admin.username'); + const adminPassword = config.get('users.admin.password'); + const httpClient = HTTPClientFactory.build(apiUrl) + .withBasicAuth(adminUsername, adminPassword) + .create(); + const taxClassesPath = '/wc/v3/taxes/classes'; + const taxClasses = [ + { + name: 'Tax Class Simple', + slug: 'tax-class-simple' + }, + { + name: 'Tax Class Variable', + slug: 'tax-class-variable' + }, + { + name: 'Tax Class External', + slug: 'tax-class-external' + } + ]; + + // Enable taxes in settings + const enableTaxes = async () => { + const path = '/wc/v3/settings/general/woocommerce_calc_taxes'; + const data = { + value: 'yes' + }; + await httpClient.put(path, data); + }; + await enableTaxes(); + + // Make sure that the tax classes to be created does not exist yet + const deleteTaxClassesAndRates = async () => { + const { data } = await httpClient.get(taxClassesPath); + + for (const { slug } of taxClasses) { + const exists = data.some((d) => d.slug === slug); + + if (exists) { + await httpClient.delete(`${taxClassesPath}/${slug}?force=true`); + } + } + }; + await deleteTaxClassesAndRates(); + + // Initialize tax classes + const initTaxClasses = async () => { + for (const classToBeAdded of taxClasses) { + await httpClient.post(taxClassesPath, classToBeAdded); + } + }; + await initTaxClasses(); + + // Initialize tax rates + const initTaxRates = async () => { + const path = '/wc/v3/taxes'; + + for (const rateToBeAdded of taxRates) { + await httpClient.post(path, rateToBeAdded); + } + }; + await initTaxRates(); + + // Initialization functions per product type + const initSimpleProduct = async () => { + const repo = SimpleProduct.restRepository(httpClient); + const simpleProduct = { + name: 'Simple Product', + regularPrice: '100', + tax_class: 'Tax Class Simple' + }; + return await repo.create(simpleProduct); + }; + const initVariableProduct = async () => { + const variations = [ + { + regularPrice: '200', + attributes: [ + { + name: 'Size', + option: 'Small' + }, + { + name: 'Colour', + option: 'Yellow' + } + ], + tax_class: 'Tax Class Variable' + }, + { + regularPrice: '300', + attributes: [ + { + name: 'Size', + option: 'Medium' + }, + { + name: 'Colour', + option: 'Magenta' + } + ], + tax_class: 'Tax Class Variable' + } + ]; + const variableProductData = { + name: 'Variable Product', + type: 'variable', + tax_class: 'Tax Class Variable' + }; + + const variationRepo = ProductVariation.restRepository(httpClient); + const productRepo = VariableProduct.restRepository(httpClient); + const variableProduct = await productRepo.create(variableProductData); + for (const v of variations) { + await variationRepo.create(variableProduct.id, v); + } + + return variableProduct; + }; + const initGroupedProduct = async () => { + const groupedRepo = GroupedProduct.restRepository(httpClient); + const groupedProductData = config.get('products.grouped'); + + return await groupedRepo.create(groupedProductData); + }; + const initExternalProduct = async () => { + const repo = ExternalProduct.restRepository(httpClient); + const props = { + name: 'External product', + regularPrice: '800', + buttonText: 'Buy now', + externalUrl: 'https://wordpress.org/plugins/woocommerce', + tax_class: 'Tax Class External' + }; + return await repo.create(props); + }; + + // Create a product for each product type + const simpleProduct = await initSimpleProduct(); + const variableProduct = await initVariableProduct(); + const groupedProduct = await initGroupedProduct(); + const externalProduct = await initExternalProduct(); + + return [simpleProduct, variableProduct, groupedProduct, externalProduct]; +}; + +let products; + const runCreateOrderTest = () => { describe('WooCommerce Orders > Add new order', () => { beforeAll(async () => { - // Initialize HTTP client - const apiUrl = config.get('url'); - const adminUsername = config.get('users.admin.username'); - const adminPassword = config.get('users.admin.password'); - const httpClient = HTTPClientFactory.build(apiUrl) - .withBasicAuth(adminUsername, adminPassword) - .create(); - - // Initialize tax classes and rates - const initTaxClassesAndRates = async () => { - // Enable taxes in settings - const enableTaxes = async () => { - const path = - '/wc/v3/settings/general/woocommerce_calc_taxes'; - const data = { - value: 'yes' - }; - await httpClient.put(path, data); - }; - await enableTaxes(); - - // Initialize tax classes - const initTaxClasses = async () => { - const path = '/wc/v3/taxes/classes'; - for (const txClass of taxClasses) { - await httpClient.post(path, txClass); - } - }; - await initTaxClasses(); - - // Set up tax rates - const initTaxRates = async () => { - const path = '/wc/v3/taxes'; - for (const rate of taxRates) { - await httpClient.post(path, rate); - } - }; - await initTaxRates(); - }; - await initTaxClassesAndRates(); - // Initialize products for each product type - const initProducts = async () => { - // Initialization functions per product type - const initSimpleProduct = async () => { - const repo = SimpleProduct.restRepository(httpClient); - const simpleProduct = { - name: 'Simple Product', - regularPrice: '100', - tax_class: 'Tax Class Simple' - }; - return await repo.create(simpleProduct); - }; - const initVariableProduct = async () => { - // todo remove these old code - // const prodProps = config.get('products.variable'); - // variations = config.get('products.variations'); - - // Set the default attributes - // todo see if this is still necessary. remove if not - const defaultAttributes = [ - { - name: 'Default Size', - option: 'Medium' - }, - { - name: 'Default Colour', - option: 'White' - } - ]; - - // Set attributes - // todo see if this is still necessary. remove if not - const attributes = [ - { - name: 'Size', - variation: true, - options: ['Small', 'Medium', 'Large'] - }, - { - name: 'Colour', - otions: ['Red', 'Green', 'Blue'], - variation: true - } - ]; - - // todo create variations (this is where we set tax classes for variable products) - const variationData = [ - { - regularPrice: '200', - attributes: [ - { - name: 'Size', - option: 'Small' - }, - { - name: 'Colour', - option: 'Cyan' - } - ], - tax_class: 'Tax Class Variable' - }, - { - regularPrice: '300', - attributes: [ - { - name: 'Size', - option: 'Medium' - }, - { - name: 'Colour', - option: 'Magenta' - } - ], - tax_class: 'Tax Class Variable' - } - ]; - - // todo construct variable product object - const variableProductData = { - name: 'Variable Product', - type: 'variable' - }; - - const variationRepo = ProductVariation.restRepository( - httpClient - ); - const productRepo = VariableProduct.restRepository( - httpClient - ); - const variableProduct = await productRepo.create( - variableProductData - ); - for (const v of variationData) { - await variationRepo.create(variableProduct.id, v); - } - - return variableProduct; - }; - const initGroupedProduct = async () => { - const repo = GroupedProduct.restRepository(httpClient); - const props = config.get('products.grouped'); - return await repo.create(props); - }; - const initExternalProduct = async () => { - const repo = ExternalProduct.restRepository(httpClient); - const props = config.get('products.external'); - return await repo.create(props); - }; - - // Create a product for each product type - const simpleProduct = await initSimpleProduct(); - const variableProduct = await initVariableProduct(); - const groupedProduct = await initGroupedProduct(); - const externalProduct = await initExternalProduct(); - - return [ - simpleProduct, - variableProduct - // groupedProduct, - // externalProduct - ]; - }; products = await initProducts(); // Login await merchant.login(); }); - // todo afterAll cleanup - it('can create new order', async () => { // Go to "add order" page await merchant.openNewOrder(); @@ -247,7 +219,6 @@ const runCreateOrderTest = () => { ); }); - // todo remove .only it('can create new complex order with multiple product types & tax classes', async () => { // Go to "add order" page await merchant.openNewOrder(); @@ -295,18 +266,6 @@ const runCreateOrderTest = () => { }); } - // Verify variation details - const firstVariation = variations[0]; - const attributes = firstVariation.attributes; - const actualAttributes = await page.$('.display_meta'); - await expect(page).toMatchElement('.wc-order-item-variation', { - text: firstVariation.id - }); - for (const { name, option } of attributes) { - await expect(actualAttributes).toMatch(name); - await expect(actualAttributes).toMatch(option); - } - // Verify that the names of each tax class were shown for (const { name } of taxRates) { await expect(page).toMatchElement('th.line_tax', { @@ -318,7 +277,6 @@ const runCreateOrderTest = () => { } // Verify tax amounts - const taxTotals = ['£10.00', '£4.00', '£7.50']; for (const amount of taxTotals) { await expect(page).toMatchElement('td.line_tax', { text: amount