From 1f8f7d7fc614b8e10fd7fc6c04d4cdbb881ef089 Mon Sep 17 00:00:00 2001 From: Andrew Duthie Date: Thu, 4 Apr 2019 15:53:12 -0400 Subject: [PATCH] Data: Return result of middleware chain in resolvers cache middleware (#14711) * Data: Return result of middleware chain in resolvers cache middleware * Data: Avoid returning result of store.dispatch in public interface --- packages/data/src/namespace-store/index.js | 5 ++- .../data/src/namespace-store/test/index.js | 41 +++++++++++++++++++ .../data/src/resolvers-cache-middleware.js | 2 +- packages/data/src/test/registry.js | 3 +- 4 files changed, 48 insertions(+), 3 deletions(-) diff --git a/packages/data/src/namespace-store/index.js b/packages/data/src/namespace-store/index.js index 8a53d28b300c3..3cdbc7a65a348 100644 --- a/packages/data/src/namespace-store/index.js +++ b/packages/data/src/namespace-store/index.js @@ -187,7 +187,10 @@ function mapSelectors( selectors, store, registry ) { * @return {Object} Actions mapped to the redux store provided. */ function mapActions( actions, store ) { - const createBoundAction = ( action ) => ( ...args ) => store.dispatch( action( ...args ) ); + const createBoundAction = ( action ) => ( ...args ) => { + store.dispatch( action( ...args ) ); + }; + return mapValues( actions, createBoundAction ); } diff --git a/packages/data/src/namespace-store/test/index.js b/packages/data/src/namespace-store/test/index.js index eba9e7e6697fc..a3ee80577ece1 100644 --- a/packages/data/src/namespace-store/test/index.js +++ b/packages/data/src/namespace-store/test/index.js @@ -39,4 +39,45 @@ describe( 'controls', () => { expect( action1 ).toBeCalled(); } ); } ); + + it( 'resolves in expected order', ( done ) => { + const actions = { + wait: () => ( { type: 'WAIT' } ), + receive: ( items ) => ( { type: 'RECEIVE', items } ), + }; + + registry.registerStore( 'store', { + reducer: ( state = null, action ) => { + if ( action.type === 'RECEIVE' ) { + return action.items; + } + + return state; + }, + selectors: { + getItems: ( state ) => state, + }, + resolvers: { + * getItems() { + yield actions.wait(); + yield actions.receive( [ 1, 2, 3 ] ); + }, + }, + controls: { + WAIT() { + return new Promise( ( resolve ) => process.nextTick( resolve ) ); + }, + }, + } ); + + registry.subscribe( () => { + const isFinished = registry.select( 'store' ).hasFinishedResolution( 'getItems' ); + if ( isFinished ) { + expect( registry.select( 'store' ).getItems() ).toEqual( [ 1, 2, 3 ] ); + done(); + } + } ); + + registry.select( 'store' ).getItems(); + } ); } ); diff --git a/packages/data/src/resolvers-cache-middleware.js b/packages/data/src/resolvers-cache-middleware.js index 7477b36d805b5..0e661442e180d 100644 --- a/packages/data/src/resolvers-cache-middleware.js +++ b/packages/data/src/resolvers-cache-middleware.js @@ -30,7 +30,7 @@ const createResolversCacheMiddleware = ( registry, reducerKey ) => () => ( next registry.dispatch( 'core/data' ).invalidateResolution( reducerKey, selectorName, args ); } ); } ); - next( action ); + return next( action ); }; export default createResolversCacheMiddleware; diff --git a/packages/data/src/test/registry.js b/packages/data/src/test/registry.js index b828cd0199159..e44accd0eab6f 100644 --- a/packages/data/src/test/registry.js +++ b/packages/data/src/test/registry.js @@ -555,7 +555,8 @@ describe( 'createRegistry', () => { }, } ); - registry.dispatch( 'counter' ).increment(); // state = 1 + const dispatchResult = registry.dispatch( 'counter' ).increment(); // state = 1 + expect( dispatchResult ).toBe( undefined ); // Actions are implementation detail. registry.dispatch( 'counter' ).increment( 4 ); // state = 5 expect( store.getState() ).toBe( 5 ); } );