Skip to content

Commit

Permalink
fix(product-actions): use correct uniq function to ensure unique sfa …
Browse files Browse the repository at this point in the history
…attributes
  • Loading branch information
emmenko committed Oct 14, 2016
1 parent d04c379 commit e9f98a3
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"jsondiffpatch": "emmenko/jsondiffpatch#patch-webpack",
"lodash.flatten": "^4.2.0",
"lodash.foreach": "^4.2.0",
"lodash.uniq": "^4.2.1",
"lodash.uniqwith": "^4.5.0",
"loose-envify": "^1.1.0"
},
"devDependencies": {
Expand Down
12 changes: 8 additions & 4 deletions src/sync/product-actions.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable max-len */
import forEach from 'lodash.foreach'
import unique from 'lodash.uniq'
import uniqWith from 'lodash.uniqwith'
import * as diffpatcher from './utils/diffpatcher'
import {
buildBaseAttributesActions,
Expand Down Expand Up @@ -152,9 +152,13 @@ export function actionsMapAttributes (
}
})

// Ensure we have each action only once per product.
// Use string representation of object to allow `===` on array objects
return unique(actions, action => JSON.stringify(action))
// Ensure that an action is unique.
// This is especially necessary for SFA attributes.
return uniqWith(actions, (a, b) =>
a.action === b.action &&
a.name === b.name &&
a.variantId === b.variantId
)
}

export function actionsMapImages (diff, oldObj, newObj) {
Expand Down
42 changes: 38 additions & 4 deletions test/sync/product-sync-variants.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,30 +113,64 @@ test('Sync::product::variants', (t) => {
id: 1,
attributes: [
{ name: 'color', value: 'red' },
{ name: 'size', value: 'M' },
{ name: 'weigth', value: '1' },
],
},
variants: [],
variants: [
{
id: 2,
attributes: [
{ name: 'color', value: 'red' },
{ name: 'size', value: 'M' },
{ name: 'weigth', value: '2' },
],
},
],
}

const now = {
id: '123',
masterVariant: {
id: 1,
attributes: [
// new
{ name: 'vendor', value: 'ferrari' },
// changed
{ name: 'color', value: 'yellow' },
// removed
{ name: 'size', value: undefined },
// normal attribute
{ name: 'weigth', value: '3' },
],
},
variants: [],
variants: [
{
id: 2,
attributes: [
// new
{ name: 'vendor', value: 'ferrari' },
// changed
{ name: 'color', value: 'yellow' },
// removed
{ name: 'size', value: undefined },
// normal attribute
{ name: 'weigth', value: '4' },
],
},
],
}

const actions = productsSync.buildActions(now, before, {
sameForAllAttributeNames: ['vendor'],
sameForAllAttributeNames: ['vendor', 'color', 'size'],
})

t.deepEqual(actions, [
{ action: 'setAttributeInAllVariants', name: 'vendor', value: 'ferrari' },
{ action: 'setAttribute', variantId: 1, name: 'color', value: 'yellow' },
{ action: 'setAttributeInAllVariants', name: 'color', value: 'yellow' },
{ action: 'setAttributeInAllVariants', name: 'size', value: undefined },
{ action: 'setAttribute', variantId: 1, name: 'weigth', value: '3' },
{ action: 'setAttribute', variantId: 2, name: 'weigth', value: '4' },
])
t.end()
})
Expand Down

0 comments on commit e9f98a3

Please sign in to comment.