Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: inventory quantity resets to 0 when any variant or option quantity is undefined #5134

Merged
merged 6 commits into from
Apr 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default async function getProductInventoryAvailableToSellQuantity(product
const variants = await getVariants(productId, collections, true);

if (variants && variants.length) {
return variants.reduce((sum, variant) => sum + variant.inventoryAvailableToSell || 0, 0);
return variants.reduce((sum, variant) => sum + (variant.inventoryAvailableToSell || 0), 0);
}
return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const mockGetVariants = jest.fn().mockName("getVariants");

const internalShopId = "123";
const internalCatalogProductId = "999";
const internalVariantIds = ["875", "874"];
const internalVariantIds = ["875", "874", "879"];

const createdAt = new Date("2018-04-16T15:34:28.043Z");
const updatedAt = new Date("2018-04-17T15:34:28.043Z");
Expand Down Expand Up @@ -95,6 +95,125 @@ const mockVariants = [
}
];

const mockVariantsWithUndefinedInventory = [
{
_id: internalVariantIds[0],
ancestors: [internalCatalogProductId],
barcode: "barcode",
createdAt,
height: 0,
index: 0,
inventoryAvailableToSell: 6,
inventoryManagement: true,
inventoryPolicy: false,
inventoryInStock: 5,
isDeleted: false,
isLowQuantity: true,
isSoldOut: false,
isVisible: true,
length: 0,
lowInventoryWarningThreshold: 0,
metafields: [
{
value: "value",
namespace: "namespace",
description: "description",
valueType: "valueType",
scope: "scope",
key: "key"
}
],
minOrderQuantity: 0,
optionTitle: "Untitled Option",
originCountry: "US",
price: 0,
shopId: internalShopId,
sku: "sku",
taxCode: "0000",
taxDescription: "taxDescription",
title: "Small Concrete Pizza",
updatedAt,
variantId: internalVariantIds[0],
weight: 0,
width: 0
},
{
_id: internalVariantIds[1],
ancestors: [internalCatalogProductId, internalVariantIds[0]],
barcode: "barcode",
height: 2,
index: 0,
inventoryAvailableToSell: 3,
inventoryManagement: true,
inventoryPolicy: true,
inventoryInStock: 5,
isDeleted: false,
isLowQuantity: true,
isSoldOut: false,
isVisible: true,
length: 2,
lowInventoryWarningThreshold: 0,
metafields: [
{
value: "value",
namespace: "namespace",
description: "description",
valueType: "valueType",
scope: "scope",
key: "key"
}
],
minOrderQuantity: 0,
optionTitle: "Awesome Soft Bike",
originCountry: "US",
price: 992.0,
shopId: internalShopId,
sku: "sku",
taxCode: "0000",
taxDescription: "taxDescription",
title: "One pound bag",
variantId: internalVariantIds[1],
weight: 2,
width: 2
}, {
_id: internalVariantIds[2],
ancestors: [internalCatalogProductId, internalVariantIds[0]],
barcode: "barcode",
height: 2,
index: 0,
inventoryManagement: true,
inventoryPolicy: true,
isDeleted: false,
isLowQuantity: true,
isSoldOut: false,
isVisible: true,
length: 2,
lowInventoryWarningThreshold: 0,
metafields: [
{
value: "value",
namespace: "namespace",
description: "description",
valueType: "valueType",
scope: "scope",
key: "key"
}
],
minOrderQuantity: 0,
optionTitle: "Awesome Soft Bike",
originCountry: "US",
price: 992.0,
shopId: internalShopId,
sku: "sku",
taxCode: "0000",
taxDescription: "taxDescription",
title: "One pound bag",
variantId: internalVariantIds[1],
weight: 2,
width: 2
}
];

beforeAll(() => {
rewire$getVariants(mockGetVariants);
});
Expand All @@ -115,3 +234,10 @@ test("expect 0 if all variants have an inventory quantity of 0", async () => {
const spec = await getProductInventoryAvailableToSellQuantity(mockVariants[0], mockCollections, mockVariants);
expect(spec).toEqual(0);
});

// Expect product variant with quantity even if one has undefined inventory
test("expect product variant quantity number even when some have undefined inventory", async () => {
mockGetVariants.mockReturnValueOnce(Promise.resolve(mockVariantsWithUndefinedInventory));
const spec = await getProductInventoryAvailableToSellQuantity(mockVariantsWithUndefinedInventory, mockCollections);
expect(spec).toEqual(9);
});
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default async function getProductInventoryInStockQuantity(productId, coll
const variants = await getVariants(productId, collections, true);

if (variants && variants.length) {
return variants.reduce((sum, variant) => sum + variant.inventoryInStock || 0, 0);
return variants.reduce((sum, variant) => sum + (variant.inventoryInStock || 0), 0);
}
return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const mockGetVariants = jest.fn().mockName("getVariants");

const internalShopId = "123";
const internalCatalogProductId = "999";
const internalVariantIds = ["875", "874"];
const internalVariantIds = ["875", "874", "879"];

const createdAt = new Date("2018-04-16T15:34:28.043Z");
const updatedAt = new Date("2018-04-17T15:34:28.043Z");
Expand Down Expand Up @@ -95,6 +95,125 @@ const mockVariants = [
}
];

const mockVariantsWithUndefinedInventory = [
{
_id: internalVariantIds[0],
ancestors: [internalCatalogProductId],
barcode: "barcode",
createdAt,
height: 0,
index: 0,
inventoryAvailableToSell: 6,
inventoryManagement: true,
inventoryPolicy: false,
inventoryInStock: 5,
isDeleted: false,
isLowQuantity: true,
isSoldOut: false,
isVisible: true,
length: 0,
lowInventoryWarningThreshold: 0,
metafields: [
{
value: "value",
namespace: "namespace",
description: "description",
valueType: "valueType",
scope: "scope",
key: "key"
}
],
minOrderQuantity: 0,
optionTitle: "Untitled Option",
originCountry: "US",
price: 0,
shopId: internalShopId,
sku: "sku",
taxCode: "0000",
taxDescription: "taxDescription",
title: "Small Concrete Pizza",
updatedAt,
variantId: internalVariantIds[0],
weight: 0,
width: 0
},
{
_id: internalVariantIds[1],
ancestors: [internalCatalogProductId, internalVariantIds[0]],
barcode: "barcode",
height: 2,
index: 0,
inventoryAvailableToSell: 3,
inventoryManagement: true,
inventoryPolicy: true,
inventoryInStock: 5,
isDeleted: false,
isLowQuantity: true,
isSoldOut: false,
isVisible: true,
length: 2,
lowInventoryWarningThreshold: 0,
metafields: [
{
value: "value",
namespace: "namespace",
description: "description",
valueType: "valueType",
scope: "scope",
key: "key"
}
],
minOrderQuantity: 0,
optionTitle: "Awesome Soft Bike",
originCountry: "US",
price: 992.0,
shopId: internalShopId,
sku: "sku",
taxCode: "0000",
taxDescription: "taxDescription",
title: "One pound bag",
variantId: internalVariantIds[1],
weight: 2,
width: 2
}, {
_id: internalVariantIds[2],
ancestors: [internalCatalogProductId, internalVariantIds[0]],
barcode: "barcode",
height: 2,
index: 0,
inventoryManagement: true,
inventoryPolicy: true,
isDeleted: false,
isLowQuantity: true,
isSoldOut: false,
isVisible: true,
length: 2,
lowInventoryWarningThreshold: 0,
metafields: [
{
value: "value",
namespace: "namespace",
description: "description",
valueType: "valueType",
scope: "scope",
key: "key"
}
],
minOrderQuantity: 0,
optionTitle: "Awesome Soft Bike",
originCountry: "US",
price: 992.0,
shopId: internalShopId,
sku: "sku",
taxCode: "0000",
taxDescription: "taxDescription",
title: "One pound bag",
variantId: internalVariantIds[1],
weight: 2,
width: 2
}
];

beforeAll(() => {
rewire$getVariants(mockGetVariants);
});
Expand All @@ -115,3 +234,10 @@ test("expect 0 if all variants have an inventory quantity of 0", async () => {
const spec = await getProductInventoryInStockQuantity(mockVariants[0], mockCollections, mockVariants);
expect(spec).toEqual(0);
});

// Expect product variant with quantity even if one has undefined inventory
test("expect product variant quantity number", async () => {
mockGetVariants.mockReturnValueOnce(Promise.resolve(mockVariantsWithUndefinedInventory));
const spec = await getProductInventoryInStockQuantity(mockVariantsWithUndefinedInventory, mockCollections);
expect(spec).toEqual(10);
});
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default async function getVariantInventoryAvailableToSellQuantity(variant
}

if (options && options.length) {
return options.reduce((sum, option) => sum + option.inventoryAvailableToSell || 0, 0);
return options.reduce((sum, option) => sum + (option.inventoryAvailableToSell || 0), 0);
}
return variant.inventoryAvailableToSell || 0;
}
Loading