1
0
Fork 0

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
master
Mike Gerwitz 2017-02-22 09:36:25 -05:00
parent d3c1d6f9b4
commit 45889f556f
2 changed files with 87 additions and 0 deletions

View File

@ -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 );
}
} );
}

View File

@ -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 ];
},
};
}