|
| 1 | +import { test, expect } from "vitest"; |
| 2 | +import { comparePatterns } from "./routing-priority"; |
| 3 | + |
| 4 | +test("sort top-level patterns", () => { |
| 5 | + const patterns = ["/foo*", "/:foo", "/foo"]; |
| 6 | + const expected = ["/foo", "/:foo", "/foo*"]; |
| 7 | + expect(patterns.toSorted(comparePatterns)).toEqual(expected); |
| 8 | +}); |
| 9 | + |
| 10 | +test("sort static paths", () => { |
| 11 | + const patterns = ["/a/z", "/a/b/c", "/a/c", "/a/b"]; |
| 12 | + const expected = ["/a/b", "/a/c", "/a/z", "/a/b/c"]; |
| 13 | + expect(patterns.toSorted(comparePatterns)).toEqual(expected); |
| 14 | +}); |
| 15 | + |
| 16 | +test("sort mixed static, dynamic, spread at multiple levels", () => { |
| 17 | + const patterns = [ |
| 18 | + "/foo", |
| 19 | + "/:id", |
| 20 | + "/bar*", |
| 21 | + "/foo/bar", |
| 22 | + "/foo/:id", |
| 23 | + "/foo/bar*", |
| 24 | + ]; |
| 25 | + const expected = [ |
| 26 | + // static first-segment |
| 27 | + "/foo", |
| 28 | + "/foo/bar", |
| 29 | + "/foo/:id", |
| 30 | + "/foo/bar*", |
| 31 | + // dynamic then spread at top level |
| 32 | + "/:id", |
| 33 | + "/bar*", |
| 34 | + ]; |
| 35 | + expect(patterns.toSorted(comparePatterns)).toEqual(expected); |
| 36 | +}); |
| 37 | + |
| 38 | +test("sort deeply nested mixed segments", () => { |
| 39 | + const patterns = ["/u/bar", "/u/:id", "/u/bar/b", "/u/:id/c", "/u/bar/*"]; |
| 40 | + const expected = [ |
| 41 | + // static second-segment |
| 42 | + "/u/bar", |
| 43 | + "/u/bar/b", |
| 44 | + "/u/bar/*", |
| 45 | + // dynamic second-segment |
| 46 | + "/u/:id", |
| 47 | + "/u/:id/c", |
| 48 | + ]; |
| 49 | + expect(patterns.toSorted(comparePatterns)).toEqual(expected); |
| 50 | +}); |
0 commit comments