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 */
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)
}
});
});
};