From befca68110a96516f466699a02aa2c6f486ad796 Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Mon, 4 Apr 2016 23:59:57 -0400 Subject: [PATCH] Extract NaFieldStyler show/hide actions into protected methods * src/ui/styler/NaFieldStyler.js (showField, hideField): Added * test/ui/styler/NaFieldStyler.js: Added respective tests --- src/ui/styler/NaFieldStyler.js | 42 +++++--- test/ui/styler/NaFieldStylerTest.js | 144 ++++++++++++++++------------ 2 files changed, 111 insertions(+), 75 deletions(-) diff --git a/src/ui/styler/NaFieldStyler.js b/src/ui/styler/NaFieldStyler.js index 1f03d42..8eba322 100644 --- a/src/ui/styler/NaFieldStyler.js +++ b/src/ui/styler/NaFieldStyler.js @@ -72,15 +72,10 @@ module.exports = Class( 'NaFieldStyler' ) */ 'public applyStyle': function( field, element, row ) { - this.addClass( element, 'hidden' ); - - // this is a workaround from the old days where jQuery would add - // styles to hide elements, which we wanted to override; this can be - // removed once jQuery is eradicated from the framework - element.style = ''; if ( this.isSubField( field ) ) { + this.hideField( element, [] ); field.getParent().removeChild( element ); // this is a child of another field; don't consider it a @@ -89,11 +84,7 @@ module.exports = Class( 'NaFieldStyler' ) return; } - for ( var i in row ) - { - this.addClass( row[ i ], 'hidden' ); - row[ i ].style = ''; - } + this.hideField( element, row ); }, @@ -108,16 +99,15 @@ module.exports = Class( 'NaFieldStyler' ) */ 'public revokeStyle': function( field, element, row ) { - this.removeClass( element, 'hidden' ); - if ( this.isSubField( field ) ) { + this.showField( element, [] ); field.getParent().appendChild( element ); return; } - this.removeClass( row, 'hidden' ); + this.showField( element, row ); }, @@ -140,5 +130,29 @@ module.exports = Class( 'NaFieldStyler' ) // ES3-compatible (don't use classList) return !!( parent && /\bwidget\b/.test( parent.className ) ); + }, + + + 'virtual protected hideField': function( element, row ) + { + this.addClass( element, 'hidden' ); + + // this is a workaround from the old days where jQuery would add + // styles to hide elements, which we wanted to override; this can be + // removed once jQuery is eradicated from the framework + element.style = ''; + + for ( var i in row ) + { + this.addClass( row[ i ], 'hidden' ); + row[ i ].style = ''; + } + }, + + + 'virtual protected showField': function( element, row ) + { + this.removeClass( element, 'hidden' ); + this.removeClass( row, 'hidden' ); } } ); diff --git a/test/ui/styler/NaFieldStylerTest.js b/test/ui/styler/NaFieldStylerTest.js index 26c7431..ae046aa 100644 --- a/test/ui/styler/NaFieldStylerTest.js +++ b/test/ui/styler/NaFieldStylerTest.js @@ -27,6 +27,71 @@ var styler = require( '../../../' ).ui.styler, describe( 'ui.styler.NaFieldStyler', function() { + function testApplyHidden() + { + var element = { className: '' }, + r1 = { className: '' }, + r2 = { className: '' }, + row = [ r1, r2 ]; + + Sut().applyStyle( getStubField( element ), element, row ); + + [ element, r1, r2 ].forEach( function( ele ) + { + expect( ele.className ).to.match( /\bhidden\b/ ); + } ); + } + + + function testApplyClear() + { + var element = { style: 'foo' }, + r1 = { style: 'foo' }, + r2 = { style: 'foo' }, + row = [ r1, r2 ]; + + Sut().applyStyle( getStubField( element ), element, row ); + + [ element, r1, r2 ].forEach( function( ele ) + { + expect( ele.style ).to.equal( '' ); + } ); + } + + + function testRevokeHidden() + { + var element = { className: 'foo hidden' }, + r1 = { className: 'foo hidden' }, + r2 = { className: 'foo hidden' }, + row = [ r1, r2 ]; + + Sut().revokeStyle( getStubField( element ), element, row ); + + [ element, r1, r2 ].forEach( function( ele ) + { + expect( ele.className ).to.not.match( /\bhidden\b/ ); + expect( ele.className ).to.match( /foo/ ); + } ); + } + + + function testRevokeStyle() + { + var element = { style: 'foo' }, + r1 = { style: 'foo' }, + r2 = { style: 'foo' }, + row = [ r1, r2 ]; + + Sut().revokeStyle( getStubField( element ), element, row ); + + [ element, r1, r2 ].forEach( function( ele ) + { + expect( ele.style ).to.equal( 'foo' ); + } ); + } + + describe( '#getId', function() { it( 'returns unique identifier', function() @@ -38,36 +103,8 @@ describe( 'ui.styler.NaFieldStyler', function() describe( '#applyStyle', function() { - it( 'sets hidden class on all elements', function() - { - var element = { className: '' }, - r1 = { className: '' }, - r2 = { className: '' }, - row = [ r1, r2 ]; - - Sut().applyStyle( getStubField( element ), element, row ); - - [ element, r1, r2 ].forEach( function( ele ) - { - expect( ele.className ).to.match( /\bhidden\b/ ); - } ); - } ); - - - it( 'clears style on all elements', function() - { - var element = { style: 'foo' }, - r1 = { style: 'foo' }, - r2 = { style: 'foo' }, - row = [ r1, r2 ]; - - Sut().applyStyle( getStubField( element ), element, row ); - - [ element, r1, r2 ].forEach( function( ele ) - { - expect( ele.style ).to.equal( '' ); - } ); - } ); + it( 'sets hidden class on all elements', testApplyHidden ); + it( 'clears style on all elements', testApplyClear ); it( 'does not set class on subfield parents', function() @@ -142,37 +179,8 @@ describe( 'ui.styler.NaFieldStyler', function() describe( '#revokeStyle', function() { - it( 'removes hidden class on all elements', function() - { - var element = { className: 'foo hidden' }, - r1 = { className: 'foo hidden' }, - r2 = { className: 'foo hidden' }, - row = [ r1, r2 ]; - - Sut().revokeStyle( getStubField( element ), element, row ); - - [ element, r1, r2 ].forEach( function( ele ) - { - expect( ele.className ).to.not.match( /\bhidden\b/ ); - expect( ele.className ).to.match( /foo/ ); - } ); - } ); - - - it( 'does not clear style on all elements', function() - { - var element = { style: 'foo' }, - r1 = { style: 'foo' }, - r2 = { style: 'foo' }, - row = [ r1, r2 ]; - - Sut().revokeStyle( getStubField( element ), element, row ); - - [ element, r1, r2 ].forEach( function( ele ) - { - expect( ele.style ).to.equal( 'foo' ); - } ); - } ); + it( 'removes hidden class on all elements', testRevokeHidden ); + it( 'does not clear style on all elements', testRevokeStyle ); it( 'does not remove hidden class on subfield parents', function() @@ -274,6 +282,20 @@ describe( 'ui.styler.NaFieldStyler', function() .to.be.false; } ); } ); + + + describe( '#hideField', function() + { + it( 'sets hidden class on all elements', testApplyHidden ); + it( 'clears style on all elements', testApplyClear ); + } ); + + + describe( '#showField', function() + { + it( 'removes hidden class on all elements', testRevokeHidden ); + it( 'does not clear style on all elements', testRevokeStyle ); + } ); } ); } );