diff --git a/src/products/components/ProductUpdatePage/formChannels.test.ts b/src/products/components/ProductUpdatePage/formChannels.test.ts new file mode 100644 index 000000000..2f114e013 --- /dev/null +++ b/src/products/components/ProductUpdatePage/formChannels.test.ts @@ -0,0 +1,98 @@ +import { updateChannelsInput } from "./formChannels"; + +describe("ProductUpdatePage - fromChannels", () => { + describe("updateChannelsInput", () => { + const channel = { + channelId: "Q2hhbm5lbDox", + availableForPurchaseDate: null, + __typename: "ProductChannelListing", + isPublished: true, + publicationDate: "2020-01-01", + isAvailableForPurchase: false, + availableForPurchase: null, + visibleInListings: true, + channel: { + __typename: "Channel", + id: "Q2hhbm5lbDox", + name: "Default channel", + currencyCode: "USD", + }, + }; + + it("should update availableForPurchaseDate if isAvailableForPurchase is set to true", () => { + const input = { + removeChannels: [], + updateChannels: [channel], + }; + const data = { + availableForPurchase: "2020-10-01", + isAvailableForPurchase: true, + isPublished: true, + publicationDate: "2020-01-01", + visibleInListings: true, + }; + + const result = updateChannelsInput(input, data, channel.channelId); + + expect(result).toEqual({ + removeChannels: [], + updateChannels: [ + { + ...channel, + isAvailableForPurchase: true, + availableForPurchaseDate: "2020-10-01", + availableForPurchase: "2020-10-01", + }, + ], + }); + }); + + it("should set availableForPurchaseDate to null if isAvailableForPurchase is set to false", () => { + const oldData = { + removeChannels: [], + updateChannels: [channel], + }; + const newData = { + availableForPurchase: "2020-10-01", + isAvailableForPurchase: false, + isPublished: true, + publicationDate: "2020-01-01", + visibleInListings: true, + }; + + const result = updateChannelsInput(oldData, newData, channel.channelId); + + expect(result).toEqual({ + removeChannels: [], + updateChannels: [ + { + ...channel, + availableForPurchaseDate: null, + availableForPurchase: "2020-10-01", + }, + ], + }); + }); + + it("should not update listing if channel id do not match", () => { + const input = { + removeChannels: [], + updateChannels: [channel], + }; + const data = { + availableForPurchase: "2020-10-01", + isAvailableForPurchase: true, + isPublished: true, + publicationDate: "2020-01-01", + visibleInListings: true, + }; + + const result = updateChannelsInput(input, data, "42"); + + expect(result).toEqual({ + removeChannels: [], + updateChannels: [channel], + }); + }); + }); +}); diff --git a/src/products/components/ProductUpdatePage/formChannels.ts b/src/products/components/ProductUpdatePage/formChannels.ts index 4de7e53ba..88b7a0e1c 100644 --- a/src/products/components/ProductUpdatePage/formChannels.ts +++ b/src/products/components/ProductUpdatePage/formChannels.ts @@ -19,6 +19,29 @@ const emptyListing: Omit = { visibleInListings: false, }; +export const updateChannelsInput = ( + input: ProductChannelListingUpdateInput, + data: ChannelOpts, + id: string, +) => { + const mergeListings = (listing: ProductChannelListingAddInput) => { + if (listing.channelId === id) { + return { + ...listing, + ...data, + availableForPurchaseDate: data.isAvailableForPurchase + ? data.availableForPurchase + : null, + }; + } + return listing; + }; + return { + ...input, + updateChannels: input.updateChannels.map(mergeListings), + }; +}; + export function useProductChannelListingsForm( product: Pick, triggerChange: () => void, @@ -40,18 +63,14 @@ export function useProductChannelListingsForm( touched.current = uniq([...touched.current, id]); }; - const handleChannelChange = useCallback((id: string, data: ChannelOpts) => { - setChannels(prevData => ({ - ...prevData, - updateChannels: prevData.updateChannels.map(prevListing => - prevListing.channelId === id - ? { ...prevListing, ...data } - : prevListing, - ), - })); - triggerChange(); - touch(id); - }, []); + const handleChannelChange = useCallback( + (id: string, data: ChannelOpts) => { + setChannels(input => updateChannelsInput(input, data, id)); + triggerChange(); + touch(id); + }, + [setChannels, triggerChange], + ); const handleChannelListUpdate: ProductChannelsListingDialogSubmit = useCallback( ({ added, removed }) => {