Skip to content

Commit

Permalink
Fix bug where no data would result in exception. #27
Browse files Browse the repository at this point in the history
  • Loading branch information
kjellmorten committed May 1, 2020
1 parent 6413794 commit 73b4963
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
28 changes: 28 additions & 0 deletions lib/adapter/send-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,34 @@ test('should insert one item', async (t) => {
t.true(updateOne.calledWith({ _id: 'entry:ent3' }, { $set: { ...data, _id } }, { upsert: true }))
})

test('should return noaction when no item', async (t) => {
const updateOne = sinon.stub().returns({
matchedCount: 1, modifiedCount: 1, upsertedCount: 0
})
const connection = createConnection({ updateOne })
const data = { id: 'ent1', type: 'entry', attributes: { title: 'Entry 1' } }
const request = {
action: 'SET',
params: {},
data: undefined,
endpoint: {
collection: 'documents',
db: 'database'
}
}
const expected = {
status: 'noaction',
error: 'No items to update',
data: []
}
const _id = 'entry:ent1'

const response = await send(request, connection)

t.deepEqual(response, expected)
t.is(updateOne.callCount, 0)
})

test('should delete one item', async (t) => {
const deleteOne = sinon.stub().returns({ deletedCount: 1 })
const connection = createConnection({ deleteOne })
Expand Down
4 changes: 3 additions & 1 deletion lib/adapter/send.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ const performOnObjectOrArray = async (data, fn, action) => {
if (Array.isArray(data)) {
const responses = await Promise.all(data.map(fn))
return returnOkOrError(responses, actionName)
} else {
} else if (typeof data === 'object' && data !== null) {
const response = await fn(data)
return returnOkOrError([response], actionName)
} else {
return { status: 'noaction', error: 'No items to update', data: [] }
}
}

Expand Down

0 comments on commit 73b4963

Please sign in to comment.