Validate channels only when it has no variants (#4095)

* Validate channels only when it has no variants

* Add test
This commit is contained in:
Patryk Andrzejewski 2023-08-22 09:11:04 +02:00 committed by GitHub
parent 4a66b5f625
commit 8a12d5496f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 3 deletions

View file

@ -28,10 +28,12 @@ describe("validateProductCreateData", () => {
]); ]);
}); });
it("returns errors when there is no prices for channels", () => { it("returns errors when there is no prices for channels in simple product", () => {
// Arrange // Arrange
const data = { const data = {
productType: "something", productType: {
hasVariants: false,
},
name: "something", name: "something",
channelListings: [ channelListings: [
{ id: "chann-1", price: "" }, { id: "chann-1", price: "" },
@ -60,4 +62,24 @@ describe("validateProductCreateData", () => {
}, },
]); ]);
}); });
it("returns errors when there is no prices for channels in product with variants", () => {
// Arrange
const data = {
productType: {
hasVariants: true,
},
name: "something",
channelListings: [
{ id: "chann-1", price: "" },
{ id: "chann-2", price: "" },
],
} as unknown as ProductCreateData;
// Act
const errors = validateProductCreateData(data);
// Assert
expect(errors).toEqual([]);
});
}); });

View file

@ -34,7 +34,9 @@ export const validateProductCreateData = (data: ProductCreateData) => {
errors = [...errors, createEmptyRequiredError("name")]; errors = [...errors, createEmptyRequiredError("name")];
} }
if (data.channelListings) { const { productType, channelListings } = data;
if (!productType.hasVariants && channelListings) {
const emptyPrices = data.channelListings const emptyPrices = data.channelListings
.filter(channel => channel.price?.length === 0) .filter(channel => channel.price?.length === 0)
.map(({ id }) => createEmptyRequiredError(`${id}-channel-price`)); .map(({ id }) => createEmptyRequiredError(`${id}-channel-price`));