From c817ddd17d03c36eb8288db62276bd7296647b73 Mon Sep 17 00:00:00 2001 From: Will Goto Date: Thu, 3 Oct 2019 11:56:17 -0700 Subject: [PATCH 1/2] Create modelDidUpdate method --- src/models/Model.js | 7 +++++++ tests/models/Model.test.js | 14 ++++++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/models/Model.js b/src/models/Model.js index e691b88..ce72635 100644 --- a/src/models/Model.js +++ b/src/models/Model.js @@ -67,6 +67,7 @@ class Model extends Publisher { setState(nextState) { const { prev, next, diff } = this._diffState(nextState); + const prevState = { ...this.state }; if (diff) { // FOR LOGGING @@ -76,10 +77,16 @@ class Model extends Publisher { // console.groupEnd(); Object.assign(this.state, nextState); + this.modelDidUpdate(prevState); this.publish(); } } + modelDidUpdate() { + // Template method for invoking behavior after + // a model has correctly updated state. + } + //================== // INTERNAL METHODS diff --git a/tests/models/Model.test.js b/tests/models/Model.test.js index 76ab4c9..9b0aaf3 100644 --- a/tests/models/Model.test.js +++ b/tests/models/Model.test.js @@ -5,7 +5,8 @@ import Model from '../../src/models/Model'; // TEST CONSTANTS //================ -const obj = { id: '1' }; +const id = '1'; +const obj = { id }; const arr = []; @@ -41,7 +42,7 @@ describe('Model', () => { let modelWithDefaultState; beforeEach(() => { - model = new Test({ id: '1', obj, arr }); + model = new Test({ id, obj, arr }); modelWithoutState = new TestWithoutState(); modelWithDefaultState = new TestWithDefaultState(); }); @@ -120,6 +121,7 @@ describe('Model', () => { describe('setState', () => { beforeEach(() => { + spyOn(model, 'modelDidUpdate'); spyOn(model, 'publish'); }); @@ -132,6 +134,10 @@ describe('Model', () => { expect(model.state.id).toBe('2'); }); + it('should call modelDidUpdate with the previous state', () => { + expect(model.modelDidUpdate).toBeCalledWith({ id, test: true, obj, arr }); + }); + it('should call publish', () => { expect(model.publish).toBeCalled(); }); @@ -147,6 +153,10 @@ describe('Model', () => { expect(model.state.obj.id).toBe('2'); }); + it('should not call modelDidUpdate', () => { + expect(model.modelDidUpdate).not.toBeCalled(); + }); + it('should not call publish', () => { expect(model.publish).not.toBeCalled(); }); From db3bf959a1b1806f39b97a58a8b4feb14c1784c3 Mon Sep 17 00:00:00 2001 From: Will Goto Date: Thu, 3 Oct 2019 12:03:44 -0700 Subject: [PATCH 2/2] Bump version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 7220598..d6407b4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-axiom", - "version": "0.4.0", + "version": "0.4.1", "description": "React Axiom is a way to use models with React.", "repository": "https://github.com/wgoto/react-axiom", "main": "lib/index.js",