From 87a98330b1e37996794b74d68ba07f43ab45243d Mon Sep 17 00:00:00 2001 From: Robert Anderson Date: Fri, 26 May 2023 11:32:05 +1000 Subject: [PATCH 1/2] Add tests, including one failing test --- packages/data/src/test/privateAPIs.js | 38 +++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/packages/data/src/test/privateAPIs.js b/packages/data/src/test/privateAPIs.js index 93df00d287d600..d72934921222eb 100644 --- a/packages/data/src/test/privateAPIs.js +++ b/packages/data/src/test/privateAPIs.js @@ -4,6 +4,7 @@ import { createRegistry } from '../registry'; import createReduxStore from '../redux-store'; import { unlock } from '../private-apis'; +import { createRegistrySelector } from '../factory'; describe( 'Private data APIs', () => { let registry; @@ -164,6 +165,43 @@ describe( 'Private data APIs', () => { ); expect( subPrivateSelectors.getSecretDiscount() ).toEqual( 800 ); } ); + + it( 'should support registry selectors', () => { + const groceryStore = createStore(); + const otherStore = createReduxStore( 'other', { + reducer: ( state = {} ) => state, + } ); + registry.register( otherStore ); + unlock( otherStore ).registerPrivateSelectors( { + getPrice: createRegistrySelector( + ( select ) => () => select( groceryStore ).getPublicPrice() + ), + } ); + const privateSelectors = unlock( registry.select( otherStore ) ); + expect( privateSelectors.getPrice() ).toEqual( 1000 ); + } ); + + it( 'should support calling a private registry selector from a public selector', () => { + const groceryStore = createStore(); + const storeA = createReduxStore( 'a', { + reducer: ( state = {} ) => state, + } ); + registry.register( storeA ); + const getPrice = createRegistrySelector( + ( select ) => () => select( groceryStore ).getPublicPrice() + ); + unlock( storeA ).registerPrivateSelectors( { + getPrice, + } ); + const storeB = createReduxStore( 'b', { + reducer: ( state = {} ) => state, + selectors: { + getPrice: ( state ) => getPrice( state ), + }, + } ); + registry.register( storeB ); + expect( registry.select( storeB ).getPrice() ).toEqual( 1000 ); + } ); } ); describe( 'private actions', () => { From 452bb376af60529bdfb54bf9e88a4a48cd9148fb Mon Sep 17 00:00:00 2001 From: Robert Anderson Date: Tue, 30 May 2023 09:27:53 +1000 Subject: [PATCH 2/2] Clarify failing test --- packages/data/src/test/privateAPIs.js | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/packages/data/src/test/privateAPIs.js b/packages/data/src/test/privateAPIs.js index d72934921222eb..0a0a62c915f975 100644 --- a/packages/data/src/test/privateAPIs.js +++ b/packages/data/src/test/privateAPIs.js @@ -183,24 +183,23 @@ describe( 'Private data APIs', () => { it( 'should support calling a private registry selector from a public selector', () => { const groceryStore = createStore(); - const storeA = createReduxStore( 'a', { - reducer: ( state = {} ) => state, - } ); - registry.register( storeA ); - const getPrice = createRegistrySelector( - ( select ) => () => select( groceryStore ).getPublicPrice() - ); - unlock( storeA ).registerPrivateSelectors( { - getPrice, - } ); - const storeB = createReduxStore( 'b', { + const store = createReduxStore( 'a', { reducer: ( state = {} ) => state, selectors: { - getPrice: ( state ) => getPrice( state ), + getPriceWithShippingAndTax: ( state ) => + getPriceWithShipping( state ) * 1.1, }, } ); - registry.register( storeB ); - expect( registry.select( storeB ).getPrice() ).toEqual( 1000 ); + registry.register( store ); + const getPriceWithShipping = createRegistrySelector( + ( select ) => () => select( groceryStore ).getPublicPrice() + 5 + ); + unlock( store ).registerPrivateSelectors( { + getPriceWithShipping, + } ); + expect( + registry.select( store ).getPriceWithShippingAndTax() + ).toEqual( 1105.5 ); } ); } );