diff --git a/src/payload/index.test.ts b/src/payload/index.test.ts index 748170cd..b44835eb 100644 --- a/src/payload/index.test.ts +++ b/src/payload/index.test.ts @@ -16,4 +16,20 @@ describe('Payload', () => { expect(root.toJson()).toMatchSnapshot(); }); + + it('should reuse correctly nested dependency', () => { + const root = new Payload({ name: 'root', folderPath: '/' }); + root.addChild( + new Payload({ name: 'GCP manual', folderPath: '', tech: 'gcp' }) + ); + root.addChild( + new Payload({ name: 'PubSub', folderPath: '/', tech: 'gcp.pubsub' }) + ); + + expect(root.childs[0].name).toEqual('GCP manual'); + expect(root.childs[0].id).toEqual('4'); + expect(root.childs[1].name).toEqual('PubSub'); + expect(root.childs[1].id).toEqual('5'); + expect(root.childs[1].inComponent!.id).toEqual('4'); + }); }); diff --git a/src/payload/index.ts b/src/payload/index.ts index 143e6f0f..efd5c081 100644 --- a/src/payload/index.ts +++ b/src/payload/index.ts @@ -121,7 +121,7 @@ export class Payload implements Analyser { * Register a child to this Payload. * If a similar child is found at the same level, it will merge them. */ - addChild(service: Payload) { + addChild(service: Payload): Payload { const exist = this.childs.find((s) => { if (s.name === service.name) return true; if (s.tech && service.tech && s.tech === service.tech) return true; @@ -136,8 +136,8 @@ export class Payload implements Analyser { folderPath: service.path[0], tech: tech.key, }); - this.addChild(pl); - service.inComponent = pl; + const child = this.addChild(pl); + service.inComponent = child; } if (exist) { @@ -158,11 +158,12 @@ export class Payload implements Analyser { // Merge dependencies exist.dependencies = [...exist.dependencies, ...service.dependencies]; - return; + return exist; } service.setParent(this); this.childs.push(service); + return service; } /**