Improve test coverage

This commit is contained in:
dominik-zeglen 2020-04-14 18:08:16 +02:00
parent e52b2bb74a
commit e167a6450e
8 changed files with 605 additions and 69 deletions

View file

@ -80,16 +80,18 @@ const ProductVariantCreatorContent: React.FC<ProductVariantCreatorContentProps>
changeApplyPriceToAllValue: { changeApplyPriceToAllValue: {
price price
}, },
type: ProductVariantCreateReducerActionType.applyPriceToAll type:
ProductVariantCreateReducerActionType.changeApplyPriceToAllValue
}) })
} }
onApplyToAllStockChange={(warehouseIndex, quantity) => onApplyToAllStockChange={(quantity, warehouseIndex) =>
dispatchFormDataAction({ dispatchFormDataAction({
changeApplyStockToAllValue: { changeApplyStockToAllValue: {
quantity: parseInt(quantity, 10), quantity,
warehouseIndex warehouseIndex
}, },
type: ProductVariantCreateReducerActionType.applyStockToAll type:
ProductVariantCreateReducerActionType.changeApplyStockToAllValue
}) })
} }
onAttributeSelect={(attributeId, type) => onAttributeSelect={(attributeId, type) =>

View file

@ -21,7 +21,7 @@ export interface ProductVariantCreatorPriceAndSkuProps {
type: PriceOrStock type: PriceOrStock
) => void; ) => void;
onApplyToAllPriceChange: (value: string) => void; onApplyToAllPriceChange: (value: string) => void;
onApplyToAllStockChange: (warehouseIndex: number, value: string) => void; onApplyToAllStockChange: (quantity: number, warehouseIndex: number) => void;
onAttributeSelect: (id: string, type: PriceOrStock) => void; onAttributeSelect: (id: string, type: PriceOrStock) => void;
onAttributePriceChange: (id: string, value: string) => void; onAttributePriceChange: (id: string, value: string) => void;
onAttributeStockChange: ( onAttributeStockChange: (

View file

@ -84,7 +84,7 @@ export interface ProductVariantCreatorStockProps {
data: ProductVariantCreateFormData; data: ProductVariantCreateFormData;
warehouses: WarehouseFragment[]; warehouses: WarehouseFragment[];
onApplyToAllChange: (mode: VariantCreatorPricesAndSkuMode) => void; onApplyToAllChange: (mode: VariantCreatorPricesAndSkuMode) => void;
onApplyToAllStockChange: (warehouseIndex: number, value: string) => void; onApplyToAllStockChange: (quantity: number, warehouseIndex: number) => void;
onAttributeSelect: (id: string) => void; onAttributeSelect: (id: string) => void;
onAttributeValueChange: ( onAttributeValueChange: (
id: string, id: string,
@ -203,8 +203,8 @@ const ProductVariantCreatorStock: React.FC<ProductVariantCreatorStockProps> = pr
value={data.stock.value[warehouseIndex]} value={data.stock.value[warehouseIndex]}
onChange={event => onChange={event =>
onApplyToAllStockChange( onApplyToAllStockChange(
warehouseIndex, parseInt(event.target.value, 10),
event.target.value warehouseIndex
) )
} }
/> />

View file

@ -26,8 +26,8 @@ Object {
}, },
], ],
"price": Object { "price": Object {
"all": false,
"attribute": "attr-2", "attribute": "attr-2",
"mode": "attribute",
"value": "", "value": "",
"values": Array [ "values": Array [
Object { Object {
@ -41,8 +41,8 @@ Object {
], ],
}, },
"stock": Object { "stock": Object {
"all": false,
"attribute": "attr-4", "attribute": "attr-4",
"mode": "attribute",
"value": Array [], "value": Array [],
"values": Array [ "values": Array [
Object { Object {
@ -438,8 +438,8 @@ Object {
}, },
], ],
"price": Object { "price": Object {
"all": false,
"attribute": "attr-2", "attribute": "attr-2",
"mode": "attribute",
"value": "", "value": "",
"values": Array [ "values": Array [
Object { Object {
@ -453,8 +453,8 @@ Object {
], ],
}, },
"stock": Object { "stock": Object {
"all": false,
"attribute": "attr-4", "attribute": "attr-4",
"mode": "attribute",
"value": Array [], "value": Array [],
"values": Array [ "values": Array [
Object { Object {
@ -850,14 +850,14 @@ Object {
}, },
], ],
"price": Object { "price": Object {
"all": true,
"attribute": undefined, "attribute": undefined,
"mode": "all",
"value": "10.99", "value": "10.99",
"values": Array [], "values": Array [],
}, },
"stock": Object { "stock": Object {
"all": true,
"attribute": undefined, "attribute": undefined,
"mode": "all",
"value": Array [], "value": Array [],
"values": Array [], "values": Array [],
}, },
@ -892,15 +892,20 @@ Object {
}, },
], ],
"price": Object { "price": Object {
"all": true,
"attribute": undefined, "attribute": undefined,
"mode": "all",
"value": "45.99", "value": "45.99",
"values": Array [], "values": Array [],
}, },
"stock": Object { "stock": Object {
"all": true,
"attribute": undefined, "attribute": undefined,
"value": Array [], "mode": "all",
"value": Array [
0,
0,
0,
0,
],
"values": Array [], "values": Array [],
}, },
"variants": Array [ "variants": Array [
@ -927,7 +932,24 @@ Object {
], ],
"priceOverride": "45.99", "priceOverride": "45.99",
"sku": "", "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 { Object {
"attributes": Array [ "attributes": Array [
@ -952,7 +974,24 @@ Object {
], ],
"priceOverride": "45.99", "priceOverride": "45.99",
"sku": "", "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 { Object {
"attributes": Array [ "attributes": Array [
@ -977,7 +1016,24 @@ Object {
], ],
"priceOverride": "45.99", "priceOverride": "45.99",
"sku": "", "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 { Object {
"attributes": Array [ "attributes": Array [
@ -1002,7 +1058,24 @@ Object {
], ],
"priceOverride": "45.99", "priceOverride": "45.99",
"sku": "", "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 { Object {
"attributes": Array [ "attributes": Array [
@ -1027,7 +1100,24 @@ Object {
], ],
"priceOverride": "45.99", "priceOverride": "45.99",
"sku": "", "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 { Object {
"attributes": Array [ "attributes": Array [
@ -1052,7 +1142,24 @@ Object {
], ],
"priceOverride": "45.99", "priceOverride": "45.99",
"sku": "", "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 { Object {
"attributes": Array [ "attributes": Array [
@ -1077,7 +1184,24 @@ Object {
], ],
"priceOverride": "45.99", "priceOverride": "45.99",
"sku": "", "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 { Object {
"attributes": Array [ "attributes": Array [
@ -1102,7 +1226,24 @@ Object {
], ],
"priceOverride": "45.99", "priceOverride": "45.99",
"sku": "", "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 [ "warehouses": Array [
@ -1140,8 +1281,8 @@ Object {
}, },
], ],
"price": Object { "price": Object {
"all": false,
"attribute": "attr-1", "attribute": "attr-1",
"mode": "attribute",
"value": "10.99", "value": "10.99",
"values": Array [ "values": Array [
Object { Object {
@ -1155,9 +1296,14 @@ Object {
], ],
}, },
"stock": Object { "stock": Object {
"all": true,
"attribute": undefined, "attribute": undefined,
"value": Array [], "mode": "all",
"value": Array [
0,
0,
0,
0,
],
"values": Array [], "values": Array [],
}, },
"variants": Array [ "variants": Array [
@ -1184,7 +1330,24 @@ Object {
], ],
"priceOverride": "45.99", "priceOverride": "45.99",
"sku": "", "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 { Object {
"attributes": Array [ "attributes": Array [
@ -1209,7 +1372,24 @@ Object {
], ],
"priceOverride": "45.99", "priceOverride": "45.99",
"sku": "", "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 { Object {
"attributes": Array [ "attributes": Array [
@ -1234,7 +1414,24 @@ Object {
], ],
"priceOverride": "45.99", "priceOverride": "45.99",
"sku": "", "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 { Object {
"attributes": Array [ "attributes": Array [
@ -1259,7 +1456,24 @@ Object {
], ],
"priceOverride": "45.99", "priceOverride": "45.99",
"sku": "", "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 { Object {
"attributes": Array [ "attributes": Array [
@ -1284,7 +1498,24 @@ Object {
], ],
"priceOverride": "51.99", "priceOverride": "51.99",
"sku": "", "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 { Object {
"attributes": Array [ "attributes": Array [
@ -1309,7 +1540,24 @@ Object {
], ],
"priceOverride": "51.99", "priceOverride": "51.99",
"sku": "", "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 { Object {
"attributes": Array [ "attributes": Array [
@ -1334,7 +1582,24 @@ Object {
], ],
"priceOverride": "51.99", "priceOverride": "51.99",
"sku": "", "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 { Object {
"attributes": Array [ "attributes": Array [
@ -1359,7 +1624,24 @@ Object {
], ],
"priceOverride": "51.99", "priceOverride": "51.99",
"sku": "", "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 [ "warehouses": Array [
@ -1397,15 +1679,20 @@ Object {
}, },
], ],
"price": Object { "price": Object {
"all": true,
"attribute": undefined, "attribute": undefined,
"mode": "all",
"value": "10.99", "value": "10.99",
"values": Array [], "values": Array [],
}, },
"stock": Object { "stock": Object {
"all": true,
"attribute": undefined, "attribute": undefined,
"value": "45.99", "mode": "all",
"value": Array [
0,
45,
0,
0,
],
"values": Array [], "values": Array [],
}, },
"variants": Array [ "variants": Array [
@ -1431,8 +1718,25 @@ Object {
}, },
], ],
"priceOverride": "10.99", "priceOverride": "10.99",
"quantity": 45,
"sku": "", "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 { Object {
"attributes": Array [ "attributes": Array [
@ -1456,8 +1760,25 @@ Object {
}, },
], ],
"priceOverride": "10.99", "priceOverride": "10.99",
"quantity": 45,
"sku": "", "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 { Object {
"attributes": Array [ "attributes": Array [
@ -1481,8 +1802,25 @@ Object {
}, },
], ],
"priceOverride": "10.99", "priceOverride": "10.99",
"quantity": 45,
"sku": "", "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 { Object {
"attributes": Array [ "attributes": Array [
@ -1506,8 +1844,25 @@ Object {
}, },
], ],
"priceOverride": "10.99", "priceOverride": "10.99",
"quantity": 45,
"sku": "", "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 { Object {
"attributes": Array [ "attributes": Array [
@ -1531,8 +1886,25 @@ Object {
}, },
], ],
"priceOverride": "10.99", "priceOverride": "10.99",
"quantity": 45,
"sku": "", "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 { Object {
"attributes": Array [ "attributes": Array [
@ -1556,8 +1928,25 @@ Object {
}, },
], ],
"priceOverride": "10.99", "priceOverride": "10.99",
"quantity": 45,
"sku": "", "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 { Object {
"attributes": Array [ "attributes": Array [
@ -1581,8 +1970,25 @@ Object {
}, },
], ],
"priceOverride": "10.99", "priceOverride": "10.99",
"quantity": 45,
"sku": "", "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 { Object {
"attributes": Array [ "attributes": Array [
@ -1606,10 +2012,33 @@ Object {
}, },
], ],
"priceOverride": "10.99", "priceOverride": "10.99",
"quantity": 45,
"sku": "", "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 { "price": Object {
"all": true,
"attribute": undefined, "attribute": undefined,
"mode": "all",
"value": "10.99", "value": "10.99",
"values": Array [], "values": Array [],
}, },
"stock": Object { "stock": Object {
"all": false,
"attribute": "attr-1", "attribute": "attr-1",
"value": Array [], "mode": "attribute",
"value": Array [
0,
0,
0,
0,
],
"values": Array [ "values": Array [
Object { Object {
"slug": "val-1-1", "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",
],
}
`;

View file

@ -24,12 +24,12 @@ describe("Creates variant matrix", () => {
...thirdStep, ...thirdStep,
price: { price: {
...thirdStep.price, ...thirdStep.price,
all: true, mode: "all",
value: price value: price
}, },
stock: { stock: {
...thirdStep.stock, ...thirdStep.stock,
all: true, mode: "all",
value: stock value: stock
} }
}; };
@ -61,8 +61,8 @@ describe("Creates variant matrix", () => {
...thirdStep, ...thirdStep,
price: { price: {
...thirdStep.price, ...thirdStep.price,
all: false,
attribute: attribute.id, attribute: attribute.id,
mode: "attribute",
values: attribute.values.map((attributeValue, attributeValueIndex) => ({ values: attribute.values.map((attributeValue, attributeValueIndex) => ({
slug: attributeValue, slug: attributeValue,
value: (price * (attributeValueIndex + 1)).toString() value: (price * (attributeValueIndex + 1)).toString()
@ -70,7 +70,7 @@ describe("Creates variant matrix", () => {
}, },
stock: { stock: {
...thirdStep.stock, ...thirdStep.stock,
all: true, mode: "all",
value: stock value: stock
} }
}; };
@ -116,13 +116,13 @@ describe("Creates variant matrix", () => {
...thirdStep, ...thirdStep,
price: { price: {
...thirdStep.price, ...thirdStep.price,
all: true, mode: "all",
value: price value: price
}, },
stock: { stock: {
...thirdStep.stock, ...thirdStep.stock,
all: false,
attribute: attribute.id, attribute: attribute.id,
mode: "attribute",
values: attribute.values.map((attributeValue, attributeValueIndex) => ({ values: attribute.values.map((attributeValue, attributeValueIndex) => ({
slug: attributeValue, slug: attributeValue,
value: stock.map( value: stock.map(
@ -173,8 +173,8 @@ describe("Creates variant matrix", () => {
...thirdStep, ...thirdStep,
price: { price: {
...thirdStep.price, ...thirdStep.price,
all: false,
attribute: attribute.id, attribute: attribute.id,
mode: "attribute",
values: attribute.values.map((attributeValue, attributeValueIndex) => ({ values: attribute.values.map((attributeValue, attributeValueIndex) => ({
slug: attributeValue, slug: attributeValue,
value: (price * (attributeValueIndex + 1)).toString() value: (price * (attributeValueIndex + 1)).toString()
@ -182,8 +182,8 @@ describe("Creates variant matrix", () => {
}, },
stock: { stock: {
...thirdStep.stock, ...thirdStep.stock,
all: false,
attribute: attribute.id, attribute: attribute.id,
mode: "attribute",
values: attribute.values.map((attributeValue, attributeValueIndex) => ({ values: attribute.values.map((attributeValue, attributeValueIndex) => ({
slug: attributeValue, slug: attributeValue,
value: stock.map( value: stock.map(

View file

@ -90,12 +90,16 @@ export const thirdStep: ProductVariantCreateFormData = {
values: [0, 4].map(index => attributes[3].values[index]) values: [0, 4].map(index => attributes[3].values[index])
} }
], ],
stock: {
...secondStep.stock,
value: warehouses.map(() => 0)
},
warehouses: warehouses.map(warehouse => warehouse.id) warehouses: warehouses.map(warehouse => warehouse.id)
}; };
const price: AllOrAttribute<string> = { const price: AllOrAttribute<string> = {
all: false,
attribute: thirdStep.attributes[1].id, attribute: thirdStep.attributes[1].id,
mode: "attribute",
value: "", value: "",
values: [ values: [
{ {
@ -109,8 +113,8 @@ const price: AllOrAttribute<string> = {
] ]
}; };
const stock: AllOrAttribute<number[]> = { const stock: AllOrAttribute<number[]> = {
all: false,
attribute: thirdStep.attributes[2].id, attribute: thirdStep.attributes[2].id,
mode: "attribute",
value: [], value: [],
values: [ values: [
{ {

View file

@ -77,7 +77,7 @@ describe("Reducer is able to", () => {
const state = execActions(thirdStep, reducer, [ const state = execActions(thirdStep, reducer, [
{ {
applyPriceOrStockToAll: { applyPriceOrStockToAll: {
all: true mode: "all"
}, },
type: ProductVariantCreateReducerActionType.applyPriceToAll 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.price.value).toBe(price);
expect(state).toMatchSnapshot(); 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", () => { it("select stock for all variants", () => {
const quantity = 45; const quantity = 45;
const warehouseIndex = 1; const warehouseIndex = 1;
const state = execActions(thirdStep, reducer, [ const state = execActions(thirdStep, reducer, [
{ {
applyPriceOrStockToAll: { applyPriceOrStockToAll: {
all: true mode: "all"
}, },
type: ProductVariantCreateReducerActionType.applyStockToAll 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.stock.value[warehouseIndex]).toBe(quantity);
expect(state).toMatchSnapshot(); expect(state).toMatchSnapshot();
}); });
@ -130,7 +150,7 @@ describe("Reducer is able to", () => {
const state = execActions(thirdStep, reducer, [ const state = execActions(thirdStep, reducer, [
{ {
applyPriceOrStockToAll: { applyPriceOrStockToAll: {
all: false mode: "attribute"
}, },
type: ProductVariantCreateReducerActionType.applyPriceToAll 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( expect(state.price.values).toHaveLength(
state.attributes.find(attribute => state.price.attribute === attribute.id) state.attributes.find(attribute => state.price.attribute === attribute.id)
.values.length .values.length
@ -174,7 +194,7 @@ describe("Reducer is able to", () => {
const state = execActions(thirdStep, reducer, [ const state = execActions(thirdStep, reducer, [
{ {
applyPriceOrStockToAll: { applyPriceOrStockToAll: {
all: false mode: "attribute"
}, },
type: ProductVariantCreateReducerActionType.applyStockToAll 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( expect(state.stock.values).toHaveLength(
state.attributes.find(attribute => state.stock.attribute === attribute.id) state.attributes.find(attribute => state.stock.attribute === attribute.id)
.values.length .values.length

View file

@ -335,9 +335,37 @@ function changeWarehouses(
state: ProductVariantCreateFormData, state: ProductVariantCreateFormData,
warehouseId: string warehouseId: string
): ProductVariantCreateFormData { ): 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 { return {
...state, ...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
}; };
} }