-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix private registry selectors #50985
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the failing test that I can't figure out how to fix. |
||
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 ), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test it bad, you can't really expect that it will work.
Where is the real-world situation where you're running into this kind of issue? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The private
That's the bug, isn't it? It shouldn't be lazy.
But then it won't be private? I shouldn't have named both selectors
In #50857, ^ Well, was a private registry selector. I've added a workaround for now. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for clarifying the test. In the previous version, behavior of store B depended on what was registered in store A, and that was fishy 🙂 I cherry-picked the tests into #51051 and fixed it there by pre-binding private selectors on store instantiation. I had to modify the test so that the private selector registration is done before the store is instantiated by The That should probably also be eventually fixed. We'll get to that sooner or later. @adamziel suggests this: createReduxStore( 'a', {
selectors: lock( { foo }, { privateFoo } )
} ); It's syntactically elegant, but semantically it's nonsense 🙂 To register a private selector, we need to What we'd like to do is:
One way to satisfy that could be: unlock( createReduxStore )( 'a', {
selectors: { foo },
privateSelectors: { privateFoo },
} ); Are there others? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This is just a way of passing the information into Exposing a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A private |
||
}, | ||
} ); | ||
registry.register( storeB ); | ||
expect( registry.select( storeB ).getPrice() ).toEqual( 1000 ); | ||
} ); | ||
} ); | ||
|
||
describe( 'private actions', () => { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a passing test for a fix I included in #50643.