Skip to content

Commit

Permalink
feat: allow withMutation() nesting (#456)
Browse files Browse the repository at this point in the history
  • Loading branch information
vonagam authored and jquense committed Feb 10, 2019
1 parent 851d421 commit e53ea8c
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/mixed.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,10 @@ const proto = (SchemaType.prototype = {
},

withMutation(fn) {
let before = this._mutate;
this._mutate = true;
let result = fn(this);
this._mutate = false;
this._mutate = before;
return result;
},

Expand Down
49 changes: 49 additions & 0 deletions test/mixed.js
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,55 @@ describe('Mixed Types ', () => {
});
});

describe('withMutation', () => {
it('should pass the same instance to a provided function', () => {
let inst = mixed();
let func = sinon.spy();

inst.withMutation(func);

func.should.have.been.calledOnceWithExactly(inst);
});

it('should temporarily make mutable', () => {
let inst = mixed();

let update = () => {
inst.withMutation(inst => {
inst.test('a', () => true);
});
};

update.should.increase(() => inst.tests.length).by(1);
});

it('should return immutability', () => {
let inst = mixed();
inst.withMutation(() => {});

let update = () => {
inst.test('a', () => true);
};

update.should.not.increase(() => inst.tests.length);
});

it('should work with nesting', () => {
let inst = mixed();

let update = () => {
inst.withMutation(inst => {
inst.withMutation(inst => {
inst.test('a', () => true);
});
inst.test('b', () => true);
});
};

update.should.increase(() => inst.tests.length).by(2);
});
});

describe('concat', () => {
let next;
let inst = object({
Expand Down

0 comments on commit e53ea8c

Please sign in to comment.