From abaa2d0adc828e4e6680f724616eb9df78be955d Mon Sep 17 00:00:00 2001 From: Richard Cox Date: Wed, 6 Sep 2023 15:41:56 +0100 Subject: [PATCH] Add unit tests for sortBy --- shell/utils/__tests__/sort.test.ts | 61 ++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 shell/utils/__tests__/sort.test.ts diff --git a/shell/utils/__tests__/sort.test.ts b/shell/utils/__tests__/sort.test.ts new file mode 100644 index 0000000000..350b46e43a --- /dev/null +++ b/shell/utils/__tests__/sort.test.ts @@ -0,0 +1,61 @@ +import { sortBy } from '@shell/utils/sort'; + +describe('fx: sort', () => { + describe('sortBy', () => { + const testSortBy = (ary: T[], key: string[], expected: T[], desc?: boolean) => { + const result = sortBy(ary, key, desc); + + expect(result).toStrictEqual(expected); + }; + + it.each([ + [[{ a: 1 }, { a: 9 }], ['a'], [{ a: 1 }, { a: 9 }]], + [[{ a: 2 }, { a: 1 }], ['a'], [{ a: 1 }, { a: 2 }]], + ])('should sort by single property', (ary, key, expected) => { + testSortBy(ary, key, expected); + }); + + it.each([ + [[{ a: 1, b: 1 }, { a: 9, b: 9 }], ['a', 'b'], [{ a: 1, b: 1 }, { a: 9, b: 9 }]], + [[{ a: 2, b: 2 }, { a: 1, b: 1 }], ['a', 'b'], [{ a: 1, b: 1 }, { a: 2, b: 2 }]], + [[{ a: 2, b: 1 }, { a: 1, b: 9 }], ['a', 'b'], [{ a: 1, b: 9 }, { a: 2, b: 1 }]], + [[{ a: 1, b: 2 }, { a: 9, b: 1 }], ['a', 'b'], [{ a: 1, b: 2 }, { a: 9, b: 1 }]], + [[{ a: 1, b: 1 }, { a: 9, b: 9 }], ['a', 'b'], [{ a: 1, b: 1 }, { a: 9, b: 9 }]], + ])('should sort by two properties (primary property always first)', (ary, key, expected) => { + testSortBy(ary, key, expected); + }); + + it.each([ + [[{ a: 1, b: 1 }, { a: 1, b: 9 }], ['a', 'b'], [{ a: 1, b: 1 }, { a: 1, b: 9 }]], + [[{ a: 1, b: 2 }, { a: 1, b: 1 }], ['a', 'b'], [{ a: 1, b: 1 }, { a: 1, b: 2 }]], + ])('should sort by two properties (primary property the same)', (ary, key, expected) => { + testSortBy(ary, key, expected); + }); + + describe('descending', () => { + it.each([ + [[{ a: 1 }, { a: 9 }], ['a'], [{ a: 9 }, { a: 1 }]], + [[{ a: 2 }, { a: 1 }], ['a'], [{ a: 2 }, { a: 1 }]], + ])('should sort by single property', (ary, key, expected) => { + testSortBy(ary, key, expected, true); + }); + + it.each([ + [[{ a: 1, b: 1 }, { a: 9, b: 9 }], ['a', 'b'], [{ a: 9, b: 9 }, { a: 1, b: 1 }]], + [[{ a: 2, b: 2 }, { a: 1, b: 1 }], ['a', 'b'], [{ a: 2, b: 2 }, { a: 1, b: 1 }]], + [[{ a: 2, b: 1 }, { a: 1, b: 9 }], ['a', 'b'], [{ a: 2, b: 1 }, { a: 1, b: 9 }]], + [[{ a: 1, b: 2 }, { a: 9, b: 1 }], ['a', 'b'], [{ a: 9, b: 1 }, { a: 1, b: 2 }]], + [[{ a: 1, b: 1 }, { a: 9, b: 9 }], ['a', 'b'], [{ a: 9, b: 9 }, { a: 1, b: 1 }]], + ])('should sort by two properties', (ary, key, expected) => { + testSortBy(ary, key, expected, true); + }); + + it.each([ + [[{ a: 1, b: 1 }, { a: 1, b: 9 }], ['a', 'b'], [{ a: 1, b: 9 }, { a: 1, b: 1 }]], + [[{ a: 1, b: 2 }, { a: 1, b: 1 }], ['a', 'b'], [{ a: 1, b: 2 }, { a: 1, b: 1 }]], + ])('should sort by two properties (primary property the same)', (ary, key, expected) => { + testSortBy(ary, key, expected, true); + }); + }); + }); +});