Skip to content

Commit

Permalink
fix: Fix bug when the input to a reducer list is undefined (#354)
Browse files Browse the repository at this point in the history
  • Loading branch information
raingerber authored Jul 30, 2019
1 parent 12f2d2d commit 82c3b95
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ function resolve (manager, resolveReducer, accumulator, reducerList) {
return Promise.resolve(undefined)
}

const initialValue =
accumulator.value === undefined ? null : accumulator.value
const result = Promise.reduce(
reducers,
(value, reducer) => {
const itemContext = utils.set(accumulator, 'value', value)
return resolveReducer(manager, itemContext, reducer)
},
accumulator.value
initialValue
)

return result
Expand Down
39 changes: 39 additions & 0 deletions packages/data-point/lib/reducer-types/reducer-list/resolve.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,42 @@ describe('resolve#reducer.resolve - reducer request', () => {
})
})
})

describe('resolve#reducer.resolve - with falsy input', () => {
const testFalsyInput = async (inputValue, expectedValue) => {
const accumulator = AccumulatorFactory.create({
value: inputValue
})

const functionA = jest.fn(input => `${input}1`)
const functionB = jest.fn(input => `${input}2`)

const reducerList = createReducerList(createReducer, [functionA, functionB])

const result = await resolveReducerList(
manager,
resolveReducer,
accumulator,
reducerList
)
expect(result).toBe(expectedValue)
expect(functionA).toHaveBeenCalledTimes(1)
expect(functionB).toHaveBeenCalledTimes(1)
}

test('with undefined as input', () => {
return testFalsyInput(undefined, 'null12')
})

test('with null as input', () => {
return testFalsyInput(null, 'null12')
})

test('with zero as input', () => {
return testFalsyInput(0, '012')
})

test('with an empty string as input', () => {
return testFalsyInput('', '12')
})
})

0 comments on commit 82c3b95

Please sign in to comment.