diff --git a/src/products/components/ProductVariantCreatorPage/ProductVariantCreator.stories.tsx b/src/products/components/ProductVariantCreatorPage/ProductVariantCreator.stories.tsx index 73fa3367a..5b8a3e0e8 100644 --- a/src/products/components/ProductVariantCreatorPage/ProductVariantCreator.stories.tsx +++ b/src/products/components/ProductVariantCreatorPage/ProductVariantCreator.stories.tsx @@ -28,20 +28,17 @@ const price: AllOrAttribute = { })) }; -const stock: AllOrAttribute = { +const stock: AllOrAttribute = { all: false, attribute: selectedAttributes[0].id, - value: selectedWarehouses.map((_, warehouseIndex) => - ((warehouseIndex + 2) * 3).toString() + value: selectedWarehouses.map( + (_, warehouseIndex) => (warehouseIndex + 2) * 3 ), values: selectedAttributes[0].values.map((attribute, attributeIndex) => ({ slug: attribute.slug, - value: selectedWarehouses.map((_, warehouseIndex) => - ( - selectedAttributes.length * 10 - - attributeIndex - - warehouseIndex * 3 - ).toString() + value: selectedWarehouses.map( + (_, warehouseIndex) => + selectedAttributes.length * 10 - attributeIndex - warehouseIndex * 3 ) })) }; diff --git a/src/products/components/ProductVariantCreatorPage/__snapshots__/reducer.test.ts.snap b/src/products/components/ProductVariantCreatorPage/__snapshots__/reducer.test.ts.snap index 22a84acfc..b2b9f6af8 100644 --- a/src/products/components/ProductVariantCreatorPage/__snapshots__/reducer.test.ts.snap +++ b/src/products/components/ProductVariantCreatorPage/__snapshots__/reducer.test.ts.snap @@ -1,5 +1,829 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`Reducer is able to modify individual variant price 1`] = ` +Object { + "attributes": Array [ + Object { + "id": "attr-1", + "values": Array [ + "val-1-1", + "val-1-7", + ], + }, + Object { + "id": "attr-2", + "values": Array [ + "val-2-2", + "val-2-4", + ], + }, + Object { + "id": "attr-4", + "values": Array [ + "val-4-1", + "val-4-5", + ], + }, + ], + "price": Object { + "all": false, + "attribute": "attr-2", + "value": "", + "values": Array [ + Object { + "slug": "val-2-2", + "value": "24.99", + }, + Object { + "slug": "val-2-4", + "value": "26.99", + }, + ], + }, + "stock": Object { + "all": false, + "attribute": "attr-4", + "value": Array [], + "values": Array [ + Object { + "slug": "val-4-1", + "value": Array [ + 50, + 20, + 45, + 75, + ], + }, + Object { + "slug": "val-4-5", + "value": Array [ + 80, + 50, + 85, + 105, + ], + }, + ], + }, + "variants": Array [ + Object { + "attributes": Array [ + Object { + "id": "attr-1", + "values": Array [ + "val-1-1", + ], + }, + Object { + "id": "attr-2", + "values": Array [ + "val-2-2", + ], + }, + Object { + "id": "attr-4", + "values": Array [ + "val-4-1", + ], + }, + ], + "priceOverride": "24.99", + "sku": "", + "stocks": Array [ + Object { + "quantity": 50, + "warehouse": "wh-1", + }, + Object { + "quantity": 20, + "warehouse": "wh-2", + }, + Object { + "quantity": 45, + "warehouse": "wh-3", + }, + Object { + "quantity": 75, + "warehouse": "wh-4", + }, + ], + }, + Object { + "attributes": Array [ + Object { + "id": "attr-1", + "values": Array [ + "val-1-1", + ], + }, + Object { + "id": "attr-2", + "values": Array [ + "val-2-2", + ], + }, + Object { + "id": "attr-4", + "values": Array [ + "val-4-5", + ], + }, + ], + "priceOverride": "24.99", + "sku": "", + "stocks": Array [ + Object { + "quantity": 80, + "warehouse": "wh-1", + }, + Object { + "quantity": 50, + "warehouse": "wh-2", + }, + Object { + "quantity": 85, + "warehouse": "wh-3", + }, + Object { + "quantity": 105, + "warehouse": "wh-4", + }, + ], + }, + Object { + "attributes": Array [ + Object { + "id": "attr-1", + "values": Array [ + "val-1-1", + ], + }, + Object { + "id": "attr-2", + "values": Array [ + "val-2-4", + ], + }, + Object { + "id": "attr-4", + "values": Array [ + "val-4-1", + ], + }, + ], + "priceOverride": "26.99", + "sku": "", + "stocks": Array [ + Object { + "quantity": 50, + "warehouse": "wh-1", + }, + Object { + "quantity": 20, + "warehouse": "wh-2", + }, + Object { + "quantity": 45, + "warehouse": "wh-3", + }, + Object { + "quantity": 75, + "warehouse": "wh-4", + }, + ], + }, + Object { + "attributes": Array [ + Object { + "id": "attr-1", + "values": Array [ + "val-1-1", + ], + }, + Object { + "id": "attr-2", + "values": Array [ + "val-2-4", + ], + }, + Object { + "id": "attr-4", + "values": Array [ + "val-4-5", + ], + }, + ], + "priceOverride": "49.99", + "sku": "", + "stocks": Array [ + Object { + "quantity": 80, + "warehouse": "wh-1", + }, + Object { + "quantity": 50, + "warehouse": "wh-2", + }, + Object { + "quantity": 85, + "warehouse": "wh-3", + }, + Object { + "quantity": 105, + "warehouse": "wh-4", + }, + ], + }, + Object { + "attributes": Array [ + Object { + "id": "attr-1", + "values": Array [ + "val-1-7", + ], + }, + Object { + "id": "attr-2", + "values": Array [ + "val-2-2", + ], + }, + Object { + "id": "attr-4", + "values": Array [ + "val-4-1", + ], + }, + ], + "priceOverride": "24.99", + "sku": "", + "stocks": Array [ + Object { + "quantity": 50, + "warehouse": "wh-1", + }, + Object { + "quantity": 20, + "warehouse": "wh-2", + }, + Object { + "quantity": 45, + "warehouse": "wh-3", + }, + Object { + "quantity": 75, + "warehouse": "wh-4", + }, + ], + }, + Object { + "attributes": Array [ + Object { + "id": "attr-1", + "values": Array [ + "val-1-7", + ], + }, + Object { + "id": "attr-2", + "values": Array [ + "val-2-2", + ], + }, + Object { + "id": "attr-4", + "values": Array [ + "val-4-5", + ], + }, + ], + "priceOverride": "24.99", + "sku": "", + "stocks": Array [ + Object { + "quantity": 80, + "warehouse": "wh-1", + }, + Object { + "quantity": 50, + "warehouse": "wh-2", + }, + Object { + "quantity": 85, + "warehouse": "wh-3", + }, + Object { + "quantity": 105, + "warehouse": "wh-4", + }, + ], + }, + Object { + "attributes": Array [ + Object { + "id": "attr-1", + "values": Array [ + "val-1-7", + ], + }, + Object { + "id": "attr-2", + "values": Array [ + "val-2-4", + ], + }, + Object { + "id": "attr-4", + "values": Array [ + "val-4-1", + ], + }, + ], + "priceOverride": "26.99", + "sku": "", + "stocks": Array [ + Object { + "quantity": 50, + "warehouse": "wh-1", + }, + Object { + "quantity": 20, + "warehouse": "wh-2", + }, + Object { + "quantity": 45, + "warehouse": "wh-3", + }, + Object { + "quantity": 75, + "warehouse": "wh-4", + }, + ], + }, + Object { + "attributes": Array [ + Object { + "id": "attr-1", + "values": Array [ + "val-1-7", + ], + }, + Object { + "id": "attr-2", + "values": Array [ + "val-2-4", + ], + }, + Object { + "id": "attr-4", + "values": Array [ + "val-4-5", + ], + }, + ], + "priceOverride": "26.99", + "sku": "", + "stocks": Array [ + Object { + "quantity": 80, + "warehouse": "wh-1", + }, + Object { + "quantity": 50, + "warehouse": "wh-2", + }, + Object { + "quantity": 85, + "warehouse": "wh-3", + }, + Object { + "quantity": 105, + "warehouse": "wh-4", + }, + ], + }, + ], + "warehouses": Array [ + "wh-1", + "wh-2", + "wh-3", + "wh-4", + ], +} +`; + +exports[`Reducer is able to modify individual variant stock 1`] = ` +Object { + "attributes": Array [ + Object { + "id": "attr-1", + "values": Array [ + "val-1-1", + "val-1-7", + ], + }, + Object { + "id": "attr-2", + "values": Array [ + "val-2-2", + "val-2-4", + ], + }, + Object { + "id": "attr-4", + "values": Array [ + "val-4-1", + "val-4-5", + ], + }, + ], + "price": Object { + "all": false, + "attribute": "attr-2", + "value": "", + "values": Array [ + Object { + "slug": "val-2-2", + "value": "24.99", + }, + Object { + "slug": "val-2-4", + "value": "26.99", + }, + ], + }, + "stock": Object { + "all": false, + "attribute": "attr-4", + "value": Array [], + "values": Array [ + Object { + "slug": "val-4-1", + "value": Array [ + 50, + 20, + 45, + 75, + ], + }, + Object { + "slug": "val-4-5", + "value": Array [ + 80, + 50, + 85, + 105, + ], + }, + ], + }, + "variants": Array [ + Object { + "attributes": Array [ + Object { + "id": "attr-1", + "values": Array [ + "val-1-1", + ], + }, + Object { + "id": "attr-2", + "values": Array [ + "val-2-2", + ], + }, + Object { + "id": "attr-4", + "values": Array [ + "val-4-1", + ], + }, + ], + "priceOverride": "24.99", + "sku": "", + "stocks": Array [ + Object { + "quantity": 50, + "warehouse": "wh-1", + }, + Object { + "quantity": 20, + "warehouse": "wh-2", + }, + Object { + "quantity": 45, + "warehouse": "wh-3", + }, + Object { + "quantity": 75, + "warehouse": "wh-4", + }, + ], + }, + Object { + "attributes": Array [ + Object { + "id": "attr-1", + "values": Array [ + "val-1-1", + ], + }, + Object { + "id": "attr-2", + "values": Array [ + "val-2-2", + ], + }, + Object { + "id": "attr-4", + "values": Array [ + "val-4-5", + ], + }, + ], + "priceOverride": "24.99", + "sku": "", + "stocks": Array [ + Object { + "quantity": 80, + "warehouse": "wh-1", + }, + Object { + "quantity": 50, + "warehouse": "wh-2", + }, + Object { + "quantity": 85, + "warehouse": "wh-3", + }, + Object { + "quantity": 105, + "warehouse": "wh-4", + }, + ], + }, + Object { + "attributes": Array [ + Object { + "id": "attr-1", + "values": Array [ + "val-1-1", + ], + }, + Object { + "id": "attr-2", + "values": Array [ + "val-2-4", + ], + }, + Object { + "id": "attr-4", + "values": Array [ + "val-4-1", + ], + }, + ], + "priceOverride": "26.99", + "sku": "", + "stocks": Array [ + Object { + "quantity": 50, + "warehouse": "wh-1", + }, + Object { + "quantity": 20, + "warehouse": "wh-2", + }, + Object { + "quantity": 45, + "warehouse": "wh-3", + }, + Object { + "quantity": 75, + "warehouse": "wh-4", + }, + ], + }, + Object { + "attributes": Array [ + Object { + "id": "attr-1", + "values": Array [ + "val-1-1", + ], + }, + Object { + "id": "attr-2", + "values": Array [ + "val-2-4", + ], + }, + Object { + "id": "attr-4", + "values": Array [ + "val-4-5", + ], + }, + ], + "priceOverride": "49.99", + "sku": "", + "stocks": Array [ + Object { + "quantity": 5, + "warehouse": "wh-1", + }, + Object { + "quantity": 50, + "warehouse": "wh-2", + }, + Object { + "quantity": 85, + "warehouse": "wh-3", + }, + Object { + "quantity": 105, + "warehouse": "wh-4", + }, + ], + }, + Object { + "attributes": Array [ + Object { + "id": "attr-1", + "values": Array [ + "val-1-7", + ], + }, + Object { + "id": "attr-2", + "values": Array [ + "val-2-2", + ], + }, + Object { + "id": "attr-4", + "values": Array [ + "val-4-1", + ], + }, + ], + "priceOverride": "24.99", + "sku": "", + "stocks": Array [ + Object { + "quantity": 50, + "warehouse": "wh-1", + }, + Object { + "quantity": 20, + "warehouse": "wh-2", + }, + Object { + "quantity": 45, + "warehouse": "wh-3", + }, + Object { + "quantity": 75, + "warehouse": "wh-4", + }, + ], + }, + Object { + "attributes": Array [ + Object { + "id": "attr-1", + "values": Array [ + "val-1-7", + ], + }, + Object { + "id": "attr-2", + "values": Array [ + "val-2-2", + ], + }, + Object { + "id": "attr-4", + "values": Array [ + "val-4-5", + ], + }, + ], + "priceOverride": "24.99", + "sku": "", + "stocks": Array [ + Object { + "quantity": 80, + "warehouse": "wh-1", + }, + Object { + "quantity": 50, + "warehouse": "wh-2", + }, + Object { + "quantity": 85, + "warehouse": "wh-3", + }, + Object { + "quantity": 105, + "warehouse": "wh-4", + }, + ], + }, + Object { + "attributes": Array [ + Object { + "id": "attr-1", + "values": Array [ + "val-1-7", + ], + }, + Object { + "id": "attr-2", + "values": Array [ + "val-2-4", + ], + }, + Object { + "id": "attr-4", + "values": Array [ + "val-4-1", + ], + }, + ], + "priceOverride": "26.99", + "sku": "", + "stocks": Array [ + Object { + "quantity": 50, + "warehouse": "wh-1", + }, + Object { + "quantity": 20, + "warehouse": "wh-2", + }, + Object { + "quantity": 45, + "warehouse": "wh-3", + }, + Object { + "quantity": 75, + "warehouse": "wh-4", + }, + ], + }, + Object { + "attributes": Array [ + Object { + "id": "attr-1", + "values": Array [ + "val-1-7", + ], + }, + Object { + "id": "attr-2", + "values": Array [ + "val-2-4", + ], + }, + Object { + "id": "attr-4", + "values": Array [ + "val-4-5", + ], + }, + ], + "priceOverride": "26.99", + "sku": "", + "stocks": Array [ + Object { + "quantity": 80, + "warehouse": "wh-1", + }, + Object { + "quantity": 50, + "warehouse": "wh-2", + }, + Object { + "quantity": 85, + "warehouse": "wh-3", + }, + Object { + "quantity": 105, + "warehouse": "wh-4", + }, + ], + }, + ], + "warehouses": Array [ + "wh-1", + "wh-2", + "wh-3", + "wh-4", + ], +} +`; + exports[`Reducer is able to select attribute values 1`] = ` Object { "attributes": Array [ @@ -34,10 +858,11 @@ Object { "stock": Object { "all": true, "attribute": undefined, - "value": "", + "value": Array [], "values": Array [], }, "variants": Array [], + "warehouses": Array [], } `; @@ -75,7 +900,7 @@ Object { "stock": Object { "all": true, "attribute": undefined, - "value": "", + "value": Array [], "values": Array [], }, "variants": Array [ @@ -101,8 +926,8 @@ Object { }, ], "priceOverride": "45.99", - "quantity": NaN, "sku": "", + "stocks": Array [], }, Object { "attributes": Array [ @@ -126,8 +951,8 @@ Object { }, ], "priceOverride": "45.99", - "quantity": NaN, "sku": "", + "stocks": Array [], }, Object { "attributes": Array [ @@ -151,8 +976,8 @@ Object { }, ], "priceOverride": "45.99", - "quantity": NaN, "sku": "", + "stocks": Array [], }, Object { "attributes": Array [ @@ -176,8 +1001,8 @@ Object { }, ], "priceOverride": "45.99", - "quantity": NaN, "sku": "", + "stocks": Array [], }, Object { "attributes": Array [ @@ -201,8 +1026,8 @@ Object { }, ], "priceOverride": "45.99", - "quantity": NaN, "sku": "", + "stocks": Array [], }, Object { "attributes": Array [ @@ -226,8 +1051,8 @@ Object { }, ], "priceOverride": "45.99", - "quantity": NaN, "sku": "", + "stocks": Array [], }, Object { "attributes": Array [ @@ -251,8 +1076,8 @@ Object { }, ], "priceOverride": "45.99", - "quantity": NaN, "sku": "", + "stocks": Array [], }, Object { "attributes": Array [ @@ -276,10 +1101,16 @@ Object { }, ], "priceOverride": "45.99", - "quantity": NaN, "sku": "", + "stocks": Array [], }, ], + "warehouses": Array [ + "wh-1", + "wh-2", + "wh-3", + "wh-4", + ], } `; @@ -326,7 +1157,7 @@ Object { "stock": Object { "all": true, "attribute": undefined, - "value": "", + "value": Array [], "values": Array [], }, "variants": Array [ @@ -352,8 +1183,8 @@ Object { }, ], "priceOverride": "45.99", - "quantity": NaN, "sku": "", + "stocks": Array [], }, Object { "attributes": Array [ @@ -377,8 +1208,8 @@ Object { }, ], "priceOverride": "45.99", - "quantity": NaN, "sku": "", + "stocks": Array [], }, Object { "attributes": Array [ @@ -402,8 +1233,8 @@ Object { }, ], "priceOverride": "45.99", - "quantity": NaN, "sku": "", + "stocks": Array [], }, Object { "attributes": Array [ @@ -427,8 +1258,8 @@ Object { }, ], "priceOverride": "45.99", - "quantity": NaN, "sku": "", + "stocks": Array [], }, Object { "attributes": Array [ @@ -452,8 +1283,8 @@ Object { }, ], "priceOverride": "51.99", - "quantity": NaN, "sku": "", + "stocks": Array [], }, Object { "attributes": Array [ @@ -477,8 +1308,8 @@ Object { }, ], "priceOverride": "51.99", - "quantity": NaN, "sku": "", + "stocks": Array [], }, Object { "attributes": Array [ @@ -502,8 +1333,8 @@ Object { }, ], "priceOverride": "51.99", - "quantity": NaN, "sku": "", + "stocks": Array [], }, Object { "attributes": Array [ @@ -527,10 +1358,16 @@ Object { }, ], "priceOverride": "51.99", - "quantity": NaN, "sku": "", + "stocks": Array [], }, ], + "warehouses": Array [ + "wh-1", + "wh-2", + "wh-3", + "wh-4", + ], } `; @@ -810,15 +1647,19 @@ Object { "stock": Object { "all": false, "attribute": "attr-1", - "value": "", + "value": Array [], "values": Array [ Object { "slug": "val-1-1", - "value": "13", + "value": Array [ + undefined, + ], }, Object { "slug": "val-1-7", - "value": "19", + "value": Array [ + undefined, + ], }, ], }, @@ -845,8 +1686,13 @@ Object { }, ], "priceOverride": "10.99", - "quantity": 13, "sku": "", + "stocks": Array [ + Object { + "quantity": undefined, + "warehouse": "wh-1", + }, + ], }, Object { "attributes": Array [ @@ -870,8 +1716,13 @@ Object { }, ], "priceOverride": "10.99", - "quantity": 13, "sku": "", + "stocks": Array [ + Object { + "quantity": undefined, + "warehouse": "wh-1", + }, + ], }, Object { "attributes": Array [ @@ -895,8 +1746,13 @@ Object { }, ], "priceOverride": "10.99", - "quantity": 13, "sku": "", + "stocks": Array [ + Object { + "quantity": undefined, + "warehouse": "wh-1", + }, + ], }, Object { "attributes": Array [ @@ -920,8 +1776,13 @@ Object { }, ], "priceOverride": "10.99", - "quantity": 13, "sku": "", + "stocks": Array [ + Object { + "quantity": undefined, + "warehouse": "wh-1", + }, + ], }, Object { "attributes": Array [ @@ -945,8 +1806,13 @@ Object { }, ], "priceOverride": "10.99", - "quantity": 19, "sku": "", + "stocks": Array [ + Object { + "quantity": undefined, + "warehouse": "wh-1", + }, + ], }, Object { "attributes": Array [ @@ -970,8 +1836,13 @@ Object { }, ], "priceOverride": "10.99", - "quantity": 19, "sku": "", + "stocks": Array [ + Object { + "quantity": undefined, + "warehouse": "wh-1", + }, + ], }, Object { "attributes": Array [ @@ -995,8 +1866,13 @@ Object { }, ], "priceOverride": "10.99", - "quantity": 19, "sku": "", + "stocks": Array [ + Object { + "quantity": undefined, + "warehouse": "wh-1", + }, + ], }, Object { "attributes": Array [ @@ -1020,9 +1896,20 @@ Object { }, ], "priceOverride": "10.99", - "quantity": 19, "sku": "", + "stocks": Array [ + Object { + "quantity": undefined, + "warehouse": "wh-1", + }, + ], }, ], + "warehouses": Array [ + "wh-1", + "wh-2", + "wh-3", + "wh-4", + ], } `; diff --git a/src/products/components/ProductVariantCreatorPage/createVariants.test.ts b/src/products/components/ProductVariantCreatorPage/createVariants.test.ts index e41c65229..6957c2030 100644 --- a/src/products/components/ProductVariantCreatorPage/createVariants.test.ts +++ b/src/products/components/ProductVariantCreatorPage/createVariants.test.ts @@ -18,7 +18,7 @@ describe("Creates variant matrix", () => { it("with constant price and stock", () => { const price = "49.99"; - const stock = 80; + const stock = [80, 40, 30]; const data: ProductVariantCreateFormData = { ...thirdStep, @@ -30,7 +30,7 @@ describe("Creates variant matrix", () => { stock: { ...thirdStep.stock, all: true, - value: stock.toString() + value: stock } }; @@ -44,13 +44,15 @@ describe("Creates variant matrix", () => { variants.forEach(variant => { expect(variant.priceOverride).toBe(price); - expect(variant.quantity).toBe(stock); + variant.stocks.forEach((_, stockIndex) => { + expect(variant.stocks[stockIndex].quantity).toBe(stock[stockIndex]); + }); }); }); it("with constant stock and attribute dependent price", () => { const price = 49.99; - const stock = 80; + const stock = [80, 40, 30]; const attribute = attributes.find( attribute => attribute.id === thirdStep.attributes[0].id ); @@ -69,7 +71,7 @@ describe("Creates variant matrix", () => { stock: { ...thirdStep.stock, all: true, - value: stock.toString() + value: stock } }; @@ -82,7 +84,9 @@ describe("Creates variant matrix", () => { ); variants.forEach(variant => { - expect(variant.quantity).toBe(stock); + variant.stocks.forEach((_, stockIndex) => { + expect(variant.stocks[stockIndex].quantity).toBe(stock[stockIndex]); + }); }); attribute.values.forEach((attributeValue, attributeValueIndex) => { @@ -103,7 +107,7 @@ describe("Creates variant matrix", () => { it("with constant price and attribute dependent stock", () => { const price = "49.99"; - const stock = 80; + const stock = [80, 40, 30]; const attribute = attributes.find( attribute => attribute.id === thirdStep.attributes[0].id ); @@ -121,7 +125,9 @@ describe("Creates variant matrix", () => { attribute: attribute.id, values: attribute.values.map((attributeValue, attributeValueIndex) => ({ slug: attributeValue, - value: (stock * (attributeValueIndex + 1)).toString() + value: stock.map( + (_, stockIndex) => stock[stockIndex] * (attributeValueIndex + 1) + ) })) } }; @@ -147,14 +153,18 @@ describe("Creates variant matrix", () => { ).values[0] === attributeValue ) .forEach(variant => { - expect(variant.quantity).toBe(stock * (attributeValueIndex + 1)); + variant.stocks.forEach((_, stockIndex) => { + expect(variant.stocks[stockIndex].quantity).toBe( + stock[stockIndex] * (attributeValueIndex + 1) + ); + }); }); }); }); it("with attribute dependent price and stock", () => { const price = 49.99; - const stock = 80; + const stock = [80, 40, 30]; const attribute = attributes.find( attribute => attribute.id === thirdStep.attributes[0].id ); @@ -176,7 +186,9 @@ describe("Creates variant matrix", () => { attribute: attribute.id, values: attribute.values.map((attributeValue, attributeValueIndex) => ({ slug: attributeValue, - value: (stock * (attributeValueIndex + 1)).toString() + value: stock.map( + (_, stockIndex) => stock[stockIndex] * (attributeValueIndex + 1) + ) })) } }; @@ -213,7 +225,11 @@ describe("Creates variant matrix", () => { ).values[0] === attributeValue ) .forEach(variant => { - expect(variant.quantity).toBe(stock * (attributeValueIndex + 1)); + variant.stocks.forEach((_, stockIndex) => { + expect(variant.stocks[stockIndex].quantity).toBe( + stock[stockIndex] * (attributeValueIndex + 1) + ); + }); }); }); }); diff --git a/src/products/components/ProductVariantCreatorPage/createVariants.ts b/src/products/components/ProductVariantCreatorPage/createVariants.ts index ae41eeda2..5ec21dd15 100644 --- a/src/products/components/ProductVariantCreatorPage/createVariants.ts +++ b/src/products/components/ProductVariantCreatorPage/createVariants.ts @@ -36,7 +36,6 @@ function createVariant( const stocks = data.stock.all ? data.stock.value : getAttributeValuePriceOrStock(attributes, data.stock); - return { attributes: attributes.map(attribute => ({ id: attribute.attributeId, @@ -45,7 +44,7 @@ function createVariant( priceOverride, sku: "", stocks: stocks.map((quantity, stockIndex) => ({ - quantity: parseInt(quantity, 10), + quantity, warehouse: data.warehouses[stockIndex] })) }; diff --git a/src/products/components/ProductVariantCreatorPage/fixtures.ts b/src/products/components/ProductVariantCreatorPage/fixtures.ts index 5cc1b3ce2..2972bc969 100644 --- a/src/products/components/ProductVariantCreatorPage/fixtures.ts +++ b/src/products/components/ProductVariantCreatorPage/fixtures.ts @@ -1,3 +1,4 @@ +import { WarehouseFragment } from "@saleor/warehouses/types/WarehouseFragment"; import { createVariants } from "./createVariants"; import { AllOrAttribute, @@ -32,8 +33,31 @@ export const attributes = [ } ]; +export const warehouses: WarehouseFragment[] = [ + { + __typename: "Warehouse", + id: "wh-1", + name: "Warehouse 1" + }, + { + __typename: "Warehouse", + id: "wh-2", + name: "Warehouse 2" + }, + { + __typename: "Warehouse", + id: "wh-3", + name: "Warehouse 3" + }, + { + __typename: "Warehouse", + id: "wh-4", + name: "Warehouse 4" + } +]; + export const secondStep: ProductVariantCreateFormData = { - ...createInitialForm([], "10.99"), + ...createInitialForm([], "10.99", warehouses), attributes: [ { id: attributes[0].id, @@ -65,10 +89,11 @@ export const thirdStep: ProductVariantCreateFormData = { id: attributes[3].id, values: [0, 4].map(index => attributes[3].values[index]) } - ] + ], + warehouses: warehouses.map(warehouse => warehouse.id) }; -const price: AllOrAttribute = { +const price: AllOrAttribute = { all: false, attribute: thirdStep.attributes[1].id, value: "", @@ -83,18 +108,18 @@ const price: AllOrAttribute = { } ] }; -const stock: AllOrAttribute = { +const stock: AllOrAttribute = { all: false, attribute: thirdStep.attributes[2].id, - value: "", + value: [], values: [ { slug: thirdStep.attributes[2].values[0], - value: "50" + value: [50, 20, 45, 75] }, { slug: thirdStep.attributes[2].values[1], - value: "35" + value: [80, 50, 85, 105] } ] }; diff --git a/src/products/components/ProductVariantCreatorPage/reducer.test.ts b/src/products/components/ProductVariantCreatorPage/reducer.test.ts index 9a224af0f..ddfcdf5fa 100644 --- a/src/products/components/ProductVariantCreatorPage/reducer.test.ts +++ b/src/products/components/ProductVariantCreatorPage/reducer.test.ts @@ -1,4 +1,10 @@ -import { attributes, fourthStep, secondStep, thirdStep } from "./fixtures"; +import { + attributes, + fourthStep, + secondStep, + thirdStep, + warehouses +} from "./fixtures"; import reducer, { VariantField } from "./reducer"; function execActions( @@ -60,6 +66,9 @@ describe("Reducer is able to", () => { { type: "changeApplyPriceToAllValue", value + }, + { + type: "reload" } ]); @@ -69,20 +78,24 @@ describe("Reducer is able to", () => { }); it("select stock for all variants", () => { - const value = 45.99; + const quantity = 45.99; const state = execActions(thirdStep, reducer, [ { all: true, type: "applyStockToAll" }, { + quantity, type: "changeApplyStockToAllValue", - value: value.toString() + warehouseIndex: 1 + }, + { + type: "reload" } ]); expect(state.stock.all).toBeTruthy(); - expect(state.stock.value).toBe(value.toString()); + expect(state.stock.value[1]).toBe(quantity); expect(state).toMatchSnapshot(); }); @@ -107,6 +120,9 @@ describe("Reducer is able to", () => { type: "changeAttributeValuePrice", value: (value + 6).toString(), valueId: attribute.values[1] + }, + { + type: "reload" } ]); @@ -139,6 +155,9 @@ describe("Reducer is able to", () => { type: "changeAttributeValueStock", value: (value + 6).toString(), valueId: attribute.values[1] + }, + { + type: "reload" } ]); @@ -168,6 +187,29 @@ describe("Reducer is able to", () => { expect(state.variants[variantIndex - 1].priceOverride).toBe( fourthStep.variants[variantIndex - 1].priceOverride ); + expect(state).toMatchSnapshot(); + }); + + it("modify individual variant stock", () => { + const quantity = 5; + const variantIndex = 3; + + const state = execActions(fourthStep, reducer, [ + { + stock: { + quantity, + warehouse: warehouses[0].id + }, + type: "changeVariantStockData", + variantIndex + } + ]); + + expect(state.variants[variantIndex].stocks[0].quantity).toBe(quantity); + expect(state.variants[variantIndex - 1].stocks[0].quantity).toBe( + fourthStep.variants[variantIndex - 1].stocks[0].quantity + ); + expect(state).toMatchSnapshot(); }); it("delete variant", () => { diff --git a/src/storybook/__snapshots__/Stories.test.ts.snap b/src/storybook/__snapshots__/Stories.test.ts.snap index 2dc1dd71d..c6a717bae 100644 --- a/src/storybook/__snapshots__/Stories.test.ts.snap +++ b/src/storybook/__snapshots__/Stories.test.ts.snap @@ -97498,7 +97498,7 @@ exports[`Storyshots Views / Products / Create multiple variants / summary defaul class="Container-root-id" >
- Summary + Created Variants
-
+
+ Variant +
+
+ Price +
+
+ C our wares +
+
+ Be stocked +
+
+ Darkwares +
+
+ SKU +
+
+
+
+ + 100g + + + Arabica + + + Round + +
+
- Variant -
-
- Price -
-
- Inventory -
-
- SKU +
+ + USD + +
- - 100g - - - Arabica - - - Round - -
-
-
+
+ + ​ + + +
-
-
-
- - -
-
-
-
-
-
- - -
-
-
-
- -
- - 100g - - - Arabica - - - Polo - -
-
-
+
+ + ​ + + +
-
-
-
- - -
-
-
-
-
-
- - -
-
-
-
- -
- - 500g - - - Arabica - - - Round - -
-
-
+
+ + ​ + + +
-
-
-
- - -
-
-
-
-
-
- - -
-
-
-
- -
- - 500g - - - Arabica - - - Polo - -
-
-
+
+ + ​ + + +
+
+
+ +
+
+
+ + 100g + + + Arabica + + + Polo + +
+
-
+ USD +
+ + ​ + + +
+
+
-
+
+ + ​ + + +
+
+
- + + ​ + + + +
+
+
+
+ + +
+
+
+
+
+
+ + +
+
+
+
+ +
+
+
+ + 500g + + + Arabica + + + Round + +
+
+
+
+ + USD + +
+
+
+
+
+
+ + +
+
+
+
+
+
+ + +
+
+
+
+
+
+ + +
+
+
+
+
+
+ + +
+
+
+
+ +
+
+
+ + 500g + + + Arabica + + + Polo + +
+
+
+
+ + USD + +
+
+
+
+
+
+ + +
+
+
+
+
+
+ + +
+
+
+
+
+
+ + +
+
+
+
+
+
+ + +
+
+
+
+ +
@@ -98157,7 +98428,7 @@ exports[`Storyshots Views / Products / Create multiple variants / summary errors class="Container-root-id" >
- Summary + Created Variants
-
+
+ Variant +
+
+ Price +
+
+ C our wares +
+
+ Be stocked +
+
+ Darkwares +
+
+ SKU +
+
+
+
+ + 100g + + + Arabica + + + Round + +
+
- Variant -
-
- Price -
-
- Inventory -
-
- SKU +
+ + USD + +
- - 100g - - - Arabica - - - Round - -
-
-
+
+ + ​ + + +
-
-
-
- - -
-
-
-
-
-
- - -
-
-
-
- -
- - 100g - - - Arabica - - - Polo - -
-
-
+
+ + ​ + + +
-
-
-
- - -
-
-
-
-
-
- - -
-
-
-
- -
- - 500g - - - Arabica - - - Round - -
-
-
+
+ + ​ + + +
-
-
-
- - -
-
-
-
-
-
- - -
-
-
-
- -
- - 500g - - - Arabica - - - Polo - -
-
-
+
+ + ​ + + +
+
+
+ +
+
+
+ + 100g + + + Arabica + + + Polo + +
+
-
+ USD +
+ + ​ + + +
+
+
-
+
-

- SKUs must be unique -

+ + ​ + + +
+
+
- + + ​ + + + +
+
+
+
+ + +
+
+
+
+
+
+ + +
+
+
+
+ +
+
+
+ + 500g + + + Arabica + + + Round + +
+
+
+
+ + USD + +
+
+
+
+
+
+ + +
+
+
+
+
+
+ + +
+
+
+
+
+
+ + +
+
+
+
+
+
+ + +
+
+
+
+ +
+
+
+ + 500g + + + Arabica + + + Polo + +
+
+
+
+ + USD + +
+
+
+
+
+
+ + +
+
+
+
+
+
+ + +
+
+
+
+
+
+ + +
+
+
+
+
+
+ + +
+

+ SKUs must be unique +

+
+
+
+ +
@@ -100466,11 +101008,11 @@ exports[`Storyshots Views / Products / Create multiple variants prices and SKU 1 role="button" tabindex="0" > - Coffee Genre + Box Size
@@ -100558,6 +101100,63 @@ exports[`Storyshots Views / Products / Create multiple variants prices and SKU 1
+
+
+
+
+
+ 500g +
+
+
+
+ +
+ + USD + +
+
+
+
@@ -100837,7 +101436,62 @@ exports[`Storyshots Views / Products / Create multiple variants prices and SKU 1 aria-invalid="false" class="MuiInputBase-input-id MuiOutlinedInput-input-id" type="text" - value="30" + value="30,27,24" + /> + +
+ + + +
+
+
+
+
+ 500g +
+
+
+
+ +
+