Repository: ViacomInc/data-point
Pull request #128 (2018-01-08 19:45:36)

Use _.partial less with resolveReducer
<!-- What changes are being made? (What feature/bug is being fixed here?) -->
**What**: Add `manager` parameter to `resolveErrorReducers` function as a first step towards #126; also adds jsdoc to some functions

<!-- Why are these changes necessary? -->
**Why**: #126 

<!-- 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? -->

coveralls: 
[![Coverage Status](https://coveralls.io/builds/14953909/badge)](https://coveralls.io/builds/14953909)

Coverage remained the same at 100.0% when pulling **ce4aa2f8d1dc2a935dbfb2d2ac1b9d7b4e179ca2 on raingerber:use-partial-less-with-resolveReducer** into **7d4f6f9b20f9d86c44dfed2114b9bcf5604b338f on ViacomInc:master**.


Patch:
diff --git a/packages/data-point/lib/entity-types/base-entity/factory.js b/packages/data-point/lib/entity-types/base-entity/factory.js
index 62ea1c9..1849758 100644
--- a/packages/data-point/lib/entity-types/base-entity/factory.js
+++ b/packages/data-point/lib/entity-types/base-entity/factory.js
@@ -4,7 +4,7 @@ const defaultTo = require('lodash/defaultTo')
 const createReducer = require('../../reducer-types').create
 
 /**
- * @param {function} Factory - factory function to create the entity
+ * @param {Function} Factory - factory function to create the entity
  * @param {Object} spec - spec for the Entity
  * @param {string} id - Entity's id
  */
diff --git a/packages/data-point/lib/entity-types/base-entity/resolve.js b/packages/data-point/lib/entity-types/base-entity/resolve.js
index 00516f1..52d31ab 100644
--- a/packages/data-point/lib/entity-types/base-entity/resolve.js
+++ b/packages/data-point/lib/entity-types/base-entity/resolve.js
@@ -7,7 +7,14 @@ const middleware = require('../../middleware')
 
 const utils = require('../../utils')
 
-function resolveErrorReducers (error, accumulator, resolveReducer) {
+/**
+ * @param {Object} manager
+ * @param {Error} error
+ * @param {Accumulator} accumulator
+ * @param {Function} resolveReducer
+ * @returns {Promise<Accumulator>}
+ */
+function resolveErrorReducers (manager, error, accumulator, resolveReducer) {
   const errorReducer = accumulator.reducer.spec.error
   if (utils.reducerIsEmpty(errorReducer)) {
     return Promise.reject(error)
@@ -15,7 +22,11 @@ function resolveErrorReducers (error, accumulator, resolveReducer) {
 
   const errorAccumulator = utils.set(accumulator, 'value', error)
 
-  const reducerResolved = resolveReducer(errorAccumulator, errorReducer)
+  const reducerResolved = resolveReducer(
+    manager,
+    errorAccumulator,
+    errorReducer
+  )
 
   return reducerResolved.then(result =>
     utils.set(accumulator, 'value', result.value)
@@ -24,6 +35,12 @@ function resolveErrorReducers (error, accumulator, resolveReducer) {
 
 module.exports.resolveErrorReducers = resolveErrorReducers
 
+/**
+ * @param {Object} manager
+ * @param {Accumulator} accumulator
+ * @param {Function} reducer
+ * @returns {Accumulator}
+ */
 function createCurrentAccumulator (manager, accumulator, reducer) {
   // get defined source
   const entity = manager.entities.get(reducer.id)
@@ -79,8 +96,17 @@ function resolveMiddleware (manager, name, acc) {
     return reqCtx
   })
 }
+
 module.exports.resolveMiddleware = resolveMiddleware
 
+/**
+ * @param {Object} manager
+ * @param {Function} resolveReducer
+ * @param {Accumulator} accumulator
+ * @param {Function} reducer
+ * @param {Function} mainResolver
+ * @returns {Promise<Accumulator>}
+ */
 function resolveEntity (
   manager,
   resolveReducer,
@@ -106,14 +132,16 @@ function resolveEntity (
     console.time(timeId)
   }
 
+  const resolveReducerBound = _.partial(resolveReducer, manager)
+
   return Promise.resolve(accUid)
     .then(acc => resolveMiddleware(manager, `before`, acc))
     .then(acc =>
       resolveMiddleware(manager, `${reducer.entityType}:before`, acc)
     )
-    .then(acc => resolveReducer(acc, acc.reducer.spec.before))
-    .then(acc => mainResolver(acc, resolveReducer))
-    .then(acc => resolveReducer(acc, acc.reducer.spec.after))
+    .then(acc => resolveReducer(manager, acc, acc.reducer.spec.before))
+    .then(acc => mainResolver(acc, resolveReducerBound))
+    .then(acc => resolveReducer(manager, acc, acc.reducer.spec.after))
     .then(acc =>
       middleware.resolve(manager, `${reducer.entityType}:after`, acc)
     )
@@ -126,7 +154,12 @@ function resolveEntity (
       // attach entity information to help debug
       error.entityId = currentAccumulator.reducer.spec.id
 
-      return resolveErrorReducers(error, currentAccumulator, resolveReducer)
+      return resolveErrorReducers(
+        manager,
+        error,
+        currentAccumulator,
+        resolveReducer
+      )
     })
     .then(resultContext => {
       if (trace === true) {
@@ -141,6 +174,14 @@ function resolveEntity (
 
 module.exports.resolveEntity = resolveEntity
 
+/**
+ * @param {Object} manager
+ * @param {Function} resolveReducer
+ * @param {Accumulator} accumulator
+ * @param {Function} reducer
+ * @param {Function} mainResolver
+ * @returns {Promise<Accumulator>}
+ */
 function resolve (manager, resolveReducer, accumulator, reducer, mainResolver) {
   const hasEmptyConditional = reducer.hasEmptyConditional
 
@@ -148,12 +189,10 @@ function resolve (manager, resolveReducer, accumulator, reducer, mainResolver) {
     return Promise.resolve(accumulator)
   }
 
-  const resolveReducerBound = _.partial(resolveReducer, manager)
-
   if (!reducer.asCollection) {
     return resolveEntity(
       manager,
-      resolveReducerBound,
+      resolveReducer,
       accumulator,
       reducer,
       mainResolver
@@ -173,7 +212,7 @@ function resolve (manager, resolveReducer, accumulator, reducer, mainResolver) {
 
     return resolveEntity(
       manager,
-      resolveReducerBound,
+      resolveReducer,
       itemCtx,
       reducer,
       mainResolver
diff --git a/packages/data-point/lib/reducer-types/reducer-function/resolve.js b/packages/data-point/lib/reducer-types/reducer-function/resolve.js
index d744825..0608df6 100644
--- a/packages/data-point/lib/reducer-types/reducer-function/resolve.js
+++ b/packages/data-point/lib/reducer-types/reducer-function/resolve.js
@@ -7,7 +7,7 @@ const utils = require('../../utils')
  * @param {Object} manager
  * @param {Function} resolveReducer
  * @param {Accumulator} accumulator
- * @param {ReducerObject} reducer
+ * @param {ReducerFunction} reducer
  * @returns {Promise<Accumulator>}
  */
 function resolve (manager, resolveReducer, accumulator, reducer) {
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 d4eff3b..7a7b74a 100644
--- a/packages/data-point/lib/reducer-types/reducer-list/resolve.js
+++ b/packages/data-point/lib/reducer-types/reducer-list/resolve.js
@@ -1,7 +1,6 @@
 'use strict'
 
 const Promise = require('bluebird')
-const partial = require('lodash/partial')
 
 /**
  * @param {Object} manager
@@ -11,13 +10,16 @@ const partial = require('lodash/partial')
  * @returns {Promise<Accumulator>}
  */
 function resolve (manager, resolveReducer, accumulator, reducerList) {
-  if (reducerList.reducers.length === 0) {
+  const reducers = reducerList.reducers
+  if (reducers.length === 0) {
     return Promise.resolve(accumulator)
   }
 
   const result = Promise.reduce(
-    reducerList.reducers,
-    partial(resolveReducer, manager),
+    reducers,
+    (accumulator, reducer) => {
+      return resolveReducer(manager, accumulator, reducer)
+    },
     accumulator
   )
 


Test patch:
diff --git a/packages/data-point/lib/entity-types/base-entity/resolve.test.js b/packages/data-point/lib/entity-types/base-entity/resolve.test.js
index b2eda58..10eb78d 100644
--- a/packages/data-point/lib/entity-types/base-entity/resolve.test.js
+++ b/packages/data-point/lib/entity-types/base-entity/resolve.test.js
@@ -11,11 +11,9 @@ const helpers = require('../../helpers')
 const utils = require('../../utils')
 
 let dataPoint
-let resolveReducerBound
 
 beforeAll(() => {
   dataPoint = FixtureStore.create()
-  resolveReducerBound = helpers.createReducerResolver(dataPoint)
 })
 
 afterEach(() => {
@@ -34,9 +32,10 @@ describe('ResolveEntity.resolveErrorReducers', () => {
       }
     )
     return ResolveEntity.resolveErrorReducers(
+      dataPoint,
       err,
       accumulator,
-      resolveReducerBound
+      resolveReducer
     )
       .catch(reason => reason)
       .then(acc => {
@@ -56,9 +55,10 @@ describe('ResolveEntity.resolveErrorReducers', () => {
       }
     )
     return ResolveEntity.resolveErrorReducers(
+      dataPoint,
       err,
       accumulator,
-      resolveReducerBound
+      resolveReducer
     ).then(acc => {
       expect(acc.value).toEqual('pass')
     })
@@ -139,7 +139,7 @@ describe('ResolveEntity.resolveEntity', () => {
     const reducer = createReducerEntity(createReducer, entityId)
     return ResolveEntity.resolveEntity(
       dataPoint,
-      resolveReducerBound,
+      resolveReducer,
       racc,
       reducer,
       resolver || defaultResolver