diff --git a/src/bucket/StagingBucket.js b/src/bucket/StagingBucket.js index 665d5c7..c353244 100644 --- a/src/bucket/StagingBucket.js +++ b/src/bucket/StagingBucket.js @@ -80,6 +80,12 @@ module.exports = Class( 'StagingBucket' ) */ 'private _dirty': false, + /** + * Prevent setCommittedValues from bypassing staging + * @type {boolean} + */ + 'private _noStagingBypass': false, + /** * Initializes staging bucket with the provided data bucket @@ -155,6 +161,11 @@ module.exports = Class( 'StagingBucket' ) */ 'public setCommittedValues': function( data /*, ...*/ ) { + if ( this._noStagingBypass ) + { + return this.setValues.apply( this, arguments ); + } + this._bucket.setValues.apply( this._bucket, arguments ); // no use in triggering a pre-update, since these values are @@ -165,6 +176,20 @@ module.exports = Class( 'StagingBucket' ) }, + /** + * Prevent #setCommittedValues from bypassing staging + * + * When set, #setCommittedValues will act as an alias of #setValues. + * + * @return {StagingBucket} self + */ + 'public forbidBypass'() + { + this._noStagingBypass = true; + return this; + }, + + /** * Determine whether values have changed * diff --git a/test/bucket/StagingBucketTest.js b/test/bucket/StagingBucketTest.js index 4f68b6e..94a3833 100644 --- a/test/bucket/StagingBucketTest.js +++ b/test/bucket/StagingBucketTest.js @@ -26,6 +26,7 @@ const { Class } = require( 'easejs' ); const root = require( '../../' ); const expect = require( 'chai' ).expect; +const sinon = require( 'sinon' ); const { Bucket, @@ -161,6 +162,48 @@ describe( 'StagingBucket', () => } ); } ); } ); + + + describe( "#setCommittedValues", () => + { + it( "bypasses staging bucket without no bypass flag", () => + { + const b = createStubBucket(); + const bmock = sinon.mock( b ); + const data = { foo: [ "bar" ] }; + const sut = Sut( b ); + + bmock.expects( 'setValues' ) + .once() + .withExactArgs( data ); + + sut.setCommittedValues( data ); + + // no diff if bypassed + expect( sut.getDiff() ).to.deep.equal( {} ); + + bmock.verify(); + } ); + + + it( "does not bypasses staging bucket with no bypass flag", () => + { + const b = createStubBucket(); + const bmock = sinon.mock( b ); + const data = { foo: [ "bar" ] }; + const sut = Sut( b ); + + bmock.expects( 'setValues' ).never(); + + sut.forbidBypass(); + sut.setCommittedValues( data ); + + // should have been staged + expect( sut.getDiff() ).to.deep.equal( data ); + + bmock.verify(); + } ); + } ); } );