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); + }); + }); + }); +});