Mike Gerwitz

Activist for User Freedom

aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMark Goldsmith <mark.goldsmith@rtspecialty.com>2020-02-14 13:46:20 -0500
committerGoldsmith, Mark <mark.goldsmith@ryansg.com>2020-02-19 08:51:14 -0500
commite9ded067b1f60311db97d405298a2663304a3fc0 (patch)
treef418bda9e2e856bba1b99386c00baef641ce0619
parentb624fa55ffbe1a03cb88968dafcd484c24433c0b (diff)
downloadliza-e9ded067b1f60311db97d405298a2663304a3fc0.tar.gz
liza-e9ded067b1f60311db97d405298a2663304a3fc0.tar.bz2
liza-e9ded067b1f60311db97d405298a2663304a3fc0.zip
[DEV-7060] Correct checkbox and noyes element ID retrieval
-rw-r--r--src/ui/ElementStyler.js30
-rw-r--r--test/ui/ElementStylerTest.js150
2 files changed, 171 insertions, 9 deletions
diff --git a/src/ui/ElementStyler.js b/src/ui/ElementStyler.js
index fd8515d..4ee28cc 100644
--- a/src/ui/ElementStyler.js
+++ b/src/ui/ElementStyler.js
@@ -833,7 +833,7 @@ module.exports = Class( 'ElementStyler',
if ( filter )
{
- throw new Error( "Filter deprecated" );
+ throw new Error( 'Filter deprecated' );
}
return this._jquery( results );
@@ -855,7 +855,7 @@ module.exports = Class( 'ElementStyler',
if ( hasindex )
{
var id = this._getElementId( name, index );
-
+
if ( id )
{
element = document.getElementById( id );
@@ -1045,11 +1045,25 @@ module.exports = Class( 'ElementStyler',
},
+ /**
+ * Determines the id of an element based on the type
+ *
+ * @param {string} name element name
+ * @param {number} index index of element to retrieve (bucket index)
+ *
+ * @return {string} element id
+ */
'private _getElementId': function( name, index )
{
switch ( this._getElementType( name ) )
{
- case 'radio': return '';
+ case 'radio':
+ return '';
+ case 'answer':
+ return name;
+ case 'checkbox':
+ name += '_n';
+ break;
case 'noyes':
// append yes/no depending on whether or not the given index is
// even/odd
@@ -1057,13 +1071,11 @@ module.exports = Class( 'ElementStyler',
? '_y'
: '_n';
- index = index / 2;
-
- /* fallthrough */
-
- default:
- return 'q_' + name + '_' + index;
+ index = Math.floor( index / 2 );
+ break;
}
+
+ return 'q_' + name + '_' + index;
},
diff --git a/test/ui/ElementStylerTest.js b/test/ui/ElementStylerTest.js
new file mode 100644
index 0000000..e7d16af
--- /dev/null
+++ b/test/ui/ElementStylerTest.js
@@ -0,0 +1,150 @@
+/**
+ * Test case for ElementStyler
+ *
+ * Copyright (C) 2010-2020 R-T Specialty, LLC.
+ *
+ * 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 Sut = require( '../../' ).ui.ElementStyler,
+ expect = require( 'chai' ).expect,
+ sinon = require( 'sinon' ),
+ Class = require( 'easejs' ).Class;
+
+
+describe( 'ui.ElementStyler', () =>
+{
+ [
+ {
+ name: 'business_city',
+ css_class: '',
+ index: '0',
+ qtypes: {
+ business_city: {
+ type: 'text',
+ }
+ },
+ expected_id: 'q_business_city_0',
+ },
+
+ {
+ name: 'business_city',
+ css_class: '',
+ index: '1',
+ qtypes: {
+ business_city: {
+ type: 'text',
+ }
+ },
+ expected_id: 'q_business_city_1',
+ },
+
+ {
+ name: 'sgo_foo',
+ css_class: 'checkbox',
+ index: '0',
+ qtypes: {
+ sgo_foo: {
+ type: 'checkbox',
+ }
+ },
+ expected_id: 'q_sgo_foo_n_0',
+ },
+
+ {
+ name: 'noyes_foo',
+ css_class: 'noyes',
+ index: '0',
+ qtypes: {
+ noyes_foo: {
+ type: 'noyes',
+ }
+ },
+ expected_id: 'q_noyes_foo_n_0',
+ },
+
+ {
+ name: 'noyes_foo',
+ css_class: 'noyes',
+ index: '1',
+ qtypes: {
+ noyes_foo: {
+ type: 'noyes',
+ }
+ },
+ expected_id: 'q_noyes_foo_y_0',
+ },
+
+ {
+ name: 'noyes_baz',
+ css_class: 'noyes',
+ index: '4',
+ qtypes: {
+ noyes_baz: {
+ type: 'noyes',
+ }
+ },
+ expected_id: 'q_noyes_baz_n_2',
+ },
+
+ {
+ name: 'noyes_baz',
+ css_class: 'noyes',
+ index: '5',
+ qtypes: {
+ noyes_baz: {
+ type: 'noyes',
+ }
+ },
+ expected_id: 'q_noyes_baz_y_2',
+ },
+ ].forEach( ( { name, css_class, qtypes, index, expected_id } ) =>
+ {
+ it( "determines the correct element id for " + name, () =>
+ {
+ // Stub all objects
+ $ = sinon.stub();
+ jQuery = sinon.stub();
+ document = sinon.stub();
+
+ const sut = Sut( jQuery );
+
+ sut.setTypeData( qtypes );
+
+ const node = '<input id="' + expected_id
+ + '" class="' + css_class + '" name="' + name
+ + '[]" data-field-name="' + name
+ + '" data-index="' + index
+ + '"></input>';
+
+ const html = '<div class="foo">' + node + '</div>';
+
+ const context = {
+ html: html,
+ };
+
+ document.getElementById = sinon.stub();
+ document.getElementById.returns( node );
+
+ sut.getWidgetByName( name, index, null, context );
+
+ const actual_id = document.getElementById.getCall(0).args[0];
+
+ expect( actual_id )
+ .to.equal( expected_id );
+ } );
+ } );
+} );