Skip to content

Commit

Permalink
fix(product-actions): correctly pass original obj for patching long t…
Browse files Browse the repository at this point in the history
…ext diff
  • Loading branch information
emmenko committed Oct 17, 2016
1 parent 1ba13f2 commit 50c663c
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 15 deletions.
33 changes: 21 additions & 12 deletions src/sync/product-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -328,8 +328,12 @@ function _buildNewSetAttributeAction (id, el, sameForAllAttributeNames) {
return action
}

function _buildSetAttributeAction (diffedValue, oldVariant, attribute,
sameForAllAttributeNames) {
function _buildSetAttributeAction (
diffedValue,
oldVariant,
attribute,
sameForAllAttributeNames
) {
if (!attribute) return undefined

const action = {
Expand All @@ -338,13 +342,18 @@ function _buildSetAttributeAction (diffedValue, oldVariant, attribute,
name: attribute.name,
}

// Used as original object for patching long diff text
const oldAttribute = oldVariant.attributes.find(
a => a.name === attribute.name
) || {}

if (sameForAllAttributeNames.indexOf(attribute.name) !== -1) {
Object.assign(action, { action: 'setAttributeInAllVariants' })
delete action.variantId
}

if (Array.isArray(diffedValue))
action.value = diffpatcher.getDeltaValue(diffedValue, attribute.value)
action.value = diffpatcher.getDeltaValue(diffedValue, oldAttribute.value)

else
// LText: value: {en: "", de: ""}
Expand All @@ -355,7 +364,7 @@ function _buildSetAttributeAction (diffedValue, oldVariant, attribute,

if (typeof diffedValue === 'string')
// normal
action.value = diffpatcher.getDeltaValue(diffedValue, attribute.value)
action.value = diffpatcher.getDeltaValue(diffedValue, oldAttribute.value)

else if (diffedValue.centAmount || diffedValue.currencyCode)
// Money
Expand All @@ -382,16 +391,16 @@ function _buildSetAttributeAction (diffedValue, oldVariant, attribute,
Object.assign(action, { value: attribute.value })
} else {
// LText
const attrib = oldVariant.attributes.find(
a => a.name === attribute.name
)

const text = attrib ? attrib.value : null
forEach(diffedValue, (localValue, lang) => {
text[lang] = diffpatcher.getDeltaValue(localValue)
})
const updatedValue = Object.keys(diffedValue).reduce((acc, lang) => {
const patchedValue = diffpatcher.getDeltaValue(
diffedValue[lang],
acc[lang]
)
return Object.assign(acc, { [lang]: patchedValue })
}, Object.assign({}, oldAttribute.value))

action.value = text
action.value = updatedValue
}

return action
Expand Down
6 changes: 3 additions & 3 deletions src/sync/utils/diffpatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export function patch (obj, delta) {
return diffpatcher.patch(obj, delta)
}

export function getDeltaValue (arr, obj) {
export function getDeltaValue (arr, originalObject) {
if (!Array.isArray(arr))
throw new Error('Expected array to extract delta value')

Expand All @@ -44,11 +44,11 @@ export function getDeltaValue (arr, obj) {
if (arr.length === 3 && arr[2] === 0) return undefined // delete

if (arr.length === 3 && arr[2] === 2) { // text diff
if (!obj)
if (!originalObject)
throw new Error('Cannot apply patch to long text diff. ' +
'Missing original object.')
// try to apply patch to given object based on delta value
return jsondiffpatch.patch(obj, arr)
return jsondiffpatch.patch(originalObject, arr)
}

if (arr.length === 3 && arr[2] === 3) // array move
Expand Down
58 changes: 58 additions & 0 deletions test/sync/product-sync-base.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,62 @@ test('Sync::product::base', (t) => {
])
t.end()
})

t.test('should build base actions for long diff text', (t) => {
setup()

const longText = `
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Nunc ultricies fringilla tortor eu egestas.
Praesent rhoncus molestie libero, eu tempor sapien placerat id.
Donec commodo nunc sed nulla scelerisque, eu pulvinar augue egestas.
Donec at leo dolor. Cras at molestie arcu.
Sed non fringilla quam, sit amet ultricies massa.
Donec luctus tempus erat, ut suscipit elit varius nec.
Mauris dolor enim, aliquet sed nulla et, dignissim lobortis augue.
Proin pharetra magna eu neque semper tristique sed.
`

/* eslint-disable max-len */
const before = {
name: {
en: longText,
},
slug: {
en: longText,
},
description: {
en: longText,
},
}
const now = {
name: {
en: `Hello, ${longText}`,
},
slug: {
en: `Hello, ${longText}`,
},
description: {
en: `Hello, ${longText}`,
},
}
/* eslint-enable max-len */
const actions = productsSync.buildActions(now, before)

t.deepEqual(actions, [
{
action: 'changeName',
name: now.name,
},
{
action: 'changeSlug',
slug: now.slug,
},
{
action: 'setDescription',
description: now.description,
},
])
t.end()
})
})
88 changes: 88 additions & 0 deletions test/sync/product-sync-variants.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -480,4 +480,92 @@ test('Sync::product::variants', (t) => {
t.deepEqual(actions, [], 'should not generate any action')
t.end()
})

t.test('should build `setAttribute` action text/ltext attributes ' +
'with long text',
(t) => {
setup()

const longText = `
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Nunc ultricies fringilla tortor eu egestas.
Praesent rhoncus molestie libero, eu tempor sapien placerat id.
Donec commodo nunc sed nulla scelerisque, eu pulvinar augue egestas.
Donec at leo dolor. Cras at molestie arcu.
Sed non fringilla quam, sit amet ultricies massa.
Donec luctus tempus erat, ut suscipit elit varius nec.
Mauris dolor enim, aliquet sed nulla et, dignissim lobortis augue.
Proin pharetra magna eu neque semper tristique sed.
`

const newLongText = `Hello, ${longText}`

/* eslint-disable max-len */
const before = {
masterVariant: {
id: 1,
attributes: [
{
name: 'text',
value: longText,
},
],
},
variants: [
{
id: 2,
attributes: [
{
name: 'ltext',
value: {
en: longText,
},
},
],
},
],
}
const now = {
masterVariant: {
id: 1,
attributes: [
{
name: 'text',
value: newLongText,
},
],
},
variants: [
{
id: 2,
attributes: [
{
name: 'ltext',
value: {
en: newLongText,
},
},
],
},
],
}
/* eslint-enable max-len */
const actions = productsSync.buildActions(now, before)

t.deepEqual(actions, [
{
action: 'setAttribute',
variantId: 1,
name: 'text',
value: newLongText,
},
{
action: 'setAttribute',
variantId: 2,
name: 'ltext',
value: { en: newLongText },
},
])
t.end()
})
})

0 comments on commit 50c663c

Please sign in to comment.