From 7df2cb221e932eb77c2dae8c1c552486944115e4 Mon Sep 17 00:00:00 2001 From: Sebastian Stehle Date: Sun, 6 Oct 2019 18:21:27 +0200 Subject: [PATCH] Tests for array extensions. --- .../framework/utils/array-extensions.spec.ts | 64 +++++++++++++++++++ .../app/framework/utils/array-extensions.ts | 2 +- 2 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 src/Squidex/app/framework/utils/array-extensions.spec.ts diff --git a/src/Squidex/app/framework/utils/array-extensions.spec.ts b/src/Squidex/app/framework/utils/array-extensions.spec.ts new file mode 100644 index 000000000..7db28d393 --- /dev/null +++ b/src/Squidex/app/framework/utils/array-extensions.spec.ts @@ -0,0 +1,64 @@ +/* + * Squidex Headless CMS + * + * @license + * Copyright (c) Squidex UG (haftungsbeschränkt). All rights reserved. + */ + +describe('ArrayExtensions', () => { + it('should return same array when replaying by property with null value', () => { + const array_0 = [{ id: 1 }, { id: 2 }]; + const array_1 = array_0.replaceBy('id', null!); + + expect(array_1).toBe(array_0); + }); + + it('should return new array when replaying by property', () => { + const array_0 = [{ id: 1, v: 10 }, { id: 2, v: 20 }]; + const array_1 = array_0.replaceBy('id', { id: 1, v: 30 }); + + expect(array_1).toEqual([{ id: 1, v: 30 }, { id: 2, v: 20 }]); + }); + + it('should return same array when removing by property with null value', () => { + const array_0 = [{ id: 1 }, { id: 2 }]; + const array_1 = array_0.removeBy('id', null!); + + expect(array_1).toBe(array_0); + }); + + it('should return new array when removing by property', () => { + const array_0 = [{ id: 1 }, { id: 2 }]; + const array_1 = array_0.removeBy('id', { id: 1 }); + + expect(array_1).toEqual([{ id: 2 }]); + }); + + it('should return same array when removing with null value', () => { + const array_0 = [1, 2, 3]; + const array_1 = array_0.removed(null!); + + expect(array_1).toBe(array_0); + }); + + it('should return new array when removing', () => { + const array_0 = [1, 2, 3]; + const array_1 = array_0.removed(2); + + expect(array_1).toEqual([1, 3]); + }); + + it('should sort by value', () => { + const array_0 = [3, 1, 2]; + const array_1 = array_0.sorted(); + + expect(array_1).toEqual([1, 2, 3]); + }); + + it('should sort by property', () => { + const array_0 = [{ id: 'C' }, { id: 'b' }, { id: 'A' }]; + const array_1 = array_0.sortedByString(x => x.id); + + expect(array_1).toEqual([{ id: 'A' }, { id: 'b' }, { id: 'C' }]); + }); +}); \ No newline at end of file diff --git a/src/Squidex/app/framework/utils/array-extensions.ts b/src/Squidex/app/framework/utils/array-extensions.ts index b16ced6f5..7033e1d83 100644 --- a/src/Squidex/app/framework/utils/array-extensions.ts +++ b/src/Squidex/app/framework/utils/array-extensions.ts @@ -56,7 +56,7 @@ Array.prototype.removed = function(value?: T) { return this.filter((v: T) => v !== value); }; -Array.prototype.sorted = function() { +Array.prototype.sorted = function() { const copy = [...this]; copy.sort();