Files
webstudio/apps/builder/app/canvas/shared/routing-priority.test.ts
Bogdan Chadkin 19a98fa1ae fix: order routes before navigating in preview (#5311)
Ref https://github.com/webstudio-is/webstudio/issues/5223

Navigating within preview didn't work well because we relied on order of
creation and more inclusive routes like `/*` could match before
`/my-page`.

Here fixed it with routes sorting. The algorithm is very simplified but
covers our usecases within builder.
2025-07-01 18:45:25 +03:00

51 lines
1.3 KiB
TypeScript

import { test, expect } from "vitest";
import { comparePatterns } from "./routing-priority";
test("sort top-level patterns", () => {
const patterns = ["/foo*", "/:foo", "/foo"];
const expected = ["/foo", "/:foo", "/foo*"];
expect(patterns.toSorted(comparePatterns)).toEqual(expected);
});
test("sort static paths", () => {
const patterns = ["/a/z", "/a/b/c", "/a/c", "/a/b"];
const expected = ["/a/b", "/a/c", "/a/z", "/a/b/c"];
expect(patterns.toSorted(comparePatterns)).toEqual(expected);
});
test("sort mixed static, dynamic, spread at multiple levels", () => {
const patterns = [
"/foo",
"/:id",
"/bar*",
"/foo/bar",
"/foo/:id",
"/foo/bar*",
];
const expected = [
// static first-segment
"/foo",
"/foo/bar",
"/foo/:id",
"/foo/bar*",
// dynamic then spread at top level
"/:id",
"/bar*",
];
expect(patterns.toSorted(comparePatterns)).toEqual(expected);
});
test("sort deeply nested mixed segments", () => {
const patterns = ["/u/bar", "/u/:id", "/u/bar/b", "/u/:id/c", "/u/bar/*"];
const expected = [
// static second-segment
"/u/bar",
"/u/bar/b",
"/u/bar/*",
// dynamic second-segment
"/u/:id",
"/u/:id/c",
];
expect(patterns.toSorted(comparePatterns)).toEqual(expected);
});