|
| 1 | +import { describe, expect, it } from 'vite-plus/test'; |
| 2 | +import { patternToRegex } from '../../src/pages/manifest.ts'; |
| 3 | +import { compareRouteSpecificity, routeSegmentKind } from '../../src/router/route-order.ts'; |
| 4 | + |
| 5 | +function route(pattern: string) { |
| 6 | + return { |
| 7 | + pattern, |
| 8 | + catchAll: pattern.split('/').some((segment) => routeSegmentKind(segment) === 'rest'), |
| 9 | + }; |
| 10 | +} |
| 11 | + |
| 12 | +function matches(pattern: string, url: string): boolean { |
| 13 | + return new RegExp(patternToRegex(pattern).slice(1, -1)).test(url); |
| 14 | +} |
| 15 | + |
| 16 | +describe('compareRouteSpecificity', () => { |
| 17 | + it.each([ |
| 18 | + // A static segment beats a dynamic one, wherever it is |
| 19 | + ['/users/:id', '/:lang/about', '/users/about'], |
| 20 | + ['/users/new/:tab', '/users/:id/edit', '/users/new/edit'], |
| 21 | + ['/:lang/about', '/:lang/:slug', '/en/about'], |
| 22 | + ['/about', '/:slug', '/about'], |
| 23 | + // Parameter names never decide |
| 24 | + ['/:lang/about', '/:category/:item', '/fr/about'], |
| 25 | + ['/:z/about', '/:a/:b', '/fr/about'], |
| 26 | + // A plain parameter beats a catch-all, and catch-alls come last |
| 27 | + ['/docs/:path{.+}', '/:slug{.+}', '/docs/intro'], |
| 28 | + ['/:lang/:path{.+}', '/:slug{.+}', '/fr/team/jobs'], |
| 29 | + ['/:lang/:slug', '/docs/:path{.+}', '/docs/intro'], |
| 30 | + ['/files/:name', '/files/*', '/files/a'], |
| 31 | + ])('ranks %s ahead of %s, which both match %s', (specific, general, url) => { |
| 32 | + expect(matches(specific, url)).toBe(true); |
| 33 | + expect(matches(general, url)).toBe(true); |
| 34 | + for (const routes of [ |
| 35 | + [route(specific), route(general)], |
| 36 | + [route(general), route(specific)], |
| 37 | + ]) { |
| 38 | + expect(routes.sort(compareRouteSpecificity).map((r) => r.pattern)).toEqual([ |
| 39 | + specific, |
| 40 | + general, |
| 41 | + ]); |
| 42 | + } |
| 43 | + }); |
| 44 | + |
| 45 | + it('gives the same order for any input order', () => { |
| 46 | + const patterns = [ |
| 47 | + '/', |
| 48 | + '/zzz', |
| 49 | + '/files/a*b', |
| 50 | + '/:lang', |
| 51 | + '/:lang/about', |
| 52 | + '/:lang/:slug', |
| 53 | + '/:category/:item', |
| 54 | + '/users/:id', |
| 55 | + '/users/:id/edit', |
| 56 | + '/users/new/:tab', |
| 57 | + '/docs/:path{.+}', |
| 58 | + '/:lang/:path{.+}', |
| 59 | + '/:slug{.+}', |
| 60 | + '/*', |
| 61 | + ]; |
| 62 | + const expected = [ |
| 63 | + // Fully static routes first, so Pages can look them up exactly |
| 64 | + '/', |
| 65 | + '/files/a*b', |
| 66 | + '/zzz', |
| 67 | + '/users/new/:tab', |
| 68 | + '/users/:id', |
| 69 | + '/users/:id/edit', |
| 70 | + '/:lang', |
| 71 | + '/:lang/about', |
| 72 | + '/:category/:item', |
| 73 | + '/:lang/:slug', |
| 74 | + // Catch-all routes last |
| 75 | + '/docs/:path{.+}', |
| 76 | + '/:lang/:path{.+}', |
| 77 | + '/*', |
| 78 | + '/:slug{.+}', |
| 79 | + ]; |
| 80 | + for (let shift = 0; shift < patterns.length; shift++) { |
| 81 | + const rotated = [...patterns.slice(shift), ...patterns.slice(0, shift)]; |
| 82 | + expect(rotated.reverse().map(route).sort(compareRouteSpecificity)).toEqual( |
| 83 | + expected.map(route), |
| 84 | + ); |
| 85 | + } |
| 86 | + }); |
| 87 | +}); |
0 commit comments