From ddffb4e301860e0e1cacddae043a801ea38a43c8 Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Tue, 19 Apr 2016 11:29:14 -0400 Subject: [PATCH] No failure event if failures have not changed in ValidStateMonitor * src/validate/ValidStateMonitor.js (mergeFailures): Return count of new failures * test/validate/ValidStateMonitorTest.js: Respective test added --- src/validate/ValidStateMonitor.js | 14 +++++++++----- test/validate/ValidStateMonitorTest.js | 16 ++++++++++++++++ 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/validate/ValidStateMonitor.js b/src/validate/ValidStateMonitor.js index 586146e..cc79c93 100644 --- a/src/validate/ValidStateMonitor.js +++ b/src/validate/ValidStateMonitor.js @@ -55,11 +55,10 @@ module.exports = Class( 'ValidStateMonitor' ) */ 'public update': function( data, failures ) { - var fixed = this.detectFixes( data, this._failures, failures ); + var fixed = this.detectFixes( data, this._failures, failures ), + count_new = this.mergeFailures( this._failures, failures ); - this.mergeFailures( this._failures, failures ); - - if ( this.hasFailures() ) + if ( this.hasFailures() && ( count_new > 0 ) ) { this.emit( 'failure', this._failures ); } @@ -136,10 +135,12 @@ module.exports = Class( 'ValidStateMonitor' ) * @param {Object} past past failures to merge with * @param {Object} failures new failures * - * @return {undefined} + * @return {number} number of new failures */ 'virtual protected mergeFailures': function( past, failures ) { + var count_new = 0; + for ( var name in failures ) { past[ name ] = past[ name ] || []; @@ -148,8 +149,11 @@ module.exports = Class( 'ValidStateMonitor' ) for ( var i in failures[ name ] ) { past[ name ][ i ] = failures[ name ][ i ]; + count_new++; } } + + return count_new; }, diff --git a/test/validate/ValidStateMonitorTest.js b/test/validate/ValidStateMonitorTest.js index ea743d0..54fcad7 100644 --- a/test/validate/ValidStateMonitorTest.js +++ b/test/validate/ValidStateMonitorTest.js @@ -151,6 +151,22 @@ describe( 'ValidStateMonitor', function() } ) .update( data, {} ); } ); + + + it( 'does not trigger failure event for existing', function() + { + var called = 0; + + Sut() + .on( 'failure', function() + { + called++; + } ) + .update( {}, { foo: [ 'bar' ] } ) + .update( {}, {} ); // do not trigger failure event + + expect( called ).to.equal( 1 ); + } ); } );