Repository: ViacomInc/data-point
Pull request #354 (2019-07-30 00:01:53)

fix: Fix bug when the input to a reducer list is undefined
<!-- What changes are being made? (What feature/bug is being fixed here?) -->
**What (Bugfix)**: Reducer lists currently skip the first reducer when the initial value is undefined

<!-- Why are these changes necessary? -->
**Why**: #353

<!-- How were these changes implemented? -->
**How**: Use null as a fallback when the initial value for Promise.map is undefined

<!-- Have you done all of these things?  -->
**Checklist**:
<!-- add "N/A" to the end of each line that's irrelevant to your changes -->
<!-- to check an item, place an "x" in the box like so: "- [x] Documentation" -->
- [ ] Documentation N/A
- [x] Tests
- [x] Ready to be merged <!-- In your opinion, is this ready to be merged as soon as it's reviewed? -->
- [x] Added username to **all-contributors** list

<!-- feel free to add additional comments -->


codecov[bot]: # [Codecov](https://codecov.io/gh/ViacomInc/data-point/pull/354?src=pr&el=h1) Report
> Merging [#354](https://codecov.io/gh/ViacomInc/data-point/pull/354?src=pr&el=desc) into [master](https://codecov.io/gh/ViacomInc/data-point/commit/12f2d2d5152b3e9a9067937100d907da76d5aa2a?src=pr&el=desc) will **not change** coverage.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/ViacomInc/data-point/pull/354/graphs/tree.svg?width=650&token=LYxItnrx4i&height=150&src=pr)](https://codecov.io/gh/ViacomInc/data-point/pull/354?src=pr&el=tree)

```diff
@@          Coverage Diff          @@
##           master   #354   +/-   ##
=====================================
  Coverage     100%   100%           
=====================================
  Files         139    139           
  Lines        1981   1982    +1     
  Branches      181    182    +1     
=====================================
+ Hits         1981   1982    +1
```


| [Impacted Files](https://codecov.io/gh/ViacomInc/data-point/pull/354?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [...ta-point/lib/reducer-types/reducer-list/resolve.js](https://codecov.io/gh/ViacomInc/data-point/pull/354/diff?src=pr&el=tree#diff-cGFja2FnZXMvZGF0YS1wb2ludC9saWIvcmVkdWNlci10eXBlcy9yZWR1Y2VyLWxpc3QvcmVzb2x2ZS5qcw==) | `100% <100%> (ø)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/ViacomInc/data-point/pull/354?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/ViacomInc/data-point/pull/354?src=pr&el=footer). Last update [12f2d2d...1ed86e0](https://codecov.io/gh/ViacomInc/data-point/pull/354?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/packages/data-point/lib/reducer-types/reducer-list/resolve.js b/packages/data-point/lib/reducer-types/reducer-list/resolve.js
index e12632c..655cbc2 100644
--- a/packages/data-point/lib/reducer-types/reducer-list/resolve.js
+++ b/packages/data-point/lib/reducer-types/reducer-list/resolve.js
@@ -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


Test patch:
diff --git a/packages/data-point/lib/reducer-types/reducer-list/resolve.test.js b/packages/data-point/lib/reducer-types/reducer-list/resolve.test.js
index eb54e49..f939b72 100644
--- a/packages/data-point/lib/reducer-types/reducer-list/resolve.test.js
+++ b/packages/data-point/lib/reducer-types/reducer-list/resolve.test.js
@@ -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')
+  })
+})