1
0
Fork 0

Consider field failure cause when checking fixes

This maintains BC with the old string-based system.

* src/validate/ValidStateMonitor.js (_getCause): Added.
(detectFixes): Consider failure cause if available when checking for fixes.
master
Mike Gerwitz 2016-04-20 12:11:17 -04:00
parent f784db5f2b
commit f624710451
2 changed files with 106 additions and 12 deletions

View File

@ -20,7 +20,8 @@
*/
var Class = require( 'easejs' ).Class,
EventEmitter = require( 'events' ).EventEmitter;
EventEmitter = require( 'events' ).EventEmitter,
Failure = require( './Failure' );
/**
@ -182,15 +183,7 @@ module.exports = Class( 'ValidStateMonitor' )
for ( var name in past )
{
// we're only interested in detecting fixes on the data that has
// been set
if ( !( data[ name ] ) )
{
continue;
}
var field = data[ name ],
past_fail = past[ name ],
var past_fail = past[ name ],
fail = failures[ name ];
// we must check each individual index because it is possible that
@ -198,17 +191,29 @@ module.exports = Class( 'ValidStateMonitor' )
// this because this is treated as a hash table, not an array)
for ( var i in past_fail )
{
var cause = this._getCause( name, i, past_fail ),
cause_name = cause[ 0 ],
cause_index = cause[ 1 ],
field = data[ cause_name ];
// if datum is unchanged, ignore it
if ( field === undefined )
{
continue;
}
// to be marked as fixed, there must both me no failure and
// there must be data for this index for the field in question
// (if the field wasn't touched, then of course there's no
// failure!)
if ( ( fail === undefined )
|| ( !( fail[ i ] ) && ( field[ i ] !== undefined ) )
|| ( !( fail[ cause_index ] )
&& ( field[ cause_index ] !== undefined ) )
)
{
// looks like it has been resolved
( fixed[ name ] = fixed[ name ] || [] )[ i ] =
data[ name ][ i ];
field[ cause_index ]
has_fixed = true;
@ -220,5 +225,34 @@ module.exports = Class( 'ValidStateMonitor' )
return ( has_fixed )
? fixed
: null;
},
/**
* Produces name and index of the field causing a failure, or NAME
* and INDEX if unavailable
*
* This maintains backwards-compatibility for the old string-based
* system.
*
* @param {string} name field name
* @param {number} index field index
*
* @param {Array.<Failure|string>} past_fail previous failure
*
* @return {Array} name/index tuple of cause field
*/
'private _getCause': function( name, index, past_fail )
{
var failure = past_fail[ index ];
if ( Class.isA( Failure, failure ) )
{
var cause = failure.getCause();
return [ cause.getName(), cause.getIndex() ];
}
return [ name, index ];
}
} );

View File

@ -204,6 +204,66 @@ describe( 'ValidStateMonitor', function()
expect( called ).to.equal( 1 );
} );
describe( 'given a cause', function()
{
it( 'considers when recognizing fix', function( done )
{
// same index
var data = { cause: [ 'bar' ] },
field = Field( 'foo', 0 ),
cause = Field( 'cause', 0 ),
fail = Failure( field, 'reason', cause );
Sut()
.on( 'fix', function( fixed )
{
expect( fixed )
.to.deep.equal( { foo: [ 'bar' ] } );
done();
} )
.update( data, { foo: [ fail ] } )
.update( data, {} );
} );
it( 'considers different cause index', function( done )
{
// different index
var data = { cause: [ undefined, 'bar' ] },
field = Field( 'foo', 0 ),
cause = Field( 'cause', 1 ),
fail = Failure( field, 'reason', cause );
Sut()
.on( 'fix', function( fixed )
{
expect( fixed )
.to.deep.equal( { foo: [ 'bar' ] } );
done();
} )
.update( data, { foo: [ fail ] } )
.update( data, {} );
} );
it( 'recognizes non-fix', function()
{
// no cause data
var data = { noncause: [ undefined, 'bar' ] },
field = Field( 'foo', 0 ),
cause = Field( 'cause', 1 ),
fail = Failure( field, 'reason', cause );
Sut()
.on( 'fix', nocall )
.update( data, { foo: [ fail ] } )
.update( data, {} );
} );
} );
} );