From 88e32f3c0dd71dfa6d183749be7dabc8a6e5f199 Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Tue, 6 Feb 2018 13:22:36 -0500 Subject: [PATCH 1/3] Client: Use last available index for `set' action This matches the behavior of assertions. This needs to be refactored into an even handler. * src/client/Client.js (handleEvent)[set]: Use last available index. --- src/client/Client.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/client/Client.js b/src/client/Client.js index 57ae2cf..924bcff 100644 --- a/src/client/Client.js +++ b/src/client/Client.js @@ -2618,12 +2618,17 @@ module.exports = Class( 'Client' ) { case 'set': var setdata = {}; + var maxi = data.value.length - 1; + setdata[ data.elementName ] = []; + // use last available index (just as assertions do) for ( var i in data.indexes ) { - var index = data.indexes[ i ]; - setdata[ data.elementName ][ index ] = data.value[ index ]; + var desti = data.indexes[ i ]; + var srci = Math.min( desti, maxi ); + + setdata[ data.elementName ][ desti ] = data.value[ srci ]; } this._quote.setData( setdata ); From f5346bef39dbe3717ac7dc7e33c238f87208c67f Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Wed, 7 Feb 2018 14:57:51 -0500 Subject: [PATCH 2/3] ElementStyler: Ignore noyes/radio/legacyradio when DOM element unknown * src/ui/ElementStyler.js (setValueByName): Ignore `question' if undefined. --- src/ui/ElementStyler.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/ui/ElementStyler.js b/src/ui/ElementStyler.js index 85c00af..82fa9c8 100644 --- a/src/ui/ElementStyler.js +++ b/src/ui/ElementStyler.js @@ -493,7 +493,11 @@ module.exports = Class( 'ElementStyler', while ( i-- ) { const question = elements[ i ][ 0 ]; - question.checked = ( question.value === ''+value ); + + if ( question ) + { + question.checked = ( question.value === ''+value ); + } } break; From 943231ee8128c6678d89e256f571fe51b18c0fd0 Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Wed, 7 Feb 2018 14:59:50 -0500 Subject: [PATCH 3/3] ValueSetEventHandler: New handler for `set' event The `set' event already existed---this merely extracts it into its own handler. * src/client/Client.js (handleEvent): Extract `set' handler. * src/client/ClientDependencyFactory.js (createClientEventHandler): Add `set'. * src/client/event/ValueSetEventHandler.js: New class. * test/event/ValueSetEventHandlerTest.js: Associated test case. --- src/client/Client.js | 20 +---- src/client/ClientDependencyFactory.js | 2 + src/client/event/ValueSetEventHandler.js | 86 ++++++++++++++++++++ test/event/ValueSetEventHandlerTest.js | 99 ++++++++++++++++++++++++ 4 files changed, 188 insertions(+), 19 deletions(-) create mode 100644 src/client/event/ValueSetEventHandler.js create mode 100644 test/event/ValueSetEventHandlerTest.js diff --git a/src/client/Client.js b/src/client/Client.js index 924bcff..a047945 100644 --- a/src/client/Client.js +++ b/src/client/Client.js @@ -1,7 +1,7 @@ /** * Liza client * - * Copyright (C) 2017 R-T Specialty, LLC. + * Copyright (C) 2017, 2018 R-T Specialty, LLC. * * This file is part of the Liza Data Collection Framework * @@ -2616,24 +2616,6 @@ module.exports = Class( 'Client' ) // perform event (XXX: replace me; see above) switch ( event_name ) { - case 'set': - var setdata = {}; - var maxi = data.value.length - 1; - - setdata[ data.elementName ] = []; - - // use last available index (just as assertions do) - for ( var i in data.indexes ) - { - var desti = data.indexes[ i ]; - var srci = Math.min( desti, maxi ); - - setdata[ data.elementName ][ desti ] = data.value[ srci ]; - } - - this._quote.setData( setdata ); - break; - case 'reset': var reset = {}; reset[ data.elementName ] = data.indexes; diff --git a/src/client/ClientDependencyFactory.js b/src/client/ClientDependencyFactory.js index 759f312..03b2682 100644 --- a/src/client/ClientDependencyFactory.js +++ b/src/client/ClientDependencyFactory.js @@ -369,6 +369,8 @@ module.exports = Class( 'ClientDependencyFactory', 'show': field_vis_handler, 'hide': field_vis_handler, + 'set': requireh( 'ValueSetEventHandler' )( client ), + 'action$cvv2Dialog': requireh( 'Cvv2DialogEventHandler' )( jquery ) } ); } diff --git a/src/client/event/ValueSetEventHandler.js b/src/client/event/ValueSetEventHandler.js new file mode 100644 index 0000000..77ad029 --- /dev/null +++ b/src/client/event/ValueSetEventHandler.js @@ -0,0 +1,86 @@ +/** + * Value set event handler + * + * Copyright (C) 2018 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 . + */ + +"use strict"; + +const Class = require( 'easejs' ).Class; +const EventHandler = require( './EventHandler' ); + + +/** + * Set field values + */ +module.exports = Class( 'ValueSetEventHandler' ) + .implement( EventHandler ) + .extend( +{ + /** + * Client + * @type {Client} + */ + 'private _client': null, + + + /** + * Initialize with client + * + * Ideally we don't want the client (Law of Demeter), but at the point + * that this class is instantiated, a quote may not yet be + * initialized. Refactoring is needed. + * + * @param {Client} client + */ + constructor( client ) + { + this._client = client; + }, + + + /** + * Set value of specified fields + * + * If the destination index exceeds the number of indexes of the source + * field, the last available source index will be used. This matches + * the behavior of assertions. + * + * @param {string} event_id event id + * @param {function(*,Object)} callback continuation to invoke on completion + * @param {Object} data event data + * + * @return {EventHandler} self + */ + 'public handle'( + event_id, callback, { elementName: field_name, indexes, value } + ) + { + const quote = this._client.getQuote(); + const maxi = value.length - 1; + + const set_data = indexes.reduce( ( result, desti ) => + { + result[ desti ] = value[ Math.min( desti, maxi ) ]; + return result; + }, [] ); + + quote.setData( { [field_name]: set_data } ); + callback(); + }, +} ); diff --git a/test/event/ValueSetEventHandlerTest.js b/test/event/ValueSetEventHandlerTest.js new file mode 100644 index 0000000..50523b7 --- /dev/null +++ b/test/event/ValueSetEventHandlerTest.js @@ -0,0 +1,99 @@ +/** + * Test case for ValueSetEventHandler + * + * Copyright (C) 2018 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 . + */ + +"use strict"; + +const event = require( '../../' ).client.event; +const expect = require( 'chai' ).expect; +const Class = require( 'easejs' ).Class; + +const { ValueSetEventHandler: Sut } = event; + + +describe( 'ValueSetEventHandler', () => +{ + it( "sets relative indexes up until last source index", done => + { + const expected = { + foo: [ "zero", "one", "one" ], + }; + + let qcalled = false; + const quote = { + setData( data ) + { + expect( data ).to.deep.equal( expected ); + qcalled = true; + } + }; + + const callback = () => + { + expect( qcalled ).to.be.true; + done(); + }; + + Sut( { getQuote: () => quote } ) + .handle( + '', + callback, + { + elementName: 'foo', + indexes: [ 0, 1, 2 ], + value: [ "zero", "one" ], + } + ); + } ); + + + it( "sets only given indexes", done => + { + const expected = { + bar: [ , "set1", "set2" ], + }; + + let qcalled = false; + const quote = { + setData( data ) + { + expect( data ).to.deep.equal( expected ); + qcalled = true; + } + }; + + const callback = () => + { + expect( qcalled ).to.be.true; + done(); + }; + + Sut( { getQuote: () => quote } ) + .handle( + '', + callback, + { + elementName: 'bar', + indexes: [ 1, 2 ], + value: [ "ignore", "set1", "set2", "extra" ], + } + ); + } ); +} );