diff --git a/frontend/src/app/framework/utils/array-extensions.spec.ts b/frontend/src/app/framework/utils/array-extensions.spec.ts index f60868cec..57c581bf6 100644 --- a/frontend/src/app/framework/utils/array-extensions.spec.ts +++ b/frontend/src/app/framework/utils/array-extensions.spec.ts @@ -62,6 +62,14 @@ describe('ArrayExtensions', () => { expect(array_1).toBe(array_0); expect(array_1).toEqual([1, 3]); }); + + it('should return same array if value is not found', () => { + const array_0 = [1, 2, 3]; + const array_1 = array_0.remove(4); + + expect(array_1).toBe(array_0); + expect(array_1).toEqual([1, 2, 3]); + }); }); describe('removedBy', () => { @@ -96,6 +104,14 @@ describe('ArrayExtensions', () => { expect(array_1).toBe(array_0); expect(array_1).toEqual([{ id: 2 }]); }); + + it('should return same array if value is not found', () => { + const array_0 = [{ id: 1 }, { id: 2 }]; + const array_1 = array_0.removeBy('id', { id: 3 }); + + expect(array_1).toBe(array_0); + expect(array_1).toEqual([{ id: 1 }, { id: 2 }]); + }); }); it('should sort by value', () => { diff --git a/frontend/src/app/framework/utils/array-extensions.ts b/frontend/src/app/framework/utils/array-extensions.ts index ca5cd3ed3..9eb4b5c29 100644 --- a/frontend/src/app/framework/utils/array-extensions.ts +++ b/frontend/src/app/framework/utils/array-extensions.ts @@ -108,7 +108,12 @@ Array.prototype.removeBy = function(field: keyof T, value: T) { return self; } - self.splice(self.findIndex(x => x[field] === value[field]), 1); + const index = self.findIndex(x => x[field] === value[field]); + + // Splice with -1 would remove the last element. + if (index >= 0) { + self.splice(index, 1); + } return self; }; @@ -132,7 +137,10 @@ Array.prototype.remove = function(value?: T) { const index = self.indexOf(value); - self.splice(index, 1); + // Splice with -1 would remove the last element. + if (index >= 0) { + self.splice(index, 1); + } return self; }; diff --git a/frontend/src/app/shared/state/schemas.state.spec.ts b/frontend/src/app/shared/state/schemas.state.spec.ts index efe4e26a7..efc3498f2 100644 --- a/frontend/src/app/shared/state/schemas.state.spec.ts +++ b/frontend/src/app/shared/state/schemas.state.spec.ts @@ -132,6 +132,14 @@ describe('SchemasState', () => { expect([...schemasState.snapshot.addedCategories]).toEqualIgnoringProps([]); }); + it('should not remove other category if removed category was never added', () => { + schemasState.addCategory('A/B'); + schemasState.addCategory('schema-category3'); + schemasState.removeCategory('A'); + + expect([...schemasState.snapshot.addedCategories]).toEqualIgnoringProps(['A/B', 'schema-category3']); + }); + it('should return schema on select and reload if already loaded', () => { schemasService.setup(x => x.getSchema(app, schema1.name)) .returns(() => of(schema1)).verifiable(Times.exactly(2));