Skip to content

Commit

Permalink
fix(di): provided registries must be global
Browse files Browse the repository at this point in the history
  • Loading branch information
awinogradov committed Jan 16, 2019
1 parent 5b6f618 commit 57fdb8b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
3 changes: 1 addition & 2 deletions packages/di/di.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export type RegistryContext = Record<string, Registry>;

const registryContext = createContext<RegistryContext>({});
const RegistryProvider = registryContext.Provider;
const providedRegistries: RegistryContext = {};

export const RegistryConsumer = registryContext.Consumer;

Expand All @@ -14,8 +15,6 @@ export function withRegistry(...registries: Registry[]) {
return (
<RegistryConsumer>
{contextRegistries => {
const providedRegistries: RegistryContext = {};

registries.forEach(registry => {
const overrides = contextRegistries[registry.id];

Expand Down
30 changes: 30 additions & 0 deletions packages/di/test/di.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -227,5 +227,35 @@ describe('@bem-react/di', () => {
expect(render(<Compositor/>).text()).eq('contentextra');
expect(render(<OverridedCompositor/>).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<ICommonProps> = () => <span>content</span>;
const Element2Presenter: React.SFC<ICommonProps> = () => (
<ComponentRegistryConsumer id="Compositor">
{({ Element }: ICompositorRegistry) => <><Element/>extra</>}
</ComponentRegistryConsumer>
);
const Element2 = withRegistry(element2Registry)(Element2Presenter);

interface ICompositorRegistry {
Element: React.ComponentType<ICommonProps>;
Element2: React.ComponentType<ICommonProps>;
}

compositorRegistry.set('Element', Element1);
compositorRegistry.set('Element2', Element2);

const CompositorPresenter: React.SFC<ICommonProps> = () => (
<ComponentRegistryConsumer id="Compositor">
{({ Element2 }: ICompositorRegistry) => <Element2/>}
</ComponentRegistryConsumer>
);

const Compositor = withRegistry(compositorRegistry)(CompositorPresenter);

expect(render(<Compositor/>).text()).eq('contentextra');
});
});
});

0 comments on commit 57fdb8b

Please sign in to comment.