-
Notifications
You must be signed in to change notification settings - Fork 64
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
Partially registries merge #384
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,231 @@ | ||
// tslint:disable no-shadowed-variable | ||
import React from 'react'; | ||
import { describe, it } from 'mocha'; | ||
import { expect } from 'chai'; | ||
import { render } from 'enzyme'; | ||
|
||
import { Registry } from '../di'; | ||
import { Registry, withRegistry, RegistryConsumer, ComponentRegistryConsumer } from '../di'; | ||
|
||
interface ICommonProps { | ||
className?: string; | ||
} | ||
|
||
describe('@bem-react/di', () => { | ||
describe('Registry', () => { | ||
it('should set components and return this by id', () => { | ||
const registry = new Registry({ id: 'id' }); | ||
it('should set and components by id', () => { | ||
const registry = new Registry({ id: 'registry' }); | ||
const Component1 = () => null; | ||
const Component2 = () => <span/>; | ||
|
||
registry | ||
.set('id-1', Component1) | ||
.set('id-2', Component2); | ||
|
||
expect(registry.get('id-1')).to.eq(Component1); | ||
expect(registry.get('id-2')).to.eq(Component2); | ||
}); | ||
|
||
it('should return list of components', () => { | ||
const registry = new Registry({ id: 'registry' }); | ||
const Component1 = () => null; | ||
const Component2 = () => <span/>; | ||
|
||
registry | ||
.set('id-1', Component1) | ||
.set('id-2', Component2); | ||
|
||
const snapshot = { | ||
'id-1': Component1, | ||
'id-2': Component2, | ||
}; | ||
|
||
expect(registry.snapshot()).to.eql(snapshot); | ||
}); | ||
|
||
it('should merge registries', () => { | ||
const registry = new Registry({ id: 'registry' }); | ||
const Component1 = () => null; | ||
const Component2 = () => null; | ||
const Component2 = () => <span/>; | ||
|
||
registry | ||
.set('id-1', Component1) | ||
.set('id-2', Component2); | ||
|
||
expect(registry.get('id-1')).to.equal(Component1); | ||
expect(registry.get('id-2')).to.equal(Component2); | ||
const overrides = new Registry({ id: 'overrides' }); | ||
const Component1Overrided = () => <div/>; | ||
|
||
overrides.set('id-1', Component1Overrided); | ||
|
||
const snapshot = { | ||
'id-1': Component1Overrided, | ||
'id-2': Component2, | ||
}; | ||
|
||
expect(registry.merge(overrides).snapshot()).to.eql(snapshot); | ||
}); | ||
|
||
it('should not affect registry in merge with undefined', () => { | ||
const registry = new Registry({ id: 'registry' }); | ||
const Component1 = () => null; | ||
const Component2 = () => <span/>; | ||
|
||
registry | ||
.set('id-1', Component1) | ||
.set('id-2', Component2); | ||
|
||
const snapshot = { | ||
'id-1': Component1, | ||
'id-2': Component2, | ||
}; | ||
|
||
// @ts-ignore to check inside logic | ||
expect(registry.merge().snapshot()).to.eql(snapshot); | ||
}); | ||
|
||
it('should throw error when component doesn\'t exist', () => { | ||
const registry = new Registry({ id: 'registry' }); | ||
|
||
expect(() => registry.get('id')).to.throw('Component with id \'id\' not found.'); | ||
}); | ||
}); | ||
|
||
describe.skip('withRegistry', () => { | ||
// TODO: Add test for withRegistry | ||
return null; | ||
describe('withRegistry', () => { | ||
it('should provide registry to context', () => { | ||
const compositorRegistry = new Registry({ id: 'Compositor' }); | ||
const Element: React.SFC<ICommonProps> = () => <span>content</span>; | ||
|
||
interface ICompositorRegistry { | ||
Element: React.ComponentType<ICommonProps>; | ||
} | ||
|
||
compositorRegistry.set('Element', Element); | ||
|
||
const CompositorPresenter: React.SFC<ICommonProps> = () => ( | ||
<RegistryConsumer> | ||
{registries => { | ||
const registry = registries['Compositor']; | ||
const { Element } = registry.snapshot<ICompositorRegistry>(); | ||
|
||
return <Element/>; | ||
}} | ||
</RegistryConsumer> | ||
); | ||
|
||
const Compositor = withRegistry(compositorRegistry)(CompositorPresenter); | ||
|
||
expect(render(<Compositor/>).text()).eq('content'); | ||
}); | ||
|
||
it('should provide assign registry with component', () => { | ||
const compositorRegistry = new Registry({ id: 'Compositor' }); | ||
const Element: React.SFC<ICommonProps> = () => <span>content</span>; | ||
|
||
interface ICompositorRegistry { | ||
Element: React.ComponentType<ICommonProps>; | ||
} | ||
|
||
compositorRegistry.set('Element', Element); | ||
|
||
const CompositorPresenter: React.SFC<ICommonProps> = () => ( | ||
<ComponentRegistryConsumer id="Compositor"> | ||
{({ Element }: ICompositorRegistry) => <Element/>} | ||
</ComponentRegistryConsumer> | ||
); | ||
|
||
const Compositor = withRegistry(compositorRegistry)(CompositorPresenter); | ||
|
||
expect(render(<Compositor/>).text()).eq('content'); | ||
}); | ||
|
||
it('should override components in registry by context', () => { | ||
const compositorRegistry = new Registry({ id: 'Compositor' }); | ||
const Element: React.SFC<ICommonProps> = () => <span>content</span>; | ||
|
||
const overridedCompositorRegistry = new Registry({ id: 'Compositor' }); | ||
const OverridedElement: React.SFC<ICommonProps> = () => <span>overrided</span>; | ||
|
||
interface ICompositorRegistry { | ||
Element: React.ComponentType<ICommonProps>; | ||
} | ||
|
||
compositorRegistry.set('Element', Element); | ||
overridedCompositorRegistry.set('Element', OverridedElement); | ||
|
||
const CompositorPresenter: React.SFC<ICommonProps> = () => { | ||
const Content: React.SFC<ICommonProps> = withRegistry(overridedCompositorRegistry)(() => ( | ||
<ComponentRegistryConsumer id="Compositor"> | ||
{({ Element }: ICompositorRegistry) => <Element/>} | ||
</ComponentRegistryConsumer> | ||
)); | ||
|
||
return <Content/>; | ||
}; | ||
|
||
const Compositor = withRegistry(compositorRegistry)(CompositorPresenter); | ||
|
||
expect(render(<Compositor/>).text()).eq('overrided'); | ||
}); | ||
|
||
it('should override components in registry from top node', () => { | ||
const compositorRegistry = new Registry({ id: 'Compositor', inverted: true }); | ||
const Element: React.SFC<ICommonProps> = () => <span>content</span>; | ||
|
||
const overridedCompositorRegistry = new Registry({ id: 'Compositor' }); | ||
const OverridedElement: React.SFC<ICommonProps> = () => <span>overrided</span>; | ||
|
||
interface ICompositorRegistry { | ||
Element: React.ComponentType<ICommonProps>; | ||
} | ||
|
||
compositorRegistry.set('Element', Element); | ||
overridedCompositorRegistry.set('Element', OverridedElement); | ||
|
||
const CompositorPresenter: React.SFC<ICommonProps> = () => ( | ||
<ComponentRegistryConsumer id="Compositor"> | ||
{({ Element }: ICompositorRegistry) => <Element/>} | ||
</ComponentRegistryConsumer> | ||
); | ||
|
||
const Compositor = withRegistry(compositorRegistry)(CompositorPresenter); | ||
const OverridedCompositor = withRegistry(overridedCompositorRegistry)(Compositor); | ||
|
||
expect(render(<Compositor/>).text()).eq('content'); | ||
expect(render(<OverridedCompositor/>).text()).eq('overrided'); | ||
}); | ||
|
||
it('should partially override components in registry', () => { | ||
const compositorRegistry = new Registry({ id: 'Compositor', inverted: true }); | ||
const Element1: React.SFC<ICommonProps> = () => <span>content</span>; | ||
const Element2: React.SFC<ICommonProps> = () => <span>extra</span>; | ||
|
||
const overridedCompositorRegistry = new Registry({ id: 'Compositor' }); | ||
const OverridedElement: React.SFC<ICommonProps> = () => <span>overrided</span>; | ||
|
||
interface ICompositorRegistry { | ||
Element1: React.ComponentType<ICommonProps>; | ||
Element2: React.ComponentType<ICommonProps>; | ||
} | ||
|
||
compositorRegistry.set('Element1', Element1); | ||
compositorRegistry.set('Element2', Element2); | ||
overridedCompositorRegistry.set('Element1', OverridedElement); | ||
|
||
const CompositorPresenter: React.SFC<ICommonProps> = () => ( | ||
<ComponentRegistryConsumer id="Compositor"> | ||
{({ Element1, Element2 }: ICompositorRegistry) => ( | ||
<> | ||
<Element1/> | ||
<Element2/> | ||
</> | ||
)} | ||
</ComponentRegistryConsumer> | ||
); | ||
|
||
const Compositor = withRegistry(compositorRegistry)(CompositorPresenter); | ||
const OverridedCompositor = withRegistry(overridedCompositorRegistry)(Compositor); | ||
|
||
expect(render(<Compositor/>).text()).eq('contentextra'); | ||
expect(render(<OverridedCompositor/>).text()).eq('overridedextra'); | ||
}); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Какой смысл от такого теста?
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.
В покрытии 100% ;)))