From cd7426c7a939a9fc1fecb24e58f5b24dbfe6300e Mon Sep 17 00:00:00 2001 From: dominik-zeglen Date: Fri, 10 Apr 2020 12:34:13 +0200 Subject: [PATCH] Update tests --- .../__snapshots__/reducer.test.ts.snap | 22 +-- .../ProductVariantCreatorPage/reducer.test.ts | 156 ++++++++++++------ .../ProductVariantCreatorPage/reducer.ts | 12 +- 3 files changed, 120 insertions(+), 70 deletions(-) diff --git a/src/products/components/ProductVariantCreatorPage/__snapshots__/reducer.test.ts.snap b/src/products/components/ProductVariantCreatorPage/__snapshots__/reducer.test.ts.snap index b2b9f6af8..db7e66201 100644 --- a/src/products/components/ProductVariantCreatorPage/__snapshots__/reducer.test.ts.snap +++ b/src/products/components/ProductVariantCreatorPage/__snapshots__/reducer.test.ts.snap @@ -625,7 +625,7 @@ Object { ], }, ], - "priceOverride": "49.99", + "priceOverride": "26.99", "sku": "", "stocks": Array [ Object { @@ -1652,13 +1652,13 @@ Object { Object { "slug": "val-1-1", "value": Array [ - undefined, + 13, ], }, Object { "slug": "val-1-7", "value": Array [ - undefined, + 19, ], }, ], @@ -1689,7 +1689,7 @@ Object { "sku": "", "stocks": Array [ Object { - "quantity": undefined, + "quantity": 13, "warehouse": "wh-1", }, ], @@ -1719,7 +1719,7 @@ Object { "sku": "", "stocks": Array [ Object { - "quantity": undefined, + "quantity": 13, "warehouse": "wh-1", }, ], @@ -1749,7 +1749,7 @@ Object { "sku": "", "stocks": Array [ Object { - "quantity": undefined, + "quantity": 13, "warehouse": "wh-1", }, ], @@ -1779,7 +1779,7 @@ Object { "sku": "", "stocks": Array [ Object { - "quantity": undefined, + "quantity": 13, "warehouse": "wh-1", }, ], @@ -1809,7 +1809,7 @@ Object { "sku": "", "stocks": Array [ Object { - "quantity": undefined, + "quantity": 19, "warehouse": "wh-1", }, ], @@ -1839,7 +1839,7 @@ Object { "sku": "", "stocks": Array [ Object { - "quantity": undefined, + "quantity": 19, "warehouse": "wh-1", }, ], @@ -1869,7 +1869,7 @@ Object { "sku": "", "stocks": Array [ Object { - "quantity": undefined, + "quantity": 19, "warehouse": "wh-1", }, ], @@ -1899,7 +1899,7 @@ Object { "sku": "", "stocks": Array [ Object { - "quantity": undefined, + "quantity": 19, "warehouse": "wh-1", }, ], diff --git a/src/products/components/ProductVariantCreatorPage/reducer.test.ts b/src/products/components/ProductVariantCreatorPage/reducer.test.ts index b4a0bb90f..b7cfca23b 100644 --- a/src/products/components/ProductVariantCreatorPage/reducer.test.ts +++ b/src/products/components/ProductVariantCreatorPage/reducer.test.ts @@ -22,34 +22,47 @@ describe("Reducer is able to", () => { it("select attribute values", () => { const state = execActions(secondStep, reducer, [ { - attributeId: attributes[0].id, - type: ProductVariantCreateReducerActionType.selectValue, - valueId: attributes[0].values[0] + selectValue: { + attributeId: attributes[0].id, + valueId: attributes[0].values[0] + }, + type: ProductVariantCreateReducerActionType.selectValue }, { - attributeId: attributes[0].id, - type: ProductVariantCreateReducerActionType.selectValue, - valueId: attributes[0].values[6] + selectValue: { + attributeId: attributes[0].id, + valueId: attributes[0].values[6] + }, + + type: ProductVariantCreateReducerActionType.selectValue }, { - attributeId: attributes[1].id, - type: ProductVariantCreateReducerActionType.selectValue, - valueId: attributes[1].values[1] + selectValue: { + attributeId: attributes[1].id, + valueId: attributes[1].values[1] + }, + type: ProductVariantCreateReducerActionType.selectValue }, { - attributeId: attributes[1].id, - type: ProductVariantCreateReducerActionType.selectValue, - valueId: attributes[1].values[3] + selectValue: { + attributeId: attributes[1].id, + valueId: attributes[1].values[3] + }, + type: ProductVariantCreateReducerActionType.selectValue }, { - attributeId: attributes[3].id, - type: ProductVariantCreateReducerActionType.selectValue, - valueId: attributes[3].values[0] + selectValue: { + attributeId: attributes[3].id, + valueId: attributes[3].values[0] + }, + type: ProductVariantCreateReducerActionType.selectValue }, { - attributeId: attributes[3].id, - type: ProductVariantCreateReducerActionType.selectValue, - valueId: attributes[3].values[4] + selectValue: { + attributeId: attributes[3].id, + valueId: attributes[3].values[4] + }, + type: ProductVariantCreateReducerActionType.selectValue } ]); @@ -63,12 +76,16 @@ describe("Reducer is able to", () => { const value = "45.99"; const state = execActions(thirdStep, reducer, [ { - all: true, + applyPriceOrStockToAll: { + all: true + }, type: ProductVariantCreateReducerActionType.applyPriceToAll }, { - type: ProductVariantCreateReducerActionType.changeApplyPriceToAllValue, - value + changeApplyPriceToAllValue: { + value + }, + type: ProductVariantCreateReducerActionType.changeApplyPriceToAllValue }, { type: ProductVariantCreateReducerActionType.reload @@ -81,16 +98,21 @@ describe("Reducer is able to", () => { }); it("select stock for all variants", () => { - const quantity = 45.99; + const quantity = 45; + const warehouseIndex = 1; const state = execActions(thirdStep, reducer, [ { - all: true, + applyPriceOrStockToAll: { + all: true + }, type: ProductVariantCreateReducerActionType.applyStockToAll }, { - quantity, - type: ProductVariantCreateReducerActionType.changeApplyStockToAllValue, - warehouseIndex: 1 + changeApplyStockToAllValue: { + quantity, + warehouseIndex + }, + type: ProductVariantCreateReducerActionType.changeApplyStockToAllValue }, { type: ProductVariantCreateReducerActionType.reload @@ -98,7 +120,7 @@ describe("Reducer is able to", () => { ]); expect(state.stock.all).toBeTruthy(); - expect(state.stock.value[1]).toBe(quantity); + expect(state.stock.value[warehouseIndex]).toBe(quantity); expect(state).toMatchSnapshot(); }); @@ -107,23 +129,31 @@ describe("Reducer is able to", () => { const value = 45.99; const state = execActions(thirdStep, reducer, [ { - all: false, + applyPriceOrStockToAll: { + all: false + }, type: ProductVariantCreateReducerActionType.applyPriceToAll }, { - attributeId: attribute.id, + changeApplyPriceOrStockToAttributeId: { + attributeId: attribute.id + }, type: ProductVariantCreateReducerActionType.changeApplyPriceToAttributeId }, { - type: ProductVariantCreateReducerActionType.changeAttributeValuePrice, - value: value.toString(), - valueId: attribute.values[0] + changeAttributeValuePrice: { + price: value.toString(), + valueId: attribute.values[0] + }, + type: ProductVariantCreateReducerActionType.changeAttributeValuePrice }, { - type: ProductVariantCreateReducerActionType.changeAttributeValuePrice, - value: (value + 6).toString(), - valueId: attribute.values[1] + changeAttributeValuePrice: { + price: (value + 6).toString(), + valueId: attribute.values[1] + }, + type: ProductVariantCreateReducerActionType.changeAttributeValuePrice }, { type: ProductVariantCreateReducerActionType.reload @@ -140,26 +170,36 @@ describe("Reducer is able to", () => { it("select stock to each attribute value", () => { const attribute = thirdStep.attributes[0]; - const value = 13; + const quantity = 13; const state = execActions(thirdStep, reducer, [ { - all: false, + applyPriceOrStockToAll: { + all: false + }, type: ProductVariantCreateReducerActionType.applyStockToAll }, { - attributeId: attribute.id, + changeApplyPriceOrStockToAttributeId: { + attributeId: attribute.id + }, type: ProductVariantCreateReducerActionType.changeApplyStockToAttributeId }, { - type: ProductVariantCreateReducerActionType.changeAttributeValueStock, - value: value.toString(), - valueId: attribute.values[0] + changeAttributeValueStock: { + quantity, + valueId: attribute.values[0], + warehouseIndex: 0 + }, + type: ProductVariantCreateReducerActionType.changeAttributeValueStock }, { - type: ProductVariantCreateReducerActionType.changeAttributeValueStock, - value: (value + 6).toString(), - valueId: attribute.values[1] + changeAttributeValueStock: { + quantity: quantity + 6, + valueId: attribute.values[1], + warehouseIndex: 0 + }, + type: ProductVariantCreateReducerActionType.changeAttributeValueStock }, { type: ProductVariantCreateReducerActionType.reload @@ -181,10 +221,12 @@ describe("Reducer is able to", () => { const state = execActions(fourthStep, reducer, [ { - field, - type: ProductVariantCreateReducerActionType.changeVariantData, - value, - variantIndex + changeVariantData: { + field, + value, + variantIndex + }, + type: ProductVariantCreateReducerActionType.changeVariantData } ]); @@ -201,12 +243,14 @@ describe("Reducer is able to", () => { const state = execActions(fourthStep, reducer, [ { - stock: { - quantity, - warehouse: warehouses[0].id + changeVariantStockData: { + stock: { + quantity, + warehouse: warehouses[0].id + }, + variantIndex }, - type: ProductVariantCreateReducerActionType.changeVariantStockData, - variantIndex + type: ProductVariantCreateReducerActionType.changeVariantStockData } ]); @@ -222,8 +266,10 @@ describe("Reducer is able to", () => { const state = execActions(fourthStep, reducer, [ { - type: ProductVariantCreateReducerActionType.deleteVariant, - variantIndex + deleteVariant: { + variantIndex + }, + type: ProductVariantCreateReducerActionType.deleteVariant } ]); diff --git a/src/products/components/ProductVariantCreatorPage/reducer.ts b/src/products/components/ProductVariantCreatorPage/reducer.ts index a341ad349..ebc4ba734 100644 --- a/src/products/components/ProductVariantCreatorPage/reducer.ts +++ b/src/products/components/ProductVariantCreatorPage/reducer.ts @@ -283,7 +283,9 @@ function changeVariantData( value: string, variantIndex: number ): ProductVariantCreateFormData { - const variant = state.variants[variantIndex]; + const variant = { + ...state.variants[variantIndex] + }; if (field === "price") { variant.priceOverride = value; } else if (field === "sku") { @@ -301,7 +303,9 @@ function changeVariantStockData( stock: StockInput, variantIndex: number ): ProductVariantCreateFormData { - const variant = state.variants[variantIndex]; + const variant = { + ...state.variants[variantIndex] + }; variant.stocks = update( stock, variant.stocks, @@ -342,7 +346,7 @@ function reduceProductVariantCreateFormData( return selectValue( prevState, action.selectValue.attributeId, - action.selectValue.attributeId + action.selectValue.valueId ); case ProductVariantCreateReducerActionType.applyPriceToAll: return applyPriceToAll(prevState, action.applyPriceOrStockToAll.all); @@ -398,7 +402,7 @@ function reduceProductVariantCreateFormData( case ProductVariantCreateReducerActionType.deleteVariant: return deleteVariant(prevState, action.deleteVariant.variantIndex); case ProductVariantCreateReducerActionType.reload: - return action.reload.data || createVariantMatrix(prevState); + return action.reload?.data || createVariantMatrix(prevState); default: return prevState; }