diff options
author | Mike Gerwitz <gerwitzm@lovullo.com> | 2017-02-07 13:56:52 -0500 |
---|---|---|
committer | Mike Gerwitz <gerwitzm@lovullo.com> | 2017-02-08 11:24:56 -0500 |
commit | e26a7c3cac34ae498800e8835f9312d4e717e51f (patch) | |
tree | 5a469a29c01d50d15aa7b4408c455e6d9f070c7f /test | |
parent | ed217079200766837306ccb8d13abf49fa91da58 (diff) | |
download | liza-e26a7c3cac34ae498800e8835f9312d4e717e51f.tar.gz liza-e26a7c3cac34ae498800e8835f9312d4e717e51f.tar.bz2 liza-e26a7c3cac34ae498800e8835f9312d4e717e51f.zip |
FieldVisibilityEventHandler: Add class
This extracts code from internal lovullo repo rating-fw (originally
Client#_hideFields), 5d4019f1973f9271f4b1bd24ce1f55b56ccda09e.
* src/event/FieldVisibilityEventHandler.js: Add class.
* test/event/FieldVisibilityEventHandlerTest.js: Add test case.
Diffstat (limited to 'test')
-rw-r--r-- | test/event/FieldVisibilityEventHandlerTest.js | 145 |
1 files changed, 145 insertions, 0 deletions
diff --git a/test/event/FieldVisibilityEventHandlerTest.js b/test/event/FieldVisibilityEventHandlerTest.js new file mode 100644 index 0000000..005e0e7 --- /dev/null +++ b/test/event/FieldVisibilityEventHandlerTest.js @@ -0,0 +1,145 @@ +/** + * Test case for FieldVisibilityEventHandler + * + * 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 event = require( '../../' ).event; +const expect = require( 'chai' ).expect; +const Class = require( 'easejs' ).Class; + +const { + FieldVisibilityEventHandler: Sut, + UnknownEventError +} = event; + + +describe( 'FieldVisibilityEventHandler', () => +{ + it( 'shows/hides each element index', done => + { + const name = 'field_name'; + const shown = { [name]: [] }; + const hidden = { [name]: [] }; + + const sut = Sut( + createMockStepUi( + name, + ( field, index ) => shown[ field ].push( index ), + ( field, index ) => hidden[ field ].push( index ) + ), + createStubDataProvider() + ); + + // purposefully sparse indexes + const show_indexes = [ 2, 4, ]; + const hide_indexes = [ 0, 3, ]; + + const show_data = { + elementName: name, + indexes: show_indexes, + }; + + const hide_data = { + elementName: name, + indexes: hide_indexes, + }; + + sut.handle( 'show', () => + { + // implicitly ensures proper name is passed + expect( shown[ name ] ).to.deep.equal( show_indexes ); + + sut.handle( 'hide', () => + { + expect( hidden[ name ] ).to.deep.equal( hide_indexes ); + done(); + }, hide_data ); + }, show_data ); + } ); + + + it( 'throws error given unknown event', () => + { + expect( () => + { + Sut( createMockStepUi() ).handle( 'unknown', () => {}, {} ); + } ).to.throw( UnknownEventError ); + } ); + + + it( 'ignores unknown groups', done => + { + expect( () => + { + Sut( { + getCurrentStep: () => ( { getElementGroup: () => null } ) + } ).handle( 'hide', done, {} ) + } ).to.not.throw( Error ); + } ); + + + it( 'clears failures on hidden fields', done => + { + const name = 'foo_bar'; + + const hide_data = { + elementName: name, + indexes: [ 0 ], + }; + + Sut( + createMockStepUi( name, () => {}, () => {} ), + createStubDataProvider( failures => + { + expect( failures ) + .to.deep.equal( [ name ] ) + + // we don't care about the rest of the processing at this + // point + done(); + } ) + ).handle( 'hide', () => {}, hide_data ); + } ); +} ); + + +function createMockStepUi( expected_name, showf, hidef ) +{ + return { + getCurrentStep: () => ( { + getElementGroup( field_name ) + { + expect( field_name ).to.equal( expected_name ); + + return { + showField: showf, + hideField: hidef, + }; + } + } ), + }; +} + + +function createStubDataProvider( fail_callback ) +{ + return { + clearFailures: fail_callback || () => {}, + }; +} |