Repository: vigetlabs/microcosm (js)

Issue:
Presenter context between setup() and intercept() mismatch
**What kind of issue is this?** (check at least one)

- [x] Report a bug
- [ ] Feature Request
- [ ] Question
- [ ] Discussion
- [ ] Documentation
- [ ] Other, please describe:

**What version of Microcosm is this for**

12.9.0-beta

**Steps to Reproduce/Example** (if applicable)

Failing spec added [here](https://github.com/vigetlabs/microcosm/commit/604acafa31cedcfb87126753a6f1dbd0cfe0e052)

<!-- What is the expected behaviour? -->

Presenter `this` context should be shared between `setup()` and `intercept()`.

<!-- What is happening instead? -->

It looks like logging `this` in `setup()` returns an instance of the Presenter like I expected, but logging `this` in `intercept()` returns an instance of `PresenterMediator` so any attributes defined on `this` in `setup()` are lost in `intercept()`.


Pull request description:
Presenter context bug
Presenter `intercept` hooks were getting invoked in the wrong context.

Fixes #363

Patch:
diff --git a/src/addons/presenter.js b/src/addons/presenter.js
index 89b58e0d..be2d6dde 100644
--- a/src/addons/presenter.js
+++ b/src/addons/presenter.js
@@ -270,7 +270,7 @@ class PresenterMediator extends React.PureComponent {
       let handler = mediator.getHandler(taggedIntent)
 
       if (handler) {
-        return handler.call(mediator, mediator.repo, ...params)
+        return handler.call(this.presenter, mediator.repo, ...params)
       }
 
       mediator = mediator.getParent()


Test patch:
diff --git a/test/addons/presenter.test.js b/test/addons/presenter.test.js
index e60cab13..92cc2fe9 100644
--- a/test/addons/presenter.test.js
+++ b/test/addons/presenter.test.js
@@ -1187,6 +1187,26 @@ describe('intercepting actions', function() {
 
     expect(test).toHaveBeenCalled()
   })
+
+  it('shares context between setup() and intercept()', function() {
+    class Parent extends Presenter {
+      setup() {
+        this.foo = 'bar'
+      }
+
+      intercept() {
+        return {
+          test: this.assertionFunction
+        }
+      }
+
+      assertionFunction() {
+        expect(this.foo).toEqual('bar')
+      }
+    }
+
+    mount(<Parent />).instance().send('test')
+  })
 })
 
 describe('forks', function() {