Repository: ViacomInc/data-point
Pull request #379 (2019-08-15 20:23:36)

test(data-point): add tests for Accumulator module
<!--
Thanks for your interest in the project. We appreciate bugs filed and PRs submitted!

Please make sure that you are familiar with and follow the Code of Conduct for
this project (found in the CODE_OF_CONDUCT.md file).

Also, please make sure you're familiar with and follow the instructions in the
contributing guidelines (found in the CONTRIBUTING.md file).

Please fill out the information below to expedite the review and (hopefully)
merge of your pull request!

BREAKING CHANGES:

If your PR includes a breaking change, please submit it with a codemod under
data-point-codemods that will help users upgrade their codebase.

Breaking changes without a codemod will not be accepted unless a codemod is not
viable or does not apply to the specific situation.
-->

<!-- What changes are being made? (What feature/bug is being fixed here?) -->
**What**:

Add tests for Accumulator module
<!-- Why are these changes necessary? -->
**Why**:
there is none

<!-- 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" -->
- [ ] Has Breaking changes
- [ ] Documentation
- [x] Tests
- [x] Ready to be merged <!-- In your opinion, is this ready to be merged as soon as it's reviewed? -->
- [ ] Added username to **all-contributors** list

<!-- feel free to add additional comments -->


codecov[bot]: # [Codecov](https://codecov.io/gh/ViacomInc/data-point/pull/379?src=pr&el=h1) Report
> Merging [#379](https://codecov.io/gh/ViacomInc/data-point/pull/379?src=pr&el=desc) into [canary](https://codecov.io/gh/ViacomInc/data-point/commit/8f07f00aca5a70e5ff73904a412fef462f506150?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/379/graphs/tree.svg?width=650&token=LYxItnrx4i&height=150&src=pr)](https://codecov.io/gh/ViacomInc/data-point/pull/379?src=pr&el=tree)

```diff
@@          Coverage Diff          @@
##           canary   #379   +/-   ##
=====================================
  Coverage     100%   100%           
=====================================
  Files           2      3    +1     
  Lines          81     92   +11     
  Branches        9      9           
=====================================
+ Hits           81     92   +11
```


| [Impacted Files](https://codecov.io/gh/ViacomInc/data-point/pull/379?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [packages/data-point/src/Accumulator.js](https://codecov.io/gh/ViacomInc/data-point/pull/379/diff?src=pr&el=tree#diff-cGFja2FnZXMvZGF0YS1wb2ludC9zcmMvQWNjdW11bGF0b3IuanM=) | `100% <100%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/ViacomInc/data-point/pull/379?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/379?src=pr&el=footer). Last update [8f07f00...8b8cbba](https://codecov.io/gh/ViacomInc/data-point/pull/379?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/packages/data-point/src/Accumulator.js b/packages/data-point/src/Accumulator.js
index ca172ed..069a7b5 100644
--- a/packages/data-point/src/Accumulator.js
+++ b/packages/data-point/src/Accumulator.js
@@ -1,8 +1,8 @@
 /**
- * @class
+ * class
  */
 class Accumulator {
-  constructor(options) {
+  constructor(options = {}) {
     this.value = options.value;
     this.locals = options.locals;
     this.cache = options.cache;
@@ -14,31 +14,21 @@ class Accumulator {
     });
   }
 
+  create() {
+    return Object.create(this);
+  }
+
   set(prop, value) {
     const copy = this.create();
     copy[prop] = value;
     return copy;
   }
 
-  create() {
-    return Object.create(this);
-  }
-
   resolve(reducers) {
-    this.__resolve(this, reducers);
+    return this.__resolve(this, reducers);
   }
 }
 
-/**
- * creates new Accumulator based on spec
- * @param  {Object} spec - accumulator spec
- * @return {Source}
- */
-function create(spec) {
-  return new Accumulator(spec);
-}
-
 module.exports = {
-  Accumulator,
-  create
+  Accumulator
 };
diff --git a/packages/data-point/src/data-point.js b/packages/data-point/src/data-point.js
index 7b9b40f..b1b7438 100644
--- a/packages/data-point/src/data-point.js
+++ b/packages/data-point/src/data-point.js
@@ -1,5 +1,5 @@
 const { createReducer } = require("./create-reducer");
-const Accumulator = require("./Accumulator");
+const { Accumulator } = require("./Accumulator");
 const Resolve = require("./resolve-reducer");
 
 async function resolveFromAccumulator(acc, reducers) {
@@ -8,7 +8,7 @@ async function resolveFromAccumulator(acc, reducers) {
 }
 
 async function resolver(input, reducers, options = {}) {
-  const acc = Accumulator.create({
+  const acc = new Accumulator({
     value: input,
     locals: options.locals,
     resolve: resolveFromAccumulator,


Test patch:
diff --git a/packages/data-point/src/Accumulator.test.js b/packages/data-point/src/Accumulator.test.js
new file mode 100644
index 0000000..4701956
--- /dev/null
+++ b/packages/data-point/src/Accumulator.test.js
@@ -0,0 +1,62 @@
+const { Accumulator } = require("./Accumulator");
+
+describe("Accumulator", () => {
+  describe("constructor", () => {
+    it("should accept empty options object", () => {
+      expect(new Accumulator()).toEqual({});
+    });
+    it("should assign options object", () => {
+      const resolve = () => true;
+      expect(
+        new Accumulator({
+          value: "value",
+          locals: "locals",
+          cache: "cache",
+          tracer: "tracer",
+          resolve
+        })
+      ).toMatchObject({
+        value: "value",
+        locals: "locals",
+        cache: "cache",
+        tracer: "tracer",
+        __resolve: resolve
+      });
+    });
+  });
+
+  describe("create", () => {
+    it("should create new object", () => {
+      const acc1 = new Accumulator({
+        value: "a"
+      });
+      const acc2 = acc1.create();
+      expect(Object.getPrototypeOf(acc2)).toEqual(acc1);
+    });
+  });
+
+  describe("set", () => {
+    it("should create new object and assign property/value", () => {
+      const acc1 = new Accumulator({
+        value: "a"
+      });
+      const acc2 = acc1.set("value", "b");
+      expect(acc1.value).toEqual("a");
+      expect(acc2.value).toEqual("b");
+      expect(Object.getPrototypeOf(acc2)).toEqual(acc1);
+    });
+  });
+  describe("resolve", () => {
+    it("should call internal resolve method", () => {
+      const mockResolve = jest.fn(() => "resolved");
+      const acc = new Accumulator({
+        value: "a",
+        resolve: mockResolve
+      });
+
+      const result = acc.resolve("reducers");
+      expect(result).toEqual("resolved");
+      expect(mockResolve).toBeCalledWith(acc, "reducers");
+    });
+  });
+});