From e6ede4ad3aa58baea85fa69938fd83e73963728a Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Thu, 28 Apr 2016 11:53:14 -0400 Subject: [PATCH] ValidStateMonitor: merge subsequent failures before fixes * src/validate/ValidStateMonitor.js (mergeFailures): Another error on a field that previously failed will no longer overwrite the previous failure, which caused issue when the causes changed (leaving fields potentially unfixed). * test/validate/ValidStateMonitorTest.js: Respective tests added. --- src/validate/ValidStateMonitor.js | 10 +++++- test/validate/ValidStateMonitorTest.js | 44 ++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/src/validate/ValidStateMonitor.js b/src/validate/ValidStateMonitor.js index c3ddc20..2eae4ac 100644 --- a/src/validate/ValidStateMonitor.js +++ b/src/validate/ValidStateMonitor.js @@ -146,10 +146,18 @@ module.exports = Class( 'ValidStateMonitor' ) { past[ name ] = past[ name ] || []; + var cur_past = past[ name ]; + // copy each failure into the past failures table for ( var i in failures[ name ] ) { - past[ name ][ i ] = failures[ name ][ i ]; + var new_failure = failures[ name ][ i ]; + + // merge with past failure if present + cur_past[ i ] = ( cur_past[ i ] !== undefined ) + ? cur_past[ i ].merge( new_failure ) + : new_failure; + count_new++; } } diff --git a/test/validate/ValidStateMonitorTest.js b/test/validate/ValidStateMonitorTest.js index 52f3705..fa3db01 100644 --- a/test/validate/ValidStateMonitorTest.js +++ b/test/validate/ValidStateMonitorTest.js @@ -147,6 +147,50 @@ describe( 'ValidStateMonitor', function() .on( 'fix', nocall( 'fix' ) ) .update( {}, { foo: fail } ); } ); + + + it( 'does not discard existing failures', function( done ) + { + var sut = Sut(); + + // has both failures + var fail1 = Failure( + Field( 'foo', 0 ), + '', + [ Field( 'cause1', 0 ), Field( 'cause2', 0 ) ] + ); + + // has only one of the two failures + var fail2 = Failure( + Field( 'foo', 1 ), + '', + [ Field( 'cause2', 1 ) ] + ); + + // the second failure has fewer causes than the first; + // we need to make sure that it doesn't overwrite, + // leading to fewer caues + sut + .update( {}, { foo: [ fail1 ] } ) + .update( {}, { foo: [ fail2 ] } ); + + // if cause1 wasn't removed, then this will fix it + sut + .once( 'fix', function( fixed ) + { + expect( fixed ) + .to.deep.equal( { foo: [ 'causefix1' ] } ); + + // and then we should have no failures + expect( sut.hasFailures() ).to.be.false; + + done(); + } ) + .update( + { foo: [ 'moo' ], cause1: [ 'causefix1' ] }, + {} + ); + } ); } );