Mike Gerwitz

Activist for User Freedom

aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMike Gerwitz <gerwitzm@lovullo.com>2017-03-17 14:54:22 -0400
committerMike Gerwitz <gerwitzm@lovullo.com>2017-03-17 14:54:22 -0400
commit16ab8b87418c664b4f84bc7a609725da20bf2314 (patch)
tree5e437939d69ac261ccbafbc9a5da4fd3b900f52f
parent2138ddf81a6a76a17d7594b8e20da83f2f35b50e (diff)
parent7567306123c4bc8448b4003f3fd77f3caaf42570 (diff)
downloadliza-16ab8b87418c664b4f84bc7a609725da20bf2314.tar.gz
liza-16ab8b87418c664b4f84bc7a609725da20bf2314.tar.bz2
liza-16ab8b87418c664b4f84bc7a609725da20bf2314.zip
Handle fixes triggered by scalar diffs
-rw-r--r--src/ui/styler/FieldStyler.js9
-rw-r--r--src/validate/ValidStateMonitor.js13
-rw-r--r--test/ui/styler/FieldStylerTest.js98
-rw-r--r--test/validate/ValidStateMonitorTest.js43
4 files changed, 157 insertions, 6 deletions
diff --git a/src/ui/styler/FieldStyler.js b/src/ui/styler/FieldStyler.js
index 3d62cd9..07d5ee9 100644
--- a/src/ui/styler/FieldStyler.js
+++ b/src/ui/styler/FieldStyler.js
@@ -1,7 +1,7 @@
/**
* Style fields using CSS
*
- * Copyright (C) 2016 LoVullo Associates, Inc.
+ * Copyright (C) 2016, 2017 LoVullo Associates, Inc.
*
* This file is part of liza.
*
@@ -140,10 +140,11 @@ module.exports = AbstractClass( 'FieldStyler',
}
else if ( typeof element.className === 'string' )
{
- // note that we use a space instead of a boundary for the character
- // preceding the match due to the implementation of addClass()
+ // note that the implementation of #addClass adds a space,
+ // but we also need to cater to classes that might have been
+ // added outside of this system
element.className = element.className.replace(
- new RegExp( ( ' ' + cls + '\\b' ), 'g' ), ''
+ new RegExp( ( '( +|\\b)' + cls + '\\b' ), 'g' ), ''
);
}
diff --git a/src/validate/ValidStateMonitor.js b/src/validate/ValidStateMonitor.js
index a25a79a..7b9d0db 100644
--- a/src/validate/ValidStateMonitor.js
+++ b/src/validate/ValidStateMonitor.js
@@ -277,6 +277,9 @@ module.exports = Class( 'ValidStateMonitor' )
* fix. If so, the promise is fulfilled with the fix data. It is the
* responsibility of the caller to handle removing past failures.
*
+ * If the diff contains a scalar instead of an array diff, it is
+ * considered to affect every index.
+ *
* @param {Object} data validated data
* @param {Object} fail failure records
* @param {Promise} causep cause promise to chain onto
@@ -293,15 +296,21 @@ module.exports = Class( 'ValidStateMonitor' )
new Promise( ( keepgoing, found ) =>
data.get( cause_name ).then( field =>
{
+ // we want everything to be an array, but we need a sane
+ // fallback if we _are_ provided with scalars
+ const index_data = ( Array.isArray( field ) )
+ ? field[ cause_index ]
+ : field;
+
// 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[ cause_index ] ) )
- && ( field[ cause_index ] !== undefined )
+ && ( index_data !== undefined )
)
{
- found( field[ cause_index ] );
+ found( index_data );
return;
}
diff --git a/test/ui/styler/FieldStylerTest.js b/test/ui/styler/FieldStylerTest.js
new file mode 100644
index 0000000..0a4fc09
--- /dev/null
+++ b/test/ui/styler/FieldStylerTest.js
@@ -0,0 +1,98 @@
+/**
+ * Test case for FieldStyler
+ *
+ * Copyright (C) 2017 LoVullo Associates, Inc.
+ *
+ * This file is part of the Liza Data Collection Framework
+ *
+ * Liza is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+const { expect } = require( 'chai' );
+const { Class } = require( 'easejs' );
+const styler = require( '../../../' ).ui.styler;
+
+const Sut = styler.FieldStyler.extend(
+{
+ getId() {},
+ isApplied( field, element ) {},
+ applyStyle( field, element, row ) {},
+ revokeStyle( field, element, row ) {},
+ add( element, cls ) { this.addClass( element, cls ); },
+ remove( element, cls ) { this.removeClass( element, cls ); },
+} );
+
+
+describe( 'ui/styler/FieldStyler', () =>
+{
+ [
+ {
+ label: 'adds class using #addClass on empty',
+ given: '',
+ add: 'foo',
+ expected: ' foo',
+ },
+
+ {
+ label: 'adds class using #addClass with existing',
+ given: 'foo bar',
+ add: 'baz',
+ expected: 'foo bar baz',
+ },
+
+ {
+ label: 'removes class added by #addClass',
+ given: 'cow',
+ add: 'moo',
+ remove: 'moo',
+ expected: 'cow',
+ },
+
+ {
+ label: 'removes class added by #addClass on empty',
+ given: '',
+ add: 'moo',
+ remove: 'moo',
+ expected: '',
+ },
+
+ {
+ label: 'removes classes added externally',
+ given: 'cow moo',
+ remove: 'cow',
+ expected: ' moo',
+ },
+
+ {
+ label: 'removes duplicate classes',
+ given: 'moo cow cow and',
+ remove: 'cow',
+ expected: 'moo and',
+ },
+ ].forEach( ( { label, given, add, remove, expected } ) =>
+ {
+ it( label, () =>
+ {
+ const element = { className: given };
+ const sut = Sut();
+
+ // #addClass and #removeClass respectively
+ add && sut.add( element, add )
+ remove && sut.remove( element, remove );
+
+ expect( element.className )
+ .to.equal( expected );
+ } );
+ } );
+} );
diff --git a/test/validate/ValidStateMonitorTest.js b/test/validate/ValidStateMonitorTest.js
index ca140df..082bcd9 100644
--- a/test/validate/ValidStateMonitorTest.js
+++ b/test/validate/ValidStateMonitorTest.js
@@ -424,6 +424,49 @@ describe( 'ValidStateMonitor', function()
} );
+ // if the field diff is a scalar (normally it should be an
+ // array), it must recognize it as a fix for all indexes
+ it( 'recognizes scalar diff as fix to any index', function()
+ {
+ // scalar
+ const update_data = { cause: 1 };
+
+ return new Promise( ( accept, reject ) =>
+ {
+ return mkstore( update_data ).then( data =>
+ {
+ return Sut()
+ .on( 'fix', function( fixed )
+ {
+ expect( fixed )
+ .to.deep.equal( { foo: [ 1, 1 ] } );
+
+ accept();
+ } )
+ .update( data, {
+ foo: [
+ Failure(
+ Field( 'foo', 0 ),
+ '',
+ [ Field( 'cause', 0 ) ]
+ ),
+ Failure(
+ Field( 'foo', 1 ),
+ '',
+ [ Field( 'cause', 1 ) ]
+ ),
+ ],
+ } )
+ .then( sut =>
+ {
+ return sut.update( data, {} );
+ } );
+ } )
+ .catch( e => reject( e ) );
+ } );
+ } );
+
+
it( 'considers any number of causes', function()
{
// different index