From 140cec8603ea4dcaa9daf65225cfe358f20af412 Mon Sep 17 00:00:00 2001 From: Giovanni Ferrari Date: Tue, 26 Mar 2024 11:14:47 +0100 Subject: [PATCH] Fix bug using splice() without specifing the number of elements to remove (#6199) Signed-off-by: Giovanni Ferrari --- .../src/domain/client-side/configService.ts | 4 ++-- .../supervisor/src/tests/configService.test.ts | 12 +++++++----- .../business/store/lightcards/lightcards-store.ts | 4 ++-- .../card/components/card-ack/card-ack.component.ts | 2 +- 4 files changed, 12 insertions(+), 10 deletions(-) diff --git a/node-services/supervisor/src/domain/client-side/configService.ts b/node-services/supervisor/src/domain/client-side/configService.ts index 2ba9382267..c802f20e79 100644 --- a/node-services/supervisor/src/domain/client-side/configService.ts +++ b/node-services/supervisor/src/domain/client-side/configService.ts @@ -88,7 +88,7 @@ export default class ConfigService { (entity) => entity.entityId === supervisedEntity.entityId ); if (index >= 0) { - this.supervisorConfig.entitiesToSupervise.splice(index); + this.supervisorConfig.entitiesToSupervise.splice(index, 1); } this.supervisorConfig.entitiesToSupervise.push(supervisedEntity); this.save(); @@ -99,7 +99,7 @@ export default class ConfigService { const index = this.supervisorConfig.entitiesToSupervise.findIndex((entity) => entity.entityId === entityId); if (index >= 0) { - this.supervisorConfig.entitiesToSupervise.splice(index); + this.supervisorConfig.entitiesToSupervise.splice(index, 1); } this.save(); } diff --git a/node-services/supervisor/src/tests/configService.test.ts b/node-services/supervisor/src/tests/configService.test.ts index c230b87d40..9beabda9fe 100644 --- a/node-services/supervisor/src/tests/configService.test.ts +++ b/node-services/supervisor/src/tests/configService.test.ts @@ -17,13 +17,15 @@ class SupervisorDatabaseServiceStub extends SupervisorDatabaseService { public supervisedEntities: any[] = []; public async getSupervisedEntities(): Promise { - return this.supervisedEntities; + const supervised: any[] = []; + this.supervisedEntities.forEach((entity) => supervised.push(entity)); + return supervised; } public async saveSupervisedEntity(supervisedEntity: any): Promise { const index = this.supervisedEntities.findIndex((entity) => entity.entityId === supervisedEntity.entityId); if (index >= 0) { - this.supervisedEntities.splice(index); + this.supervisedEntities.splice(index, 1); } this.supervisedEntities.push(supervisedEntity); } @@ -31,7 +33,7 @@ class SupervisorDatabaseServiceStub extends SupervisorDatabaseService { public async deleteSupervisedEntity(id: string): Promise { const index = this.supervisedEntities.findIndex((entity) => entity.entityId === id); if (index >= 0) { - this.supervisedEntities.splice(index); + this.supervisedEntities.splice(index, 1); } } } @@ -124,9 +126,9 @@ describe('config service', function () { {entityId: 'ENTITY3', supervisors: ['ENTITY2']} ]); - await configService.deleteSupervisedEntity('ENTITY3'); + await configService.deleteSupervisedEntity('ENTITY4'); expect(configService.getSupervisorConfig().entitiesToSupervise).toEqual([ - {entityId: 'ENTITY4', supervisors: ['ENTITY1']} + {entityId: 'ENTITY3', supervisors: ['ENTITY2']} ]); }); }); diff --git a/ui/main/src/app/business/store/lightcards/lightcards-store.ts b/ui/main/src/app/business/store/lightcards/lightcards-store.ts index a691420b83..a3e6e3bc9c 100644 --- a/ui/main/src/app/business/store/lightcards/lightcards-store.ts +++ b/ui/main/src/app/business/store/lightcards/lightcards-store.ts @@ -282,7 +282,7 @@ export class LightCardsStore { const children = this.childCards.get(card.parentCardId); if (children) { const childIndex = children.findIndex((child) => child.id === card.id); - if (childIndex >= 0) children.splice(childIndex); + if (childIndex >= 0) children.splice(childIndex, 1); children.push(card); } else { this.childCards.set(card.parentCardId, [card]); @@ -399,7 +399,7 @@ export class LightCardsStore { if (card?.entitiesAcks && entitiesAcksToRemove) { entitiesAcksToRemove.forEach((entityToRemove) => { const indexToRemove = card.entitiesAcks.indexOf(entityToRemove); - if (indexToRemove >= 0) card.entitiesAcks.splice(indexToRemove); + if (indexToRemove >= 0) card.entitiesAcks.splice(indexToRemove, 1); }); card.hasBeenAcknowledged = AcknowledgeService.isLightCardHasBeenAcknowledgedByUserOrByUserEntity(card); this.lightCardsEvents.next(this.lightCards); diff --git a/ui/main/src/app/modules/card/components/card-ack/card-ack.component.ts b/ui/main/src/app/modules/card/components/card-ack/card-ack.component.ts index 193e74c78a..b8668e52bf 100644 --- a/ui/main/src/app/modules/card/components/card-ack/card-ack.component.ts +++ b/ui/main/src/app/modules/card/components/card-ack/card-ack.component.ts @@ -100,7 +100,7 @@ export class CardAckComponent implements OnInit, OnChanges, OnDestroy { if (lightcard?.entitiesAcks && entitiesAcksToRemove) { entitiesAcksToRemove.forEach((entityToRemove) => { const indexToRemove = lightcard.entitiesAcks.indexOf(entityToRemove); - if (indexToRemove >= 0) lightcard.entitiesAcks.splice(indexToRemove); + if (indexToRemove >= 0) lightcard.entitiesAcks.splice(indexToRemove, 1); }); } this.card = {