Mike Gerwitz

Activist for User Freedom

aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMike Gerwitz <gerwitzm@lovullo.com>2017-02-22 09:36:25 -0500
committerMike Gerwitz <gerwitzm@lovullo.com>2017-02-22 09:53:59 -0500
commit45889f556fa039a56a2556711b44ed7f5fe63bee (patch)
tree564ae27d45a5bb9e35a390e60c8228701014dbf7
parentd3c1d6f9b4120604b72068371f39a41ab261e706 (diff)
downloadliza-45889f556fa039a56a2556711b44ed7f5fe63bee.tar.gz
liza-45889f556fa039a56a2556711b44ed7f5fe63bee.tar.bz2
liza-45889f556fa039a56a2556711b44ed7f5fe63bee.zip
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
-rw-r--r--src/dapi/DataApiManager.js4
-rw-r--r--test/dapi/DataApiManagerTest.js83
2 files changed, 87 insertions, 0 deletions
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 <http://www.gnu.org/licenses/>.
+ *
+ * @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 ];
+ },
+ };
+}