Skip to content

Commit

Permalink
fix(di): fix partial merge of registries
Browse files Browse the repository at this point in the history
  • Loading branch information
Vittly authored and yarastqt committed Jun 29, 2021
1 parent 2e5c735 commit 3ef511d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
12 changes: 2 additions & 10 deletions packages/di/di.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,6 @@ export class Registry {
if (!otherRegistryEntities.hasOwnProperty(entityName)) continue

clone.entities[entityName] = this.mergeEntities(
this.id,
clone.entities[entityName],
otherRegistryEntities[entityName],
)
Expand All @@ -232,19 +231,12 @@ export class Registry {
/**
* Returns extended or replaced entity
*
* @param id entity entry id
* @param base base implementation
* @param overrides overridden implementation
*/
private mergeEntities(
id: string,
base: IRegistryEntity,
overrides: IRegistryEntity,
): IRegistryEntity {
private mergeEntities(base: IRegistryEntity, overrides: IRegistryEntity): IRegistryEntity {
if (isOverload(overrides)) {
if (!base && __DEV__) {
throw new Error(`Overload has no base in Registry '${id}'.`)
}
if (!base) return overrides

if (isOverload(base)) {
// If both entities are hocs, then create compose-hoc
Expand Down
34 changes: 34 additions & 0 deletions packages/di/test/di.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,40 @@ describe('@bem-react/di', () => {
expect(render(<AppSuperExtended />).text()).toEqual('super extended content')
})

test('should partially extend components in registry', () => {
const baseRegistry = new Registry({ id: 'registry' })
const extendedLeftRegistry = new Registry({ id: 'registry' })
const extendedRightRegistry = new Registry({ id: 'registry' })
const Left: React.FC = () => <span>left</span>
const Right: React.FC = () => <span>right</span>
const Extension = (Base: React.FC) => () => (
<div>
extended <Base />
</div>
)

baseRegistry.fill({ Left, Right })
extendedLeftRegistry.extends<React.FC>('Left', Extension)
extendedRightRegistry.extends<React.FC>('Right', Extension)

const AppPresenter: React.FC = () => (
<RegistryConsumer id="registry">
{({ Left, Right }) => (
<>
<Left />
<Right />
</>
)}
</RegistryConsumer>
)

const App = withRegistry(baseRegistry)(AppPresenter)
const AppExtended = withRegistry(extendedLeftRegistry, extendedRightRegistry)(App)

expect(render(<App />).text()).toEqual('leftright')
expect(render(<AppExtended />).text()).toEqual('extended leftextended right')
})

test('should extend other values in registry', () => {
const baseRegistry = new Registry({ id: 'registry' }).fill({
prop: 'foo',
Expand Down

0 comments on commit 3ef511d

Please sign in to comment.