|
| 1 | +/** |
| 2 | + * (c) 2026, Micro:bit Educational Foundation and contributors |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: MIT |
| 5 | + */ |
| 6 | +import { sortBy } from "./sort-util"; |
| 7 | + |
| 8 | +describe("sortBy", () => { |
| 9 | + it("sorts ascending by a single iteratee", () => { |
| 10 | + expect(sortBy(["banana", "apple", "cherry"], (s) => s)).toEqual([ |
| 11 | + "apple", |
| 12 | + "banana", |
| 13 | + "cherry", |
| 14 | + ]); |
| 15 | + }); |
| 16 | + |
| 17 | + it("uses later iteratees as tie-breakers", () => { |
| 18 | + const files = [ |
| 19 | + { name: "b.py", main: false }, |
| 20 | + { name: "main.py", main: true }, |
| 21 | + { name: "a.py", main: false }, |
| 22 | + ]; |
| 23 | + expect( |
| 24 | + sortBy( |
| 25 | + files, |
| 26 | + (f) => !f.main, |
| 27 | + (f) => f.name |
| 28 | + ).map((f) => f.name) |
| 29 | + ).toEqual(["main.py", "a.py", "b.py"]); |
| 30 | + }); |
| 31 | + |
| 32 | + it("is stable and does not mutate its input", () => { |
| 33 | + const input = [ |
| 34 | + { key: 1, id: "first" }, |
| 35 | + { key: 0, id: "a" }, |
| 36 | + { key: 1, id: "second" }, |
| 37 | + ]; |
| 38 | + const result = sortBy(input, (x) => x.key); |
| 39 | + expect(result.map((x) => x.id)).toEqual(["a", "first", "second"]); |
| 40 | + expect(input.map((x) => x.id)).toEqual(["first", "a", "second"]); |
| 41 | + }); |
| 42 | +}); |
0 commit comments