Use factories object to create variable products

This commit is contained in:
Rodel Calasagsag 2021-06-02 21:12:31 +08:00
parent 0a6d38fc6b
commit 46b5cf8803
5 changed files with 78 additions and 17 deletions

View File

@ -14,13 +14,14 @@ const {
const config = require( 'config' );
// Variables for simple product
let simplePostIdValue;
const simpleProductName = config.get( 'products.simple.name' );
let simplePostIdValue;
// Variables for variable product
let variableProduct;
const variableProductName = config.get( 'products.variable.name' );
const variations = config.get( 'products.variations' );
const attributes = variations[0].attributes;
let variableProductId;
// Variables for grouped product
let groupedPostIdValue;
@ -53,12 +54,12 @@ const runSingleProductPageTest = () => {
describe('Variable Product Page', () => {
beforeAll(async () => {
variableProduct = await createVariableProduct();
variableProductId = await createVariableProduct();
});
it('should be able to add variation products to the cart', async () => {
// Add a product with one set of variations to cart
await shopper.goToProduct( variableProduct.id );
await shopper.goToProduct( variableProductId );
for( const attr of attributes ){
const selectElem = `#${attr.name.toLowerCase()}`;
@ -72,12 +73,12 @@ const runSingleProductPageTest = () => {
// Verify cart contents
await shopper.goToCart();
await shopper.productIsInCart(variableProduct.name);
await shopper.productIsInCart(variableProductName);
});
it('should be able to remove variation products from the cart', async () => {
// Remove items from cart
await shopper.removeFromCart(variableProduct.name);
await shopper.removeFromCart(variableProductName);
await uiUnblocked();
await expect(page).toMatchElement('.cart-empty', {text: 'Your cart is currently empty.'});
});

View File

@ -18,7 +18,8 @@ import {
waitForSelectorWithoutThrow,
} from './page-utils';
import factories from './factories';
import { Coupon, VariableProduct, ProductVariation } from '@woocommerce/api';
import { Coupon, ProductVariation } from '@woocommerce/api';
import { variableProductFactory } from './factories/variable-product';
const client = factories.api.withDefaultPermalinks;
const config = require( 'config' );
@ -221,22 +222,21 @@ const createSimpleProductWithCategory = async ( productName, productPrice, categ
/**
* Create variable product.
*
* @returns the ID of the variable product
*/
const createVariableProduct = async () => {
// mytodo convert to factory
// Create a variable product with attributes (no variations yet)
const defaultVariableProduct = config.get( 'products.variable' );
const variableProductRepo = VariableProduct.restRepository( client );
const variableProduct = await variableProductRepo.create( defaultVariableProduct );
// Create variations for this product
const variableProduct = await factories.products.variable.create();
const defaultVariations = config.get( 'products.variations' );
const variationsRepo = ProductVariation.restRepository( client );
for( const variation of defaultVariations ){
await variationsRepo.create( variableProduct.id, variation );
await factories.products.variation.create({
productId: variableProduct.id,
variation: variation
});
}
return variableProduct;
return variableProduct.id;
};
/**

View File

@ -1,6 +1,8 @@
import { HTTPClientFactory } from '@woocommerce/api';
const config = require( 'config' );
import { simpleProductFactory } from './factories/simple-product';
import { variableProductFactory } from './factories/variable-product';
import { variationFactory } from './factories/variation';
const apiUrl = config.get( 'url' );
const adminUsername = config.get( 'users.admin.username' );
@ -20,6 +22,8 @@ const factories = {
},
products: {
simple: simpleProductFactory( withDefaultPermalinks ),
variable: variableProductFactory( withDefaultPermalinks ),
variation: variationFactory( withDefaultPermalinks )
},
};

View File

@ -0,0 +1,28 @@
import { VariableProduct } from '@woocommerce/api';
import { Factory } from 'fishery';
import config from 'config';
/**
* Creates a new factory for creating variable products.
* This factory will create a default variable product when `create()` is called without parameters.
*
* @param {HTTPClient} httpClient The HTTP client we will give the repository.
* @return {AsyncFactory} The factory for creating models.
*/
export function variableProductFactory(httpClient) {
const repository = VariableProduct.restRepository(httpClient);
const defaultVariableProduct = config.get('products.variable');
return Factory.define(({ params, onCreate }) => {
onCreate((model) => {
return repository.create(model);
});
return {
name: params.name ?? defaultVariableProduct.name,
type: 'variable',
defaultAttributes: params.defaultAttributes ?? defaultVariableProduct.defaultAttributes,
attributes: params.attributes ?? defaultVariableProduct.attributes
};
});
}

View File

@ -0,0 +1,28 @@
import { ProductVariation } from '@woocommerce/api';
import { Factory } from 'fishery';
import config from 'config';
/**
* Creates a new factory for creating a product variation.
*
* @param {HTTPClient} httpClient The HTTP client we will give the repository.
* @return {AsyncFactory} The factory for creating models.
*/
export function variationFactory(httpClient) {
const repository = ProductVariation.restRepository(httpClient);
const defaultVariations = config.get('products.variations');
const defaultVariation = defaultVariations[0];
return Factory.define(({ params, onCreate }) => {
const { productId, variation } = params;
onCreate((model) => {
return repository.create(productId, model);
});
return {
regularPrice: variation.regularPrice ?? defaultVariation.regularPrice,
attributes: variation.attributes ?? defaultVariation.attributes
};
});
}