Skip to content

Commit

Permalink
fix(nested): id was incorrect since it could be reused multiple times
Browse files Browse the repository at this point in the history
  • Loading branch information
bodinsamuel committed Jun 29, 2023
1 parent 88e06b2 commit 9c3784c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
16 changes: 16 additions & 0 deletions src/payload/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
9 changes: 5 additions & 4 deletions src/payload/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -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;
}

/**
Expand Down

0 comments on commit 9c3784c

Please sign in to comment.