2016-04-01 22:13:21 -04:00
|
|
|
/**
|
|
|
|
* Test case for NaFieldStyler
|
|
|
|
*
|
|
|
|
* Copyright (C) 2016 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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
var styler = require( '../../../' ).ui.styler,
|
|
|
|
expect = require( 'chai' ).expect,
|
|
|
|
Class = require( 'easejs' ).Class,
|
|
|
|
Sut = styler.NaFieldStyler;
|
|
|
|
|
|
|
|
|
|
|
|
describe( 'ui.styler.NaFieldStyler', function()
|
|
|
|
{
|
2016-04-04 23:59:57 -04:00
|
|
|
function testApplyHidden()
|
2016-04-01 22:13:21 -04:00
|
|
|
{
|
2016-04-05 11:53:29 -04:00
|
|
|
var element = stubEle( { className: '' } ),
|
|
|
|
r1 = stubEle( { className: '' } ),
|
|
|
|
r2 = stubEle( { className: '' } ),
|
2016-04-04 23:59:57 -04:00
|
|
|
row = [ r1, r2 ];
|
|
|
|
|
|
|
|
Sut().applyStyle( getStubField( element ), element, row );
|
|
|
|
|
|
|
|
[ element, r1, r2 ].forEach( function( ele )
|
2016-04-01 22:13:21 -04:00
|
|
|
{
|
2016-04-04 23:59:57 -04:00
|
|
|
expect( ele.className ).to.match( /\bhidden\b/ );
|
2016-04-01 22:13:21 -04:00
|
|
|
} );
|
2016-04-04 23:59:57 -04:00
|
|
|
}
|
2016-04-01 22:13:21 -04:00
|
|
|
|
|
|
|
|
2016-04-04 23:59:57 -04:00
|
|
|
function testApplyClear()
|
2016-04-01 22:13:21 -04:00
|
|
|
{
|
2016-04-05 11:53:29 -04:00
|
|
|
var element = stubEle( { style: 'foo' } ),
|
|
|
|
r1 = stubEle( { style: 'foo' } ),
|
|
|
|
r2 = stubEle( { style: 'foo' } ),
|
2016-04-04 23:59:57 -04:00
|
|
|
row = [ r1, r2 ];
|
|
|
|
|
|
|
|
Sut().applyStyle( getStubField( element ), element, row );
|
|
|
|
|
|
|
|
[ element, r1, r2 ].forEach( function( ele )
|
2016-04-01 22:13:21 -04:00
|
|
|
{
|
2016-04-04 23:59:57 -04:00
|
|
|
expect( ele.style ).to.equal( '' );
|
|
|
|
} );
|
|
|
|
}
|
2016-04-01 22:13:21 -04:00
|
|
|
|
|
|
|
|
2016-04-04 23:59:57 -04:00
|
|
|
function testRevokeHidden()
|
|
|
|
{
|
2016-04-05 11:53:29 -04:00
|
|
|
var element = stubEle( { className: 'foo hidden' } ),
|
|
|
|
r1 = stubEle( { className: 'foo hidden' } ),
|
|
|
|
r2 = stubEle( { className: 'foo hidden' } ),
|
2016-04-04 23:59:57 -04:00
|
|
|
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/ );
|
2016-04-01 22:13:21 -04:00
|
|
|
} );
|
2016-04-04 23:59:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function testRevokeStyle()
|
|
|
|
{
|
2016-04-05 11:53:29 -04:00
|
|
|
var element = stubEle( { style: 'foo' } ),
|
|
|
|
r1 = stubEle( { style: 'foo' } ),
|
|
|
|
r2 = stubEle( { style: 'foo' } ),
|
2016-04-04 23:59:57 -04:00
|
|
|
row = [ r1, r2 ];
|
2016-04-01 22:13:21 -04:00
|
|
|
|
2016-04-04 23:59:57 -04:00
|
|
|
Sut().revokeStyle( getStubField( element ), element, row );
|
2016-04-01 22:13:21 -04:00
|
|
|
|
2016-04-04 23:59:57 -04:00
|
|
|
[ element, r1, r2 ].forEach( function( ele )
|
2016-04-01 22:13:21 -04:00
|
|
|
{
|
2016-04-04 23:59:57 -04:00
|
|
|
expect( ele.style ).to.equal( 'foo' );
|
|
|
|
} );
|
|
|
|
}
|
2016-04-01 22:13:21 -04:00
|
|
|
|
|
|
|
|
2016-04-04 23:59:57 -04:00
|
|
|
describe( '#getId', function()
|
|
|
|
{
|
|
|
|
it( 'returns unique identifier', function()
|
|
|
|
{
|
|
|
|
expect( Sut().getId() ).to.equal( 'na' );
|
2016-04-01 22:13:21 -04:00
|
|
|
} );
|
2016-04-04 23:59:57 -04:00
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
|
|
describe( '#applyStyle', function()
|
|
|
|
{
|
|
|
|
it( 'sets hidden class on all elements', testApplyHidden );
|
|
|
|
it( 'clears style on all elements', testApplyClear );
|
2016-04-04 11:22:22 -04:00
|
|
|
|
|
|
|
|
|
|
|
it( 'does not set class on subfield parents', function()
|
|
|
|
{
|
2016-04-05 11:53:29 -04:00
|
|
|
var element = stubEle( {
|
2016-04-04 11:22:22 -04:00
|
|
|
className: '',
|
2016-04-04 12:18:58 -04:00
|
|
|
parentElement: {
|
|
|
|
className: 'widget',
|
|
|
|
removeChild: function() {},
|
|
|
|
}
|
2016-04-05 11:53:29 -04:00
|
|
|
} );
|
2016-04-04 11:22:22 -04:00
|
|
|
|
2016-04-05 11:53:29 -04:00
|
|
|
var r1 = stubEle( { className: '' } ),
|
|
|
|
r2 = stubEle( { className: '' } ),
|
2016-04-04 11:22:22 -04:00
|
|
|
row = [ r1, r2 ];
|
|
|
|
|
2016-04-04 12:18:58 -04:00
|
|
|
Sut().applyStyle( getStubField( element ), element, row );
|
2016-04-04 11:22:22 -04:00
|
|
|
|
|
|
|
expect( element.className ).to.match( /\bhidden\b/ );
|
|
|
|
|
|
|
|
[ r1, r2 ].forEach( function( ele )
|
|
|
|
{
|
|
|
|
expect( ele.className ).to.equal( '' );
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
|
|
it( 'does not clears style subfield parents', function()
|
|
|
|
{
|
2016-04-05 11:53:29 -04:00
|
|
|
var element = stubEle( {
|
2016-04-04 11:22:22 -04:00
|
|
|
style: 'foo',
|
2016-04-04 12:18:58 -04:00
|
|
|
parentElement: {
|
|
|
|
className: 'widget',
|
|
|
|
removeChild: function() {},
|
|
|
|
}
|
2016-04-05 11:53:29 -04:00
|
|
|
} );
|
2016-04-04 11:22:22 -04:00
|
|
|
|
2016-04-05 11:53:29 -04:00
|
|
|
var r1 = stubEle( { style: 'foo' } ),
|
|
|
|
r2 = stubEle( { style: 'foo' } ),
|
2016-04-04 11:22:22 -04:00
|
|
|
row = [ r1, r2 ];
|
|
|
|
|
2016-04-04 12:18:58 -04:00
|
|
|
Sut().applyStyle( getStubField( element ), element, row );
|
2016-04-04 11:22:22 -04:00
|
|
|
|
|
|
|
expect( element.style ).to.equal( '' );
|
|
|
|
|
|
|
|
[ r1, r2 ].forEach( function( ele )
|
|
|
|
{
|
|
|
|
expect( ele.style ).to.equal( 'foo' );
|
|
|
|
} );
|
|
|
|
} );
|
2016-04-04 12:18:58 -04:00
|
|
|
|
|
|
|
|
|
|
|
// f@#(& IE
|
|
|
|
it( 'removes subfield from DOM', function( done )
|
|
|
|
{
|
2016-04-05 11:53:29 -04:00
|
|
|
var element = stubEle( {
|
2016-04-04 12:18:58 -04:00
|
|
|
style: '',
|
|
|
|
parentElement: {
|
|
|
|
className: 'widget',
|
|
|
|
removeChild: function( ele )
|
|
|
|
{
|
|
|
|
expect( ele ).to.equal( element );
|
|
|
|
done();
|
|
|
|
},
|
|
|
|
}
|
2016-04-05 11:53:29 -04:00
|
|
|
} );
|
2016-04-04 12:18:58 -04:00
|
|
|
|
|
|
|
Sut().applyStyle( getStubField( element ), element, [] );
|
|
|
|
} );
|
2016-04-01 22:13:21 -04:00
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
|
|
describe( '#revokeStyle', function()
|
|
|
|
{
|
2016-04-04 23:59:57 -04:00
|
|
|
it( 'removes hidden class on all elements', testRevokeHidden );
|
|
|
|
it( 'does not clear style on all elements', testRevokeStyle );
|
2016-04-04 11:22:22 -04:00
|
|
|
|
|
|
|
|
|
|
|
it( 'does not remove hidden class on subfield parents', function()
|
|
|
|
{
|
|
|
|
var element = {
|
|
|
|
className: 'foo hidden',
|
2016-04-04 12:18:58 -04:00
|
|
|
parentElement: {
|
|
|
|
className: 'widget',
|
|
|
|
appendChild: function() {},
|
|
|
|
}
|
2016-04-04 11:22:22 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
var r1 = { className: 'foo hidden' },
|
|
|
|
r2 = { className: 'foo hidden' },
|
|
|
|
row = [ r1, r2 ];
|
|
|
|
|
2016-04-04 12:18:58 -04:00
|
|
|
Sut().revokeStyle( getStubField( element ), element, row );
|
2016-04-04 11:22:22 -04:00
|
|
|
|
|
|
|
expect( element.className ).to.not.match( /\bhidden\b/ );
|
|
|
|
expect( element.className ).to.match( /foo/ );
|
|
|
|
|
|
|
|
[ r1, r2 ].forEach( function( ele )
|
|
|
|
{
|
|
|
|
expect( ele.className ).to.equal( 'foo hidden' );
|
|
|
|
} );
|
|
|
|
} );
|
2016-04-04 12:18:58 -04:00
|
|
|
|
|
|
|
|
|
|
|
// we eventually need to care about where it's re-attached
|
|
|
|
it( 're-attaches subfield to DOM', function( done )
|
|
|
|
{
|
|
|
|
var element = {
|
|
|
|
className: '',
|
|
|
|
parentElement: {
|
|
|
|
className: 'widget',
|
|
|
|
appendChild: function( ele )
|
|
|
|
{
|
|
|
|
expect( ele ).to.equal( element );
|
|
|
|
done();
|
|
|
|
},
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Sut().revokeStyle( getStubField( element ), element, [] );
|
|
|
|
} );
|
2016-04-04 11:22:22 -04:00
|
|
|
} );
|
|
|
|
|
|
|
|
|
2016-04-04 15:25:52 -04:00
|
|
|
describe( '#isApplied', function()
|
|
|
|
{
|
|
|
|
it( 'recognizes when applied', function()
|
|
|
|
{
|
2016-04-05 11:53:29 -04:00
|
|
|
var element = stubEle( {
|
2016-04-04 15:25:52 -04:00
|
|
|
className: '',
|
2016-04-05 11:53:29 -04:00
|
|
|
} );
|
2016-04-04 15:25:52 -04:00
|
|
|
|
|
|
|
var sut = Sut(),
|
|
|
|
field = getStubField( element );
|
|
|
|
|
|
|
|
sut.applyStyle( field, element, [] );
|
|
|
|
|
|
|
|
expect( sut.isApplied( field, element ) )
|
|
|
|
.to.be.true;
|
|
|
|
|
|
|
|
sut.revokeStyle( field, element, [] );
|
|
|
|
|
|
|
|
expect( sut.isApplied( field, element ) )
|
|
|
|
.to.be.false;
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
2016-04-04 11:22:22 -04:00
|
|
|
describe( 'protected API', function()
|
|
|
|
{
|
|
|
|
describe( '#isSubField', function()
|
|
|
|
{
|
|
|
|
it( 'recognizes parent widget class as subfield', function()
|
|
|
|
{
|
|
|
|
var element = {
|
|
|
|
className: '',
|
2016-04-04 12:18:58 -04:00
|
|
|
parentElement: {
|
|
|
|
className: 'widget',
|
|
|
|
removeChild: function() {},
|
|
|
|
}
|
2016-04-04 11:22:22 -04:00
|
|
|
};
|
|
|
|
|
2016-04-04 12:18:58 -04:00
|
|
|
expect( protSut().protIsSubField( getStubField( element ) ) )
|
2016-04-04 11:22:22 -04:00
|
|
|
.to.be.true;
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
|
|
it( 'missing parent widget class is non-subfield', function()
|
|
|
|
{
|
|
|
|
var element = {
|
|
|
|
className: '',
|
|
|
|
};
|
|
|
|
|
2016-04-04 12:18:58 -04:00
|
|
|
expect( protSut().protIsSubField( getStubField( element ) ) )
|
2016-04-04 11:22:22 -04:00
|
|
|
.to.be.false;
|
|
|
|
} );
|
|
|
|
} );
|
2016-04-04 23:59:57 -04:00
|
|
|
|
|
|
|
|
|
|
|
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 );
|
|
|
|
} );
|
2016-04-01 22:13:21 -04:00
|
|
|
} );
|
|
|
|
} );
|
2016-04-04 11:22:22 -04:00
|
|
|
|
|
|
|
|
2016-04-04 12:18:58 -04:00
|
|
|
function getStubField( element )
|
|
|
|
{
|
|
|
|
return {
|
|
|
|
getParent: function()
|
|
|
|
{
|
|
|
|
return element.parentElement;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-04-04 11:22:22 -04:00
|
|
|
function protSut()
|
|
|
|
{
|
|
|
|
return Class.extend( Sut, {
|
|
|
|
protIsSubField: function( element )
|
|
|
|
{
|
|
|
|
return this.isSubField( element );
|
|
|
|
}
|
|
|
|
} )();
|
|
|
|
}
|
2016-04-05 11:53:29 -04:00
|
|
|
|
|
|
|
|
|
|
|
function stubEle( obj )
|
|
|
|
{
|
|
|
|
obj.setAttribute = function( name, value )
|
|
|
|
{
|
|
|
|
obj[ name ] = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|