From e167a6450eaec152858ae31f45dfee4293643408 Mon Sep 17 00:00:00 2001 From: dominik-zeglen Date: Tue, 14 Apr 2020 18:08:16 +0200 Subject: [PATCH] Improve test coverage --- .../ProductVariantCreatorContent.tsx | 10 +- .../ProductVariantCreatorPriceAndSku.tsx | 2 +- .../ProductVariantCreatorStock.tsx | 6 +- .../__snapshots__/reducer.test.ts.snap | 566 ++++++++++++++++-- .../createVariants.test.ts | 16 +- .../ProductVariantCreatorPage/fixtures.ts | 8 +- .../ProductVariantCreatorPage/reducer.test.ts | 36 +- .../ProductVariantCreatorPage/reducer.ts | 30 +- 8 files changed, 605 insertions(+), 69 deletions(-) diff --git a/src/products/components/ProductVariantCreatorPage/ProductVariantCreatorContent.tsx b/src/products/components/ProductVariantCreatorPage/ProductVariantCreatorContent.tsx index cc8fa7bb7..0653a0b0b 100644 --- a/src/products/components/ProductVariantCreatorPage/ProductVariantCreatorContent.tsx +++ b/src/products/components/ProductVariantCreatorPage/ProductVariantCreatorContent.tsx @@ -80,16 +80,18 @@ const ProductVariantCreatorContent: React.FC changeApplyPriceToAllValue: { price }, - type: ProductVariantCreateReducerActionType.applyPriceToAll + type: + ProductVariantCreateReducerActionType.changeApplyPriceToAllValue }) } - onApplyToAllStockChange={(warehouseIndex, quantity) => + onApplyToAllStockChange={(quantity, warehouseIndex) => dispatchFormDataAction({ changeApplyStockToAllValue: { - quantity: parseInt(quantity, 10), + quantity, warehouseIndex }, - type: ProductVariantCreateReducerActionType.applyStockToAll + type: + ProductVariantCreateReducerActionType.changeApplyStockToAllValue }) } onAttributeSelect={(attributeId, type) => diff --git a/src/products/components/ProductVariantCreatorPage/ProductVariantCreatorPriceAndSku.tsx b/src/products/components/ProductVariantCreatorPage/ProductVariantCreatorPriceAndSku.tsx index 0d6829942..52ccf460c 100644 --- a/src/products/components/ProductVariantCreatorPage/ProductVariantCreatorPriceAndSku.tsx +++ b/src/products/components/ProductVariantCreatorPage/ProductVariantCreatorPriceAndSku.tsx @@ -21,7 +21,7 @@ export interface ProductVariantCreatorPriceAndSkuProps { type: PriceOrStock ) => void; onApplyToAllPriceChange: (value: string) => void; - onApplyToAllStockChange: (warehouseIndex: number, value: string) => void; + onApplyToAllStockChange: (quantity: number, warehouseIndex: number) => void; onAttributeSelect: (id: string, type: PriceOrStock) => void; onAttributePriceChange: (id: string, value: string) => void; onAttributeStockChange: ( diff --git a/src/products/components/ProductVariantCreatorPage/ProductVariantCreatorStock.tsx b/src/products/components/ProductVariantCreatorPage/ProductVariantCreatorStock.tsx index 6606c2e69..0f3f45389 100644 --- a/src/products/components/ProductVariantCreatorPage/ProductVariantCreatorStock.tsx +++ b/src/products/components/ProductVariantCreatorPage/ProductVariantCreatorStock.tsx @@ -84,7 +84,7 @@ export interface ProductVariantCreatorStockProps { data: ProductVariantCreateFormData; warehouses: WarehouseFragment[]; onApplyToAllChange: (mode: VariantCreatorPricesAndSkuMode) => void; - onApplyToAllStockChange: (warehouseIndex: number, value: string) => void; + onApplyToAllStockChange: (quantity: number, warehouseIndex: number) => void; onAttributeSelect: (id: string) => void; onAttributeValueChange: ( id: string, @@ -203,8 +203,8 @@ const ProductVariantCreatorStock: React.FC = pr value={data.stock.value[warehouseIndex]} onChange={event => onApplyToAllStockChange( - warehouseIndex, - event.target.value + parseInt(event.target.value, 10), + warehouseIndex ) } /> diff --git a/src/products/components/ProductVariantCreatorPage/__snapshots__/reducer.test.ts.snap b/src/products/components/ProductVariantCreatorPage/__snapshots__/reducer.test.ts.snap index db7e66201..32a195361 100644 --- a/src/products/components/ProductVariantCreatorPage/__snapshots__/reducer.test.ts.snap +++ b/src/products/components/ProductVariantCreatorPage/__snapshots__/reducer.test.ts.snap @@ -26,8 +26,8 @@ Object { }, ], "price": Object { - "all": false, "attribute": "attr-2", + "mode": "attribute", "value": "", "values": Array [ Object { @@ -41,8 +41,8 @@ Object { ], }, "stock": Object { - "all": false, "attribute": "attr-4", + "mode": "attribute", "value": Array [], "values": Array [ Object { @@ -438,8 +438,8 @@ Object { }, ], "price": Object { - "all": false, "attribute": "attr-2", + "mode": "attribute", "value": "", "values": Array [ Object { @@ -453,8 +453,8 @@ Object { ], }, "stock": Object { - "all": false, "attribute": "attr-4", + "mode": "attribute", "value": Array [], "values": Array [ Object { @@ -850,14 +850,14 @@ Object { }, ], "price": Object { - "all": true, "attribute": undefined, + "mode": "all", "value": "10.99", "values": Array [], }, "stock": Object { - "all": true, "attribute": undefined, + "mode": "all", "value": Array [], "values": Array [], }, @@ -892,15 +892,20 @@ Object { }, ], "price": Object { - "all": true, "attribute": undefined, + "mode": "all", "value": "45.99", "values": Array [], }, "stock": Object { - "all": true, "attribute": undefined, - "value": Array [], + "mode": "all", + "value": Array [ + 0, + 0, + 0, + 0, + ], "values": Array [], }, "variants": Array [ @@ -927,7 +932,24 @@ Object { ], "priceOverride": "45.99", "sku": "", - "stocks": Array [], + "stocks": Array [ + Object { + "quantity": 0, + "warehouse": "wh-1", + }, + Object { + "quantity": 0, + "warehouse": "wh-2", + }, + Object { + "quantity": 0, + "warehouse": "wh-3", + }, + Object { + "quantity": 0, + "warehouse": "wh-4", + }, + ], }, Object { "attributes": Array [ @@ -952,7 +974,24 @@ Object { ], "priceOverride": "45.99", "sku": "", - "stocks": Array [], + "stocks": Array [ + Object { + "quantity": 0, + "warehouse": "wh-1", + }, + Object { + "quantity": 0, + "warehouse": "wh-2", + }, + Object { + "quantity": 0, + "warehouse": "wh-3", + }, + Object { + "quantity": 0, + "warehouse": "wh-4", + }, + ], }, Object { "attributes": Array [ @@ -977,7 +1016,24 @@ Object { ], "priceOverride": "45.99", "sku": "", - "stocks": Array [], + "stocks": Array [ + Object { + "quantity": 0, + "warehouse": "wh-1", + }, + Object { + "quantity": 0, + "warehouse": "wh-2", + }, + Object { + "quantity": 0, + "warehouse": "wh-3", + }, + Object { + "quantity": 0, + "warehouse": "wh-4", + }, + ], }, Object { "attributes": Array [ @@ -1002,7 +1058,24 @@ Object { ], "priceOverride": "45.99", "sku": "", - "stocks": Array [], + "stocks": Array [ + Object { + "quantity": 0, + "warehouse": "wh-1", + }, + Object { + "quantity": 0, + "warehouse": "wh-2", + }, + Object { + "quantity": 0, + "warehouse": "wh-3", + }, + Object { + "quantity": 0, + "warehouse": "wh-4", + }, + ], }, Object { "attributes": Array [ @@ -1027,7 +1100,24 @@ Object { ], "priceOverride": "45.99", "sku": "", - "stocks": Array [], + "stocks": Array [ + Object { + "quantity": 0, + "warehouse": "wh-1", + }, + Object { + "quantity": 0, + "warehouse": "wh-2", + }, + Object { + "quantity": 0, + "warehouse": "wh-3", + }, + Object { + "quantity": 0, + "warehouse": "wh-4", + }, + ], }, Object { "attributes": Array [ @@ -1052,7 +1142,24 @@ Object { ], "priceOverride": "45.99", "sku": "", - "stocks": Array [], + "stocks": Array [ + Object { + "quantity": 0, + "warehouse": "wh-1", + }, + Object { + "quantity": 0, + "warehouse": "wh-2", + }, + Object { + "quantity": 0, + "warehouse": "wh-3", + }, + Object { + "quantity": 0, + "warehouse": "wh-4", + }, + ], }, Object { "attributes": Array [ @@ -1077,7 +1184,24 @@ Object { ], "priceOverride": "45.99", "sku": "", - "stocks": Array [], + "stocks": Array [ + Object { + "quantity": 0, + "warehouse": "wh-1", + }, + Object { + "quantity": 0, + "warehouse": "wh-2", + }, + Object { + "quantity": 0, + "warehouse": "wh-3", + }, + Object { + "quantity": 0, + "warehouse": "wh-4", + }, + ], }, Object { "attributes": Array [ @@ -1102,7 +1226,24 @@ Object { ], "priceOverride": "45.99", "sku": "", - "stocks": Array [], + "stocks": Array [ + Object { + "quantity": 0, + "warehouse": "wh-1", + }, + Object { + "quantity": 0, + "warehouse": "wh-2", + }, + Object { + "quantity": 0, + "warehouse": "wh-3", + }, + Object { + "quantity": 0, + "warehouse": "wh-4", + }, + ], }, ], "warehouses": Array [ @@ -1140,8 +1281,8 @@ Object { }, ], "price": Object { - "all": false, "attribute": "attr-1", + "mode": "attribute", "value": "10.99", "values": Array [ Object { @@ -1155,9 +1296,14 @@ Object { ], }, "stock": Object { - "all": true, "attribute": undefined, - "value": Array [], + "mode": "all", + "value": Array [ + 0, + 0, + 0, + 0, + ], "values": Array [], }, "variants": Array [ @@ -1184,7 +1330,24 @@ Object { ], "priceOverride": "45.99", "sku": "", - "stocks": Array [], + "stocks": Array [ + Object { + "quantity": 0, + "warehouse": "wh-1", + }, + Object { + "quantity": 0, + "warehouse": "wh-2", + }, + Object { + "quantity": 0, + "warehouse": "wh-3", + }, + Object { + "quantity": 0, + "warehouse": "wh-4", + }, + ], }, Object { "attributes": Array [ @@ -1209,7 +1372,24 @@ Object { ], "priceOverride": "45.99", "sku": "", - "stocks": Array [], + "stocks": Array [ + Object { + "quantity": 0, + "warehouse": "wh-1", + }, + Object { + "quantity": 0, + "warehouse": "wh-2", + }, + Object { + "quantity": 0, + "warehouse": "wh-3", + }, + Object { + "quantity": 0, + "warehouse": "wh-4", + }, + ], }, Object { "attributes": Array [ @@ -1234,7 +1414,24 @@ Object { ], "priceOverride": "45.99", "sku": "", - "stocks": Array [], + "stocks": Array [ + Object { + "quantity": 0, + "warehouse": "wh-1", + }, + Object { + "quantity": 0, + "warehouse": "wh-2", + }, + Object { + "quantity": 0, + "warehouse": "wh-3", + }, + Object { + "quantity": 0, + "warehouse": "wh-4", + }, + ], }, Object { "attributes": Array [ @@ -1259,7 +1456,24 @@ Object { ], "priceOverride": "45.99", "sku": "", - "stocks": Array [], + "stocks": Array [ + Object { + "quantity": 0, + "warehouse": "wh-1", + }, + Object { + "quantity": 0, + "warehouse": "wh-2", + }, + Object { + "quantity": 0, + "warehouse": "wh-3", + }, + Object { + "quantity": 0, + "warehouse": "wh-4", + }, + ], }, Object { "attributes": Array [ @@ -1284,7 +1498,24 @@ Object { ], "priceOverride": "51.99", "sku": "", - "stocks": Array [], + "stocks": Array [ + Object { + "quantity": 0, + "warehouse": "wh-1", + }, + Object { + "quantity": 0, + "warehouse": "wh-2", + }, + Object { + "quantity": 0, + "warehouse": "wh-3", + }, + Object { + "quantity": 0, + "warehouse": "wh-4", + }, + ], }, Object { "attributes": Array [ @@ -1309,7 +1540,24 @@ Object { ], "priceOverride": "51.99", "sku": "", - "stocks": Array [], + "stocks": Array [ + Object { + "quantity": 0, + "warehouse": "wh-1", + }, + Object { + "quantity": 0, + "warehouse": "wh-2", + }, + Object { + "quantity": 0, + "warehouse": "wh-3", + }, + Object { + "quantity": 0, + "warehouse": "wh-4", + }, + ], }, Object { "attributes": Array [ @@ -1334,7 +1582,24 @@ Object { ], "priceOverride": "51.99", "sku": "", - "stocks": Array [], + "stocks": Array [ + Object { + "quantity": 0, + "warehouse": "wh-1", + }, + Object { + "quantity": 0, + "warehouse": "wh-2", + }, + Object { + "quantity": 0, + "warehouse": "wh-3", + }, + Object { + "quantity": 0, + "warehouse": "wh-4", + }, + ], }, Object { "attributes": Array [ @@ -1359,7 +1624,24 @@ Object { ], "priceOverride": "51.99", "sku": "", - "stocks": Array [], + "stocks": Array [ + Object { + "quantity": 0, + "warehouse": "wh-1", + }, + Object { + "quantity": 0, + "warehouse": "wh-2", + }, + Object { + "quantity": 0, + "warehouse": "wh-3", + }, + Object { + "quantity": 0, + "warehouse": "wh-4", + }, + ], }, ], "warehouses": Array [ @@ -1397,15 +1679,20 @@ Object { }, ], "price": Object { - "all": true, "attribute": undefined, + "mode": "all", "value": "10.99", "values": Array [], }, "stock": Object { - "all": true, "attribute": undefined, - "value": "45.99", + "mode": "all", + "value": Array [ + 0, + 45, + 0, + 0, + ], "values": Array [], }, "variants": Array [ @@ -1431,8 +1718,25 @@ Object { }, ], "priceOverride": "10.99", - "quantity": 45, "sku": "", + "stocks": Array [ + Object { + "quantity": 0, + "warehouse": "wh-1", + }, + Object { + "quantity": 45, + "warehouse": "wh-2", + }, + Object { + "quantity": 0, + "warehouse": "wh-3", + }, + Object { + "quantity": 0, + "warehouse": "wh-4", + }, + ], }, Object { "attributes": Array [ @@ -1456,8 +1760,25 @@ Object { }, ], "priceOverride": "10.99", - "quantity": 45, "sku": "", + "stocks": Array [ + Object { + "quantity": 0, + "warehouse": "wh-1", + }, + Object { + "quantity": 45, + "warehouse": "wh-2", + }, + Object { + "quantity": 0, + "warehouse": "wh-3", + }, + Object { + "quantity": 0, + "warehouse": "wh-4", + }, + ], }, Object { "attributes": Array [ @@ -1481,8 +1802,25 @@ Object { }, ], "priceOverride": "10.99", - "quantity": 45, "sku": "", + "stocks": Array [ + Object { + "quantity": 0, + "warehouse": "wh-1", + }, + Object { + "quantity": 45, + "warehouse": "wh-2", + }, + Object { + "quantity": 0, + "warehouse": "wh-3", + }, + Object { + "quantity": 0, + "warehouse": "wh-4", + }, + ], }, Object { "attributes": Array [ @@ -1506,8 +1844,25 @@ Object { }, ], "priceOverride": "10.99", - "quantity": 45, "sku": "", + "stocks": Array [ + Object { + "quantity": 0, + "warehouse": "wh-1", + }, + Object { + "quantity": 45, + "warehouse": "wh-2", + }, + Object { + "quantity": 0, + "warehouse": "wh-3", + }, + Object { + "quantity": 0, + "warehouse": "wh-4", + }, + ], }, Object { "attributes": Array [ @@ -1531,8 +1886,25 @@ Object { }, ], "priceOverride": "10.99", - "quantity": 45, "sku": "", + "stocks": Array [ + Object { + "quantity": 0, + "warehouse": "wh-1", + }, + Object { + "quantity": 45, + "warehouse": "wh-2", + }, + Object { + "quantity": 0, + "warehouse": "wh-3", + }, + Object { + "quantity": 0, + "warehouse": "wh-4", + }, + ], }, Object { "attributes": Array [ @@ -1556,8 +1928,25 @@ Object { }, ], "priceOverride": "10.99", - "quantity": 45, "sku": "", + "stocks": Array [ + Object { + "quantity": 0, + "warehouse": "wh-1", + }, + Object { + "quantity": 45, + "warehouse": "wh-2", + }, + Object { + "quantity": 0, + "warehouse": "wh-3", + }, + Object { + "quantity": 0, + "warehouse": "wh-4", + }, + ], }, Object { "attributes": Array [ @@ -1581,8 +1970,25 @@ Object { }, ], "priceOverride": "10.99", - "quantity": 45, "sku": "", + "stocks": Array [ + Object { + "quantity": 0, + "warehouse": "wh-1", + }, + Object { + "quantity": 45, + "warehouse": "wh-2", + }, + Object { + "quantity": 0, + "warehouse": "wh-3", + }, + Object { + "quantity": 0, + "warehouse": "wh-4", + }, + ], }, Object { "attributes": Array [ @@ -1606,10 +2012,33 @@ Object { }, ], "priceOverride": "10.99", - "quantity": 45, "sku": "", + "stocks": Array [ + Object { + "quantity": 0, + "warehouse": "wh-1", + }, + Object { + "quantity": 45, + "warehouse": "wh-2", + }, + Object { + "quantity": 0, + "warehouse": "wh-3", + }, + Object { + "quantity": 0, + "warehouse": "wh-4", + }, + ], }, ], + "warehouses": Array [ + "wh-1", + "wh-2", + "wh-3", + "wh-4", + ], } `; @@ -1639,15 +2068,20 @@ Object { }, ], "price": Object { - "all": true, "attribute": undefined, + "mode": "all", "value": "10.99", "values": Array [], }, "stock": Object { - "all": false, "attribute": "attr-1", - "value": Array [], + "mode": "attribute", + "value": Array [ + 0, + 0, + 0, + 0, + ], "values": Array [ Object { "slug": "val-1-1", @@ -1913,3 +2347,51 @@ Object { ], } `; + +exports[`Reducer is able to select warehouses in which stock will be created 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 { + "attribute": undefined, + "mode": "all", + "value": "10.99", + "values": Array [], + }, + "stock": Object { + "attribute": undefined, + "mode": "all", + "value": Array [ + 0, + 0, + ], + "values": Array [], + }, + "variants": Array [], + "warehouses": Array [ + "wh-2", + "wh-4", + ], +} +`; diff --git a/src/products/components/ProductVariantCreatorPage/createVariants.test.ts b/src/products/components/ProductVariantCreatorPage/createVariants.test.ts index 6957c2030..9fbcd609d 100644 --- a/src/products/components/ProductVariantCreatorPage/createVariants.test.ts +++ b/src/products/components/ProductVariantCreatorPage/createVariants.test.ts @@ -24,12 +24,12 @@ describe("Creates variant matrix", () => { ...thirdStep, price: { ...thirdStep.price, - all: true, + mode: "all", value: price }, stock: { ...thirdStep.stock, - all: true, + mode: "all", value: stock } }; @@ -61,8 +61,8 @@ describe("Creates variant matrix", () => { ...thirdStep, price: { ...thirdStep.price, - all: false, attribute: attribute.id, + mode: "attribute", values: attribute.values.map((attributeValue, attributeValueIndex) => ({ slug: attributeValue, value: (price * (attributeValueIndex + 1)).toString() @@ -70,7 +70,7 @@ describe("Creates variant matrix", () => { }, stock: { ...thirdStep.stock, - all: true, + mode: "all", value: stock } }; @@ -116,13 +116,13 @@ describe("Creates variant matrix", () => { ...thirdStep, price: { ...thirdStep.price, - all: true, + mode: "all", value: price }, stock: { ...thirdStep.stock, - all: false, attribute: attribute.id, + mode: "attribute", values: attribute.values.map((attributeValue, attributeValueIndex) => ({ slug: attributeValue, value: stock.map( @@ -173,8 +173,8 @@ describe("Creates variant matrix", () => { ...thirdStep, price: { ...thirdStep.price, - all: false, attribute: attribute.id, + mode: "attribute", values: attribute.values.map((attributeValue, attributeValueIndex) => ({ slug: attributeValue, value: (price * (attributeValueIndex + 1)).toString() @@ -182,8 +182,8 @@ describe("Creates variant matrix", () => { }, stock: { ...thirdStep.stock, - all: false, attribute: attribute.id, + mode: "attribute", values: attribute.values.map((attributeValue, attributeValueIndex) => ({ slug: attributeValue, value: stock.map( diff --git a/src/products/components/ProductVariantCreatorPage/fixtures.ts b/src/products/components/ProductVariantCreatorPage/fixtures.ts index 2972bc969..41aacbd6b 100644 --- a/src/products/components/ProductVariantCreatorPage/fixtures.ts +++ b/src/products/components/ProductVariantCreatorPage/fixtures.ts @@ -90,12 +90,16 @@ export const thirdStep: ProductVariantCreateFormData = { values: [0, 4].map(index => attributes[3].values[index]) } ], + stock: { + ...secondStep.stock, + value: warehouses.map(() => 0) + }, warehouses: warehouses.map(warehouse => warehouse.id) }; const price: AllOrAttribute = { - all: false, attribute: thirdStep.attributes[1].id, + mode: "attribute", value: "", values: [ { @@ -109,8 +113,8 @@ const price: AllOrAttribute = { ] }; const stock: AllOrAttribute = { - all: false, attribute: thirdStep.attributes[2].id, + mode: "attribute", value: [], values: [ { diff --git a/src/products/components/ProductVariantCreatorPage/reducer.test.ts b/src/products/components/ProductVariantCreatorPage/reducer.test.ts index 1d9059117..7cab43af8 100644 --- a/src/products/components/ProductVariantCreatorPage/reducer.test.ts +++ b/src/products/components/ProductVariantCreatorPage/reducer.test.ts @@ -77,7 +77,7 @@ describe("Reducer is able to", () => { const state = execActions(thirdStep, reducer, [ { applyPriceOrStockToAll: { - all: true + mode: "all" }, type: ProductVariantCreateReducerActionType.applyPriceToAll }, @@ -92,18 +92,38 @@ describe("Reducer is able to", () => { } ]); - expect(state.price.all).toBeTruthy(); + expect(state.price.mode).toBe("all"); expect(state.price.value).toBe(price); expect(state).toMatchSnapshot(); }); + it("select warehouses in which stock will be created", () => { + const state = execActions(thirdStep, reducer, [ + { + changeWarehouses: { + warehouseId: warehouses[0].id + }, + type: ProductVariantCreateReducerActionType.changeWarehouses + }, + { + changeWarehouses: { + warehouseId: warehouses[2].id + }, + type: ProductVariantCreateReducerActionType.changeWarehouses + } + ]); + + expect(state.warehouses).toHaveLength(2); + expect(state).toMatchSnapshot(); + }); + it("select stock for all variants", () => { const quantity = 45; const warehouseIndex = 1; const state = execActions(thirdStep, reducer, [ { applyPriceOrStockToAll: { - all: true + mode: "all" }, type: ProductVariantCreateReducerActionType.applyStockToAll }, @@ -119,7 +139,7 @@ describe("Reducer is able to", () => { } ]); - expect(state.stock.all).toBeTruthy(); + expect(state.stock.mode).toBe("all"); expect(state.stock.value[warehouseIndex]).toBe(quantity); expect(state).toMatchSnapshot(); }); @@ -130,7 +150,7 @@ describe("Reducer is able to", () => { const state = execActions(thirdStep, reducer, [ { applyPriceOrStockToAll: { - all: false + mode: "attribute" }, type: ProductVariantCreateReducerActionType.applyPriceToAll }, @@ -160,7 +180,7 @@ describe("Reducer is able to", () => { } ]); - expect(state.price.all).toBeFalsy(); + expect(state.price.mode).toBe("attribute"); expect(state.price.values).toHaveLength( state.attributes.find(attribute => state.price.attribute === attribute.id) .values.length @@ -174,7 +194,7 @@ describe("Reducer is able to", () => { const state = execActions(thirdStep, reducer, [ { applyPriceOrStockToAll: { - all: false + mode: "attribute" }, type: ProductVariantCreateReducerActionType.applyStockToAll }, @@ -206,7 +226,7 @@ describe("Reducer is able to", () => { } ]); - expect(state.stock.all).toBeFalsy(); + expect(state.stock.mode).toBe("attribute"); expect(state.stock.values).toHaveLength( state.attributes.find(attribute => state.stock.attribute === attribute.id) .values.length diff --git a/src/products/components/ProductVariantCreatorPage/reducer.ts b/src/products/components/ProductVariantCreatorPage/reducer.ts index 81e410723..23b387af7 100644 --- a/src/products/components/ProductVariantCreatorPage/reducer.ts +++ b/src/products/components/ProductVariantCreatorPage/reducer.ts @@ -335,9 +335,37 @@ function changeWarehouses( state: ProductVariantCreateFormData, warehouseId: string ): ProductVariantCreateFormData { + const warehouses = toggle(warehouseId, state.warehouses, (a, b) => a === b); + const added = warehouses.length > state.warehouses.length; + + if (added) { + return { + ...state, + stock: { + ...state.stock, + value: [...state.stock.value, 0], + values: state.stock.values.map(stockValue => ({ + ...stockValue, + value: [...stockValue.value, 0] + })) + }, + warehouses + }; + } + + const warehouseIndex = state.warehouses.indexOf(warehouseId); + return { ...state, - warehouses: toggle(warehouseId, state.warehouses, (a, b) => a === b) + stock: { + ...state.stock, + value: removeAtIndex(state.stock.value, warehouseIndex), + values: state.stock.values.map(stockValue => ({ + ...stockValue, + value: removeAtIndex(stockValue.value, warehouseIndex) + })) + }, + warehouses }; }