Skip to content
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
5 changes: 5 additions & 0 deletions .changeset/bitter-moments-drive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@commercetools/sync-actions': patch
---

When a product-tailorings variant does not define an image array but more than one image should be added, only one addExternalImage action was generated. This fix makes sure, that for each added image an action is generated
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,18 @@ function _buildVariantImagesAction(
oldVariant = {} as ProductVariantTailoring,
newVariant = {} as ProductVariantTailoring
) {
// When before had no `images` property, jsondiffpatch produces a "property added"
// delta [[image-1, ..., image-n]] instead of an array diff { _t: 'a', ... }.
// getDeltaValue would misread a 2-element inner array as [oldValue, newValue],
// so we detect this format and generate addExternalImage actions directly.
if (Array.isArray(diffedImages) && diffedImages.length === 1 && Array.isArray(diffedImages[0])) {
return (diffedImages[0] as Image[]).map((image) => ({
action: 'addExternalImage',
variantId: oldVariant.id,
image,
}));
}

const actions = [];
const matchingImagePairs = findMatchingPairs(
diffedImages,
Expand Down
32 changes: 32 additions & 0 deletions packages/sync-actions/test/product-tailoring-sync.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,38 @@ describe('Actions', () => {
]);
});

test('should build multiple `addExternalImage` action when image array is undefined before', () => {
const before = {
variants: [{ id: 1, assets: [] }],
};
const now = {
variants: [
{
id: 1,
images: [
{ url: '//newimage-1.jpg', dimensions: { w: 400, h: 300 } },
{ url: '//newimage-2.jpg', dimensions: { w: 400, h: 300 } },
],
assets: [],
},
],
};

const actions = productTailoringSync.buildActions(now, before);
expect(actions).toEqual([
{
action: 'addExternalImage',
variantId: 1,
image: { url: '//newimage-1.jpg', dimensions: { w: 400, h: 300 } },
},
{
action: 'addExternalImage',
variantId: 1,
image: { url: '//newimage-2.jpg', dimensions: { w: 400, h: 300 } },
},
]);
});

test('should build `removeImage` action', () => {
const before = {
variants: [
Expand Down
Loading