simplified steps and functions

This commit is contained in:
rodel 2021-03-23 09:56:39 -07:00
parent a17a629f3f
commit 1d91947953
1 changed files with 33 additions and 74 deletions

View File

@ -1,58 +1,13 @@
/* eslint-disable jest/no-export, jest/no-disabled-tests */ /* eslint-disable jest/no-export*/
const faker = require("faker");
/** /**
* Internal dependencies * Internal dependencies
*/ */
const { merchant } = require('@woocommerce/e2e-utils'); const { merchant } = require('@woocommerce/e2e-utils');
const { lorem, helpers } = require("faker");
/**
* 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 runAddShippingClassesTest = () => { const runAddShippingClassesTest = () => {
describe('Merchant can add a shipping class', () => { describe('Merchant can add shipping classes', () => {
beforeAll(async () => { beforeAll(async () => {
await merchant.login(); await merchant.login();
@ -60,34 +15,38 @@ const runAddShippingClassesTest = () => {
await merchant.openSettings('shipping', 'classes'); await merchant.openSettings('shipping', 'classes');
}); });
it('can add a new shipping class', async () => { it('can add shipping classes', async () => {
const shippingClass = generateShippingClassesTestData() const shippingClassSlug = {
name: lorem.words(),
await addShippingClasses(shippingClass) slug: lorem.slug(),
await verifySavedShippingClasses(shippingClass) description: lorem.sentence()
}); }
const shippingClassNoSlug = {
it('can add multiple shipping classes at once', async () => { name: lorem.words(3),
const shippingClasses = generateShippingClassesTestData(2)
await addShippingClasses(shippingClasses)
await verifySavedShippingClasses(shippingClasses)
});
it('can automatically generate slug', async () => {
const input = [{
name: faker.lorem.words(3),
slug: '', slug: '',
description: '' description: lorem.sentence()
}] }
const expectedShippingClass = [{ const shippingClasses = [shippingClassSlug, shippingClassNoSlug]
name: input.name,
slug: faker.helpers.slugify(input.name),
description: ''
}]
await addShippingClasses(input) // Add shipping classes
await verifySavedShippingClasses(expectedShippingClass) 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)
}
}); });
}); });
}; };