diff --git a/src/sync/utils/common-actions.js b/src/sync/utils/common-actions.js index 15ea1a28..26987f1d 100644 --- a/src/sync/utils/common-actions.js +++ b/src/sync/utils/common-actions.js @@ -24,18 +24,20 @@ export function buildBaseAttributesActions ({ const delta = diff[key] const before = oldObj[key] const now = newObj[key] + const hasBefore = oldObj[key] !== null && oldObj[key] !== undefined + const hasNow = newObj[key] !== null && newObj[key] !== undefined if (!delta) return undefined - if (!now && !before) return undefined + if (!hasNow && !hasBefore) return undefined - if (now && !before) // no value previously set + if (hasNow && !hasBefore) // no value previously set return { action: item.action, [actionKey]: now } - if (!now && !{}.hasOwnProperty.call(newObj, key)) // no new value + if (!hasNow && !{}.hasOwnProperty.call(newObj, key)) // no new value return undefined - if (!now && {}.hasOwnProperty.call(newObj, key)) // value unset + if (!hasNow && {}.hasOwnProperty.call(newObj, key)) // value unset return { action: item.action } // We need to clone `before` as `patch` will mutate it diff --git a/test/sync/inventory-sync.spec.js b/test/sync/inventory-sync.spec.js index 00e4892b..f65f930a 100644 --- a/test/sync/inventory-sync.spec.js +++ b/test/sync/inventory-sync.spec.js @@ -60,4 +60,21 @@ test('Sync::inventory', (t) => { t.end() }) + + t.test('should accept 0 as a value', (t) => { + setup() + + const before = { + quantityOnStock: 1, + } + const now = { + quantityOnStock: 0, + } + + const actual = inventorySync.buildActions(now, before) + const expected = [{ action: 'changeQuantity', quantity: 0 }] + t.deepEqual(actual, expected) + + t.end() + }) })