From 45889f556fa039a56a2556711b44ed7f5fe63bee Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Wed, 22 Feb 2017 09:36:25 -0500 Subject: [PATCH] DataApiManager: emit fieldLoaded after request completes The intent of this is to allow for clearing errors after fields load (e.g. a "field loading" message if the user attempts to continue past the step when a field hasn't yet finished loading). * src/dapi/DataApiManager.js (getApiData): Emit fieldLoaded after request completes. * test/dapi/DataApiManagerTest.js: Add test case. DEV-2299 --- src/dapi/DataApiManager.js | 4 ++ test/dapi/DataApiManagerTest.js | 83 +++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 test/dapi/DataApiManagerTest.js diff --git a/src/dapi/DataApiManager.js b/src/dapi/DataApiManager.js index 0f2e469..12fe8af 100644 --- a/src/dapi/DataApiManager.js +++ b/src/dapi/DataApiManager.js @@ -126,6 +126,9 @@ module.exports = Class( 'DataApiManager' ) * * The optional request id permits cancelling requests if necessary. * + * Once a field has finished loading, a `fieldLoaded` event will be + * emitted with `name` and `index`. + * * TODO: refactor argument list; it's just been built upon too much and * needs reordering * @@ -197,6 +200,7 @@ module.exports = Class( 'DataApiManager' ) // clear the pending flag _self._pendingApiCall[ id ] = undefined; + _self.emit( 'fieldLoaded', name, +index ); } } ); } diff --git a/test/dapi/DataApiManagerTest.js b/test/dapi/DataApiManagerTest.js new file mode 100644 index 0000000..089569a --- /dev/null +++ b/test/dapi/DataApiManagerTest.js @@ -0,0 +1,83 @@ +/** + * Test of DataApi + * + * Copyright (C) 2017 LoVullo Associates, Inc. + * + * This file is part of liza. + * + * 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 . + * + * @todo This needs tests for the rest of StagingBucket + */ + +"use strict"; + +const root = require( '../../' ); +const expect = require( 'chai' ).expect; +const Sut = root.dapi.DataApiManager; + + +describe( 'DataApiManager', () => +{ + it( 'emits fieldLoaded event once request is complete', done => + { + const dapi = createStubDapi(); + const type = 'loaded-test'; + const name = 'foo'; + + // this is intentionally a string to test casting; see below + const index = "2"; + + const fail = e => { throw( e ) }; + + Sut( createStubDapiFactory( { [type]: dapi } ) ) + .on( 'fieldLoaded', ( given_name, given_index ) => + { + expect( given_name ).to.equal( name ); + + // should cast index to number + expect( given_index ).to.equal( +index ); + + done(); + } ) + .setApis( { [type]: { type: type } } ) + .getApiData( type, {}, ()=>{}, name, index, {}, fail ); + } ); +} ); + + +function createStubDapi() +{ + return { + request( _, callback ) + { + callback(); + }, + + on() { + return this; + }, + }; +} + + +function createStubDapiFactory( dapis ) +{ + return { + fromType( type ) + { + return dapis[ type ]; + }, + }; +}