Skip to content

Commit

Permalink
fix(commons-actions): changes how value is checked, adds new test
Browse files Browse the repository at this point in the history
  • Loading branch information
lufego committed Apr 25, 2017
1 parent e0056af commit 3497748
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/sync/utils/common-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions test/sync/inventory-sync.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
})

0 comments on commit 3497748

Please sign in to comment.