Skip to content

Commit 3ef511d

Browse files
Vittlyyarastqt
authored andcommitted
fix(di): fix partial merge of registries
1 parent 2e5c735 commit 3ef511d

2 files changed

Lines changed: 36 additions & 10 deletions

File tree

packages/di/di.tsx

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,6 @@ export class Registry {
220220
if (!otherRegistryEntities.hasOwnProperty(entityName)) continue
221221

222222
clone.entities[entityName] = this.mergeEntities(
223-
this.id,
224223
clone.entities[entityName],
225224
otherRegistryEntities[entityName],
226225
)
@@ -232,19 +231,12 @@ export class Registry {
232231
/**
233232
* Returns extended or replaced entity
234233
*
235-
* @param id entity entry id
236234
* @param base base implementation
237235
* @param overrides overridden implementation
238236
*/
239-
private mergeEntities(
240-
id: string,
241-
base: IRegistryEntity,
242-
overrides: IRegistryEntity,
243-
): IRegistryEntity {
237+
private mergeEntities(base: IRegistryEntity, overrides: IRegistryEntity): IRegistryEntity {
244238
if (isOverload(overrides)) {
245-
if (!base && __DEV__) {
246-
throw new Error(`Overload has no base in Registry '${id}'.`)
247-
}
239+
if (!base) return overrides
248240

249241
if (isOverload(base)) {
250242
// If both entities are hocs, then create compose-hoc

packages/di/test/di.test.tsx

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,40 @@ describe('@bem-react/di', () => {
243243
expect(render(<AppSuperExtended />).text()).toEqual('super extended content')
244244
})
245245

246+
test('should partially extend components in registry', () => {
247+
const baseRegistry = new Registry({ id: 'registry' })
248+
const extendedLeftRegistry = new Registry({ id: 'registry' })
249+
const extendedRightRegistry = new Registry({ id: 'registry' })
250+
const Left: React.FC = () => <span>left</span>
251+
const Right: React.FC = () => <span>right</span>
252+
const Extension = (Base: React.FC) => () => (
253+
<div>
254+
extended <Base />
255+
</div>
256+
)
257+
258+
baseRegistry.fill({ Left, Right })
259+
extendedLeftRegistry.extends<React.FC>('Left', Extension)
260+
extendedRightRegistry.extends<React.FC>('Right', Extension)
261+
262+
const AppPresenter: React.FC = () => (
263+
<RegistryConsumer id="registry">
264+
{({ Left, Right }) => (
265+
<>
266+
<Left />
267+
<Right />
268+
</>
269+
)}
270+
</RegistryConsumer>
271+
)
272+
273+
const App = withRegistry(baseRegistry)(AppPresenter)
274+
const AppExtended = withRegistry(extendedLeftRegistry, extendedRightRegistry)(App)
275+
276+
expect(render(<App />).text()).toEqual('leftright')
277+
expect(render(<AppExtended />).text()).toEqual('extended leftextended right')
278+
})
279+
246280
test('should extend other values in registry', () => {
247281
const baseRegistry = new Registry({ id: 'registry' }).fill({
248282
prop: 'foo',

0 commit comments

Comments
 (0)