wip: optimized steps

This commit is contained in:
rodel 2021-03-23 04:57:29 -07:00
parent 6f67a3a5b3
commit 56bd958dde
1 changed files with 62 additions and 43 deletions

View File

@ -7,13 +7,13 @@ const {
verifyPublishAndTrash,
uiUnblocked
} = require('@woocommerce/e2e-utils');
const faker = require('faker');
const config = require('config');
const { HTTPClientFactory,
VariableProduct,
GroupedProduct,
SimpleProduct,
ProductVariation
ProductVariation,
ExternalProduct
} = require('@woocommerce/api');
const runCreateOrderTest = () => {
@ -46,84 +46,103 @@ const runCreateOrderTest = () => {
// todo remove .only
it('can create new complex order with multiple product types & tax classes', async () => {
// Initialize different product types
// Initialize products for each product type
const variations = config.get('products.variations');;
const initProducts = 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();
const productName = () => faker.commerce.productName();
const price = () => faker.commerce.price(1, 999);
// Initialize repositories
const simpleRepo = SimpleProduct.restRepository(httpClient);
const variationsRepo = ProductVariation.restRepository(httpClient);
const variableRepo = VariableProduct.restRepository(httpClient);
const groupedRepo = GroupedProduct.restRepository(httpClient);
// Initialization functions per product type
const initSimpleProduct = async () => {
const repo = SimpleProduct.restRepository(httpClient);
const props = config.get('products.simple');
return await repo.create(props);
};
const initVariableProduct = async () => {
const prodProps = config.get('products.variable');
const productRepo = VariableProduct.restRepository(httpClient);
const variationRepo = ProductVariation.restRepository(httpClient);
const variableProduct = await productRepo.create(prodProps);
for (const v of variations) {
await variationRepo.create(variableProduct.id, v);
}
// Initialize Simple Product
const simpleProduct = await simpleRepo.create({ name: productName(), price: price() });
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);
};
// Initialize Variable Product
const variations = config.get('products.variations');
const variableProduct = await variableRepo.create({ name: productName() });
await variationsRepo.create(variableProduct.id, variations)
// Initialize Grouped Products
const groupSize = 3;
const groupedProducts = [];
for (let i = 0; i < groupSize; i++) {
const prod = await simpleRepo.create({ name: productName(), price: price() });
groupedProducts.push(prod)
}
const groupedProduct = await groupedRepo.create({
name: productName(),
groupedProducts: groupedProducts
});
// 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
groupedProduct,
externalProduct
];
}
};
const products = await initProducts();
// Go to "add order" page
await merchant.openNewOrder();
// Add products (line items) to the order
// Open modal window for adding line items
await expect(page).toClick('button.add-line-item');
await expect(page).toClick('button.add-order-item');
await page.waitForSelector('.wc-backbone-modal-header');
for (const { name, id } of products) {
// Search for each product to add, then verify that they are saved
for (const { name } of products) {
await expect(page).toClick('.wc-backbone-modal-content tr:last-child .wc-product-search');
await expect(page).toFill('#wc-backbone-modal-dialog + .select2-container .select2-search__field', name);
await expect(page).toClick('li[aria-selected="true"]');
await expect(page).toFill('#wc-backbone-modal-dialog + .select2-container .select2-search__field',
name
);
const firstResult = await page.waitForSelector('li[data-selected]');
await firstResult.click();
await expect(page).toMatchElement(
'.wc-backbone-modal-content tr:nth-last-child(2) .wc-product-search option',
`${name} (#${id})`
name
);
}
await page.click('.wc-backbone-modal-content #btn-ok');
// Save the line items, then verify
await expect(page).toClick('.wc-backbone-modal-content #btn-ok');
await uiUnblocked();
// Verify the products we added show as line items now
await expect(page).toClick('button.save_order');
await page.waitForNavigation();
for (const { name } of products) {
await expect(page).toMatchElement('.wc-order-item-name', { text: name });
}
// Verify variation details in variable product line item
await expect(page).toMatchElement('.wc-order-item-variation', { text: products.filter(p => p.variations).id })
// Verify variation details
const lastVariation = variations[0];
const attributes = lastVariation.attributes;
const actualAttributes = await page.$('.display_meta');
await expect(page).toMatchElement('.wc-order-item-variation', { text: lastVariation.id });
for (const { name, option } of attributes) {
await expect(actualAttributes).toMatch(name);
await expect(actualAttributes).toMatch(option);
}
// todo specify quantities
// todo setup tax classes
})
});
}