From 1d9194795327096eaa5fc57015566c8d0c3c48c3 Mon Sep 17 00:00:00 2001 From: rodel Date: Tue, 23 Mar 2021 09:56:39 -0700 Subject: [PATCH] simplified steps and functions --- ...wp-admin-settings-shipping-classes.test.js | 107 ++++++------------ 1 file changed, 33 insertions(+), 74 deletions(-) diff --git a/tests/e2e/core-tests/specs/merchant/wp-admin-settings-shipping-classes.test.js b/tests/e2e/core-tests/specs/merchant/wp-admin-settings-shipping-classes.test.js index cc14307ad0e..0b1cc56d427 100644 --- a/tests/e2e/core-tests/specs/merchant/wp-admin-settings-shipping-classes.test.js +++ b/tests/e2e/core-tests/specs/merchant/wp-admin-settings-shipping-classes.test.js @@ -1,58 +1,13 @@ -/* eslint-disable jest/no-export, jest/no-disabled-tests */ - -const faker = require("faker"); +/* eslint-disable jest/no-export*/ /** * Internal dependencies */ const { merchant } = require('@woocommerce/e2e-utils'); - -/** - * Add shipping class(es) by filling up and submitting the 'Add shipping class' form - */ -const addShippingClasses = async (shippingClasses) => { - for (const { name, slug, description } of shippingClasses) { - await expect(page).toClick('.wc-shipping-class-add') - await expect(page).toFill('.editing:last-child [data-attribute="name"]', name) - await expect(page).toFill('.editing:last-child [data-attribute="slug"]', slug) - await expect(page).toFill('.editing:last-child [data-attribute="description"]', description) - } - - await expect(page).toClick('.wc-shipping-class-save') -} - -/** - * Verify that the specified shipping classes were saved - */ -const verifySavedShippingClasses = async (savedShippingClasses) => { - for (const { name, slug, description } of savedShippingClasses) { - const row = await expect(page).toMatchElement('.wc-shipping-class-rows tr', { text: slug }) - - await expect(row).toMatchElement('.wc-shipping-class-name', name) - await expect(row).toMatchElement('.wc-shipping-class-slug', slug) - await expect(row).toMatchElement('.wc-shipping-class-description', description) - } -} - -/** - * Generate an array of shipping class objects to be used as test data. - */ -const generateShippingClassesTestData = (count = 1) => { - const shippingClasses = [] - - while (count--) { - shippingClasses.push({ - name: faker.lorem.words(), - slug: faker.lorem.slug(), - description: faker.lorem.sentence() - }) - } - - return shippingClasses -} +const { lorem, helpers } = require("faker"); const runAddShippingClassesTest = () => { - describe('Merchant can add a shipping class', () => { + describe('Merchant can add shipping classes', () => { beforeAll(async () => { await merchant.login(); @@ -60,34 +15,38 @@ const runAddShippingClassesTest = () => { await merchant.openSettings('shipping', 'classes'); }); - it('can add a new shipping class', async () => { - const shippingClass = generateShippingClassesTestData() - - await addShippingClasses(shippingClass) - await verifySavedShippingClasses(shippingClass) - }); - - it('can add multiple shipping classes at once', async () => { - const shippingClasses = generateShippingClassesTestData(2) - - await addShippingClasses(shippingClasses) - await verifySavedShippingClasses(shippingClasses) - }); - - it('can automatically generate slug', async () => { - const input = [{ - name: faker.lorem.words(3), + it('can add shipping classes', async () => { + const shippingClassSlug = { + name: lorem.words(), + slug: lorem.slug(), + description: lorem.sentence() + } + const shippingClassNoSlug = { + name: lorem.words(3), slug: '', - description: '' - }] - const expectedShippingClass = [{ - name: input.name, - slug: faker.helpers.slugify(input.name), - description: '' - }] + description: lorem.sentence() + } + const shippingClasses = [shippingClassSlug, shippingClassNoSlug] - await addShippingClasses(input) - await verifySavedShippingClasses(expectedShippingClass) + // Add shipping classes + for (const { name, slug, description } of shippingClasses) { + await expect(page).toClick('.wc-shipping-class-add') + await expect(page).toFill('.editing:last-child [data-attribute="name"]', name) + await expect(page).toFill('.editing:last-child [data-attribute="slug"]', slug) + await expect(page).toFill('.editing:last-child [data-attribute="description"]', description) + } + await expect(page).toClick('.wc-shipping-class-save') + + // Set the expected auto-generated slug + shippingClassNoSlug.slug = helpers.slugify(shippingClassNoSlug.name) + + // Verify that the specified shipping classes were saved + for (const { name, slug, description } of shippingClasses) { + const row = await expect(page).toMatchElement('.wc-shipping-class-rows tr', { text: slug }) + + await expect(row).toMatchElement('.wc-shipping-class-name', name) + await expect(row).toMatchElement('.wc-shipping-class-description', description) + } }); }); };