feat: Improve how options are defined and handled for product (#7007)

* feat: Improve how options are defined and handled for product

* fix: Updating option values supports passing without id
This commit is contained in:
Stevche Radevski
2024-04-08 15:29:41 +02:00
committed by GitHub
parent 7b4ff1ae57
commit fa5c9bbe8f
18 changed files with 320 additions and 437 deletions
@@ -28,6 +28,10 @@ moduleIntegrationTestRunner({
title: "size",
values: ["large", "small"],
},
{
title: "color",
values: ["red", "blue"],
},
],
})
@@ -182,7 +186,7 @@ moduleIntegrationTestRunner({
expect(productVariant.title).toEqual("new test")
})
it("should update the options of a variant successfully", async () => {
it("should upsert the options of a variant successfully", async () => {
await service.upsertVariants([
{
id: variantOne.id,
@@ -191,12 +195,33 @@ moduleIntegrationTestRunner({
])
const productVariant = await service.retrieveVariant(variantOne.id, {
relations: ["options", "options.option_value", "options.variant"],
relations: ["options"],
})
expect(productVariant.options).toEqual(
expect.arrayContaining([
expect.objectContaining({
option_value: expect.objectContaining({ value: "small" }),
value: "small",
}),
])
)
})
it("should do a partial update on the options of a variant successfully", async () => {
await service.updateVariants(variantOne.id, {
options: { size: "small", color: "red" },
})
const productVariant = await service.retrieveVariant(variantOne.id, {
relations: ["options"],
})
expect(productVariant.options).toEqual(
expect.arrayContaining([
expect.objectContaining({
value: "small",
}),
expect.objectContaining({
value: "red",
}),
])
)
@@ -222,7 +247,7 @@ moduleIntegrationTestRunner({
const beforeDeletedVariants = await service.listVariants(
{ id: variantOne.id },
{
relations: ["options", "options.option_value", "options.variant"],
relations: ["options"],
}
)
@@ -230,7 +255,7 @@ moduleIntegrationTestRunner({
const deletedVariants = await service.listVariants(
{ id: variantOne.id },
{
relations: ["options", "options.option_value", "options.variant"],
relations: ["options"],
withDeleted: true,
}
)
@@ -239,9 +264,7 @@ moduleIntegrationTestRunner({
expect(deletedVariants[0].deleted_at).not.toBeNull()
for (const variantOption of deletedVariants[0].options) {
expect(variantOption.deleted_at).not.toBeNull()
// The value itself should not be affected
expect(variantOption?.option_value?.deleted_at).toBeNull()
expect(variantOption?.deleted_at).toBeNull()
}
})
})
@@ -54,7 +54,6 @@ moduleIntegrationTestRunner({
let productTwo: Product
let productCategoryOne: ProductCategory
let productCategoryTwo: ProductCategory
let variantTwo: ProductVariant
let productTypeOne: ProductType
let productTypeTwo: ProductType
let images = [{ url: "image-1" }]
@@ -112,43 +111,57 @@ moduleIntegrationTestRunner({
productCategoryOne = categories[0]
productCategoryTwo = categories[1]
productOne = testManager.create(Product, {
productOne = service.create({
id: "product-1",
title: "product 1",
status: ProductTypes.ProductStatus.PUBLISHED,
variants: [
{
id: "variant-1",
title: "variant 1",
inventory_quantity: 10,
},
],
})
productTwo = testManager.create(Product, {
productTwo = service.create({
id: "product-2",
title: "product 2",
status: ProductTypes.ProductStatus.PUBLISHED,
categories: [productCategoryOne],
categories: [{ id: productCategoryOne.id }],
collection_id: productCollectionOne.id,
tags: tagsData,
options: [
{
title: "size",
values: ["large", "small"],
},
{
title: "color",
values: ["red", "blue"],
},
],
variants: [
{
id: "variant-2",
title: "variant 2",
inventory_quantity: 10,
},
{
id: "variant-3",
title: "variant 3",
inventory_quantity: 10,
options: {
size: "small",
color: "red",
},
},
],
})
testManager.create(ProductVariant, {
id: "variant-1",
title: "variant 1",
inventory_quantity: 10,
product: productOne,
})
variantTwo = testManager.create(ProductVariant, {
id: "variant-2",
title: "variant 2",
inventory_quantity: 10,
product: productTwo,
})
testManager.create(ProductVariant, {
id: "variant-3",
title: "variant 3",
inventory_quantity: 10,
product: productTwo,
})
await testManager.persistAndFlush([productOne, productTwo])
const res = await Promise.all([productOne, productTwo])
productOne = res[0]
productTwo = res[1]
})
it("should update a product and upsert relations that are not created yet", async () => {
@@ -250,9 +263,7 @@ moduleIntegrationTestRunner({
options: expect.arrayContaining([
expect.objectContaining({
id: expect.any(String),
option_value: expect.objectContaining({
value: data.options[0].values[0],
}),
value: data.options[0].values[0],
}),
]),
}),
@@ -435,7 +446,7 @@ moduleIntegrationTestRunner({
// Note: VariantThree is already assigned to productTwo, that should be deleted
variants: [
{
id: variantTwo.id,
id: productTwo.variants[0].id,
title: "updated-variant",
},
{
@@ -456,7 +467,7 @@ moduleIntegrationTestRunner({
id: expect.any(String),
variants: expect.arrayContaining([
expect.objectContaining({
id: variantTwo.id,
id: productTwo.variants[0].id,
title: "updated-variant",
}),
expect.objectContaining({
@@ -468,6 +479,32 @@ moduleIntegrationTestRunner({
)
})
it("should do a partial update on the options of a variant successfully", async () => {
await service.update(productTwo.id, {
variants: [
{
id: "variant-3",
options: { size: "small", color: "blue" },
},
],
})
const fetchedProduct = await service.retrieve(productTwo.id, {
relations: ["variants", "variants.options"],
})
expect(fetchedProduct.variants[0].options).toEqual(
expect.arrayContaining([
expect.objectContaining({
value: "small",
}),
expect.objectContaining({
value: "blue",
}),
])
)
})
it("should createa variant with id that was passed if it does not exist", async () => {
const updateData = {
id: productTwo.id,
@@ -583,9 +620,7 @@ moduleIntegrationTestRunner({
options: expect.arrayContaining([
expect.objectContaining({
id: expect.any(String),
option_value: expect.objectContaining({
value: data.options[0].values[0],
}),
value: data.options[0].values[0],
}),
]),
}),