From 7caa61750de8da4d9276ff3e5b8898f94a22db3c Mon Sep 17 00:00:00 2001 From: Nathan Hunzaker Date: Mon, 31 Oct 2016 15:07:32 -0400 Subject: [PATCH] this.props should be old props in `Presenter::update` --- CHANGELOG.md | 4 ++++ src/addons/presenter.js | 28 +++++++++++++++++++++------- test/addons/presenter.test.js | 18 ++++++++++++++++++ 3 files changed, 43 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8c1ffb6f..51da9558 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## 10.3.5 + +- `this.props` within `Presenter::update` should be the old props. + ## 10.3.4 - Fixed internal loop iteration bug where change emissions of a diff --git a/src/addons/presenter.js b/src/addons/presenter.js index faad15b6..7b9f3cd6 100644 --- a/src/addons/presenter.js +++ b/src/addons/presenter.js @@ -28,6 +28,19 @@ class Presenter extends React.Component { this.render = wrappedRender } + _setRepo (repo) { + this.repo = repo + this.setup(repo, this.props) + } + + _isImpure () { + if (this.props.hasOwnProperty('pure')) { + return this.props.pure !== true + } + + return this.repo.pure !== true + } + /** * Called when a presenter is created, before it has calculated a view model. * Useful for fetching data and other prep-work. @@ -51,6 +64,12 @@ class Presenter extends React.Component { // NOOP } + componentWillReceiveProps (next) { + if (this._isImpure() || !shallowEqual(next, this.props)) { + this.update(this.repo, next) + } + } + /** * Opposite of setup. Useful for cleaning up side-effects. * @@ -140,7 +159,7 @@ class PresenterContext extends React.Component { } componentWillMount () { - this.props.presenter.setup(this.repo, this.safeProps(this.props)) + this.props.presenter._setRepo(this.repo) this.updatePropMap(this.props) this.updateState() @@ -158,7 +177,6 @@ class PresenterContext extends React.Component { componentWillReceiveProps (next) { if (this.isImpure() || !shallowEqual(next, this.props)) { this.updatePropMap(next) - this.props.presenter.update(this.repo, this.safeProps(next)) } this.updateState() @@ -177,11 +195,7 @@ class PresenterContext extends React.Component { } isImpure () { - if (this.props.hasOwnProperty('pure')) { - return this.props.pure !== true - } - - return this.repo.pure !== true + return this.props.presenter._isImpure() } safeProps (props) { diff --git a/test/addons/presenter.test.js b/test/addons/presenter.test.js index 9a031c1e..041a0bcc 100644 --- a/test/addons/presenter.test.js +++ b/test/addons/presenter.test.js @@ -510,3 +510,21 @@ test('setup is called before the initial model', function () { expect(sequence).toEqual(['setup', 'model']) }) + +describe('update', function() { + + test('it has access to the old props when update is called', function () { + const callback = jest.fn() + + class Test extends Presenter { + update (repo, { color }) { + callback(this.props.color, color) + } + } + + mount(

Hey

).setProps({ color: 'blue' }) + + expect(callback).toHaveBeenCalledWith('red', 'blue') + }) + +})