From 57fdb8bc89134adec6953bc78ec1081f135d6405 Mon Sep 17 00:00:00 2001 From: awinogradov Date: Tue, 15 Jan 2019 18:53:36 +0300 Subject: [PATCH] fix(di): provided registries must be global --- packages/di/di.tsx | 3 +-- packages/di/test/di.test.tsx | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/packages/di/di.tsx b/packages/di/di.tsx index 5d253b07..6ab9179e 100644 --- a/packages/di/di.tsx +++ b/packages/di/di.tsx @@ -5,6 +5,7 @@ export type RegistryContext = Record; const registryContext = createContext({}); const RegistryProvider = registryContext.Provider; +const providedRegistries: RegistryContext = {}; export const RegistryConsumer = registryContext.Consumer; @@ -14,8 +15,6 @@ export function withRegistry(...registries: Registry[]) { return ( {contextRegistries => { - const providedRegistries: RegistryContext = {}; - registries.forEach(registry => { const overrides = contextRegistries[registry.id]; diff --git a/packages/di/test/di.test.tsx b/packages/di/test/di.test.tsx index 6ab61180..9fad2b6f 100644 --- a/packages/di/test/di.test.tsx +++ b/packages/di/test/di.test.tsx @@ -227,5 +227,35 @@ describe('@bem-react/di', () => { expect(render().text()).eq('contentextra'); expect(render().text()).eq('overridedextra'); }); + + it('should allow to use any registry in context', () => { + const compositorRegistry = new Registry({ id: 'Compositor', inverted: true }); + const element2Registry = new Registry({ id: 'Element2', inverted: true }); + const Element1: React.SFC = () => content; + const Element2Presenter: React.SFC = () => ( + + {({ Element }: ICompositorRegistry) => <>extra} + + ); + const Element2 = withRegistry(element2Registry)(Element2Presenter); + + interface ICompositorRegistry { + Element: React.ComponentType; + Element2: React.ComponentType; + } + + compositorRegistry.set('Element', Element1); + compositorRegistry.set('Element2', Element2); + + const CompositorPresenter: React.SFC = () => ( + + {({ Element2 }: ICompositorRegistry) => } + + ); + + const Compositor = withRegistry(compositorRegistry)(CompositorPresenter); + + expect(render().text()).eq('contentextra'); + }); }); });