fix: Ensure sales channel updates don't remove sales channel on other products (#7510)

* fix: Make all product tests pass

* fix: Ensure product update doesnt remove sales channels on other products
This commit is contained in:
Stevche Radevski
2024-05-28 18:24:27 +02:00
committed by GitHub
parent dbb23d20be
commit af7a885b5b
6 changed files with 190 additions and 54 deletions

View File

@@ -45,7 +45,6 @@ const getProductFixture = (overrides) => ({
variants: [
{
title: "Test variant",
inventory_quantity: 10,
prices: [
{
currency_code: "usd",
@@ -1075,13 +1074,15 @@ medusaIntegrationTestRunner({
return [baseProduct.id, salesChannel.id]
},
async () => {
const salesChannel = await simpleSalesChannelFactory(
dbConnection,
{
name: "test name",
description: "test description",
}
)
const salesChannel = (
await api.post(
"/admin/sales-channels",
{
name: "Sales",
},
adminHeaders
)
).data.sales_channel
// Currently the product update doesn't support managing sales channels
const newProduct = (
@@ -1423,7 +1424,6 @@ medusaIntegrationTestRunner({
variants: [
{
title: "Test variant",
inventory_quantity: 10,
prices: [{ currency_code: "usd", amount: 100 }],
},
],
@@ -1446,6 +1446,7 @@ medusaIntegrationTestRunner({
it("Sets variant ranks when creating a product", async () => {
const payload = {
title: "Test product - 1",
handle: "test-1",
description: "test-product-description 1",
images: breaking(
() => ["test-image.png", "test-image-2.png"],
@@ -1456,12 +1457,10 @@ medusaIntegrationTestRunner({
variants: [
{
title: "Test variant 1",
inventory_quantity: 10,
prices: [{ currency_code: "usd", amount: 100 }],
},
{
title: "Test variant 2",
inventory_quantity: 10,
prices: [{ currency_code: "usd", amount: 100 }],
},
],
@@ -1632,7 +1631,6 @@ medusaIntegrationTestRunner({
upc: "test-upc",
created_at: expect.any(String),
id: baseProduct.variants[0].id,
inventory_quantity: 10,
manage_inventory: true,
options: breaking(
() =>
@@ -1866,6 +1864,83 @@ medusaIntegrationTestRunner({
)
})
it("updates multiple products that have the same sales channel", async () => {
const salesChannel = (
await api.post(
"/admin/sales-channels",
{
name: "Sales",
},
adminHeaders
)
).data.sales_channel
await api.post(
`/admin/products/${baseProduct.id}`,
{
sales_channels: [{ id: salesChannel.id }],
},
adminHeaders
)
await api.post(
`/admin/products/${proposedProduct.id}`,
{
sales_channels: [{ id: salesChannel.id }],
},
adminHeaders
)
let res = await api.get(
`/admin/products?fields=*sales_channels&sales_channel_id[]=${salesChannel.id}`,
adminHeaders
)
expect(res.status).toEqual(200)
expect(res.data.products).toEqual([
expect.objectContaining({
id: baseProduct.id,
sales_channels: expect.arrayContaining([
expect.objectContaining({
id: salesChannel.id,
}),
]),
}),
expect.objectContaining({
id: proposedProduct.id,
sales_channels: expect.arrayContaining([
expect.objectContaining({
id: salesChannel.id,
}),
]),
}),
])
await api.post(
`/admin/products/${proposedProduct.id}`,
{
sales_channels: [],
},
adminHeaders
)
res = await api.get(
`/admin/products?fields=*sales_channels&sales_channel_id[]=${salesChannel.id}`,
adminHeaders
)
expect(res.status).toEqual(200)
expect(res.data.products).toEqual([
expect.objectContaining({
id: baseProduct.id,
sales_channels: expect.arrayContaining([
expect.objectContaining({
id: salesChannel.id,
}),
]),
}),
])
})
it("fails to update product with invalid status", async () => {
const payload = {
status: null,
@@ -1896,15 +1971,12 @@ medusaIntegrationTestRunner({
variants: [
{
title: "first",
inventory_quantity: 10,
},
{
title: "second",
inventory_quantity: 10,
},
{
title: "third",
inventory_quantity: 10,
},
],
}
@@ -2528,7 +2600,6 @@ medusaIntegrationTestRunner({
ean: "new-ean",
upc: "new-upc",
barcode: "new-barcode",
inventory_quantity: 10,
prices: [
{
currency_code: "usd",
@@ -2790,7 +2861,6 @@ medusaIntegrationTestRunner({
variants: [
{
title: "Test variant",
inventory_quantity: 10,
prices: [{ currency_code: "usd", amount: 100 }],
},
],
@@ -2821,7 +2891,6 @@ medusaIntegrationTestRunner({
variants: [
{
title: "Test variant",
inventory_quantity: 10,
prices: [{ currency_code: "usd", amount: 100 }],
},
],
@@ -2833,7 +2902,7 @@ medusaIntegrationTestRunner({
expect(error.response.data.message).toMatch(
breaking(
() => "Product with handle base-product already exists.",
() => "Product with handle: base-product already exists."
() => "Product with handle: base-product, already exists."
)
)
}
@@ -2890,7 +2959,7 @@ medusaIntegrationTestRunner({
() =>
`Product_collection with handle ${baseCollection.handle} already exists.`,
() =>
`Product collection with handle: ${baseCollection.handle} already exists.`
`Product collection with handle: ${baseCollection.handle}, already exists.`
)
)
}
@@ -3059,7 +3128,6 @@ medusaIntegrationTestRunner({
variants: [
{
title: "Variant 1",
inventory_quantity: 5,
prices: [
{
currency_code: "usd",
@@ -3069,7 +3137,6 @@ medusaIntegrationTestRunner({
},
{
title: "Variant 2",
inventory_quantity: 20,
prices: [
{
currency_code: "usd",
@@ -3090,7 +3157,6 @@ medusaIntegrationTestRunner({
const createPayload = {
title: "Test batch create variant",
inventory_quantity: 10,
prices: [
{
currency_code: "usd",