Skip to content

Commit

Permalink
Merge pull request #57 from wgoto/did-update
Browse files Browse the repository at this point in the history
Create modelDidUpdate method
  • Loading branch information
wGoto authored Oct 3, 2019
2 parents ec25b7a + db3bf95 commit be9ff82
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
7 changes: 7 additions & 0 deletions src/models/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
14 changes: 12 additions & 2 deletions tests/models/Model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import Model from '../../src/models/Model';
// TEST CONSTANTS
//================

const obj = { id: '1' };
const id = '1';
const obj = { id };
const arr = [];


Expand Down Expand Up @@ -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();
});
Expand Down Expand Up @@ -120,6 +121,7 @@ describe('Model', () => {

describe('setState', () => {
beforeEach(() => {
spyOn(model, 'modelDidUpdate');
spyOn(model, 'publish');
});

Expand All @@ -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();
});
Expand All @@ -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();
});
Expand Down

0 comments on commit be9ff82

Please sign in to comment.