Repository: vigetlabs/microcosm
Pull request #397 (2017-08-18 20:05:18)

Pass the domain key to construction options
This PR adds the mount point to the options passed to domains. It can be accessed from `options.key` in `setup` and the constructor.

```javascript
let repo = new Microcosm()

class Planets {
  setup(repo, options) {
    console.log(options.key) // "planets"
  }
}

repo.addDomain('planets', Planets)
```

Fixes https://github.com/vigetlabs/microcosm/issues/396. 

codecov-io: # [Codecov](https://codecov.io/gh/vigetlabs/microcosm/pull/397?src=pr&el=h1) Report
> Merging [#397](https://codecov.io/gh/vigetlabs/microcosm/pull/397?src=pr&el=desc) into [master](https://codecov.io/gh/vigetlabs/microcosm/commit/dd3073352d7883466132f7752e18c3141d80a156?src=pr&el=desc) will **not change** coverage.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/vigetlabs/microcosm/pull/397/graphs/tree.svg?height=150&width=650&token=AaE0FLz9sU&src=pr)](https://codecov.io/gh/vigetlabs/microcosm/pull/397?src=pr&el=tree)

```diff
@@           Coverage Diff           @@
##           master     #397   +/-   ##
=======================================
  Coverage   99.77%   99.77%           
=======================================
  Files          25       25           
  Lines         894      894           
=======================================
  Hits          892      892           
  Misses          2        2
```


| [Impacted Files](https://codecov.io/gh/vigetlabs/microcosm/pull/397?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [src/domain-engine.js](https://codecov.io/gh/vigetlabs/microcosm/pull/397?src=pr&el=tree#diff-c3JjL2RvbWFpbi1lbmdpbmUuanM=) | `100% <100%> (ø)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/vigetlabs/microcosm/pull/397?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/vigetlabs/microcosm/pull/397?src=pr&el=footer). Last update [dd30733...db0e15e](https://codecov.io/gh/vigetlabs/microcosm/pull/397?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 5a246ab3..2f03dfbe 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,5 +1,10 @@
 # Changelog 
 
+## Edge
+
+- Domain::setup and constructors receive the key they were mounted at via
+  `options.key`.
+
 # 12.11.0
 
 - Removed string `ref` in ActionForm, avoiding some edge cases and
@@ -10,7 +15,7 @@
 - Pass `context` to ActionForm constructor, fixing a bug with context
 - Use buble instead of babel for production builds, decreasing build size
 
-# 12.10.0
+## 12.10.0
 
 - Microcosm ships with ES6 and UMD bundles
 - Domains and Effects can implement a `defaults` static object to
@@ -19,7 +24,7 @@
   without a fallback.
 - `repo.append(action, state)` should reconcile history
 
-# 12.9.0
+## 12.9.0
 
 - Added new `repo.parallel` method. This returns an action that
   represents a group of actions processing in parallel.
diff --git a/docs/api/domains.md b/docs/api/domains.md
index 0b218ec3..6b4f547b 100644
--- a/docs/api/domains.md
+++ b/docs/api/domains.md
@@ -133,6 +133,22 @@ var Planets = {
 Setup runs right after a domain is added to a Microcosm, but before it runs
 getInitialState. This is useful for one-time setup instructions.
 
+Options are passed from the second argument of
+`repo.addDomain`. Additionally, `options.key` indicates the key where
+the domain was mounted:
+
+```javascript
+let repo = new Microcosm()
+
+class Planets {
+  setup(repo, options) {
+    console.log(options.key) // "planets"
+  }
+}
+
+repo.addDomain('planets', Planets)
+```
+
 ### `teardown(repo)`
 
 Runs whenever a Microcosm is torn down. This usually happens when a
diff --git a/src/domain-engine.js b/src/domain-engine.js
index 4c4d6210..a685d3b6 100644
--- a/src/domain-engine.js
+++ b/src/domain-engine.js
@@ -66,7 +66,12 @@ class DomainEngine {
   }
 
   add(key: string | KeyPath, config: *, options?: Object) {
-    let deepOptions = merge(this.repo.options, config.defaults, options)
+    let deepOptions = merge(
+      this.repo.options,
+      config.defaults,
+      { key },
+      options
+    )
     let domain: Domain = createOrClone(config, deepOptions, this.repo)
     let keyPath: KeyPath = castPath(key)
 


Test patch:
diff --git a/test/unit/domain/construction.test.js b/test/unit/domain/construction.test.js
index 6524bb48..23be6ef3 100644
--- a/test/unit/domain/construction.test.js
+++ b/test/unit/domain/construction.test.js
@@ -108,4 +108,16 @@ describe('Domain construction', function() {
 
     repo.addDomain('count', Counter, { count: 2 })
   })
+
+  it('passes the mount key to options', function() {
+    expect.assertions(1)
+
+    const repo = new Microcosm()
+
+    repo.addDomain('count', {
+      setup(repo, options) {
+        expect(options.key).toBe('count')
+      }
+    })
+  })
 })