Skip to content

Commit 83d70fc

Browse files
test(urlRouter): Add tests for sorting by urlmatcher path length
1 parent 5f82fdf commit 83d70fc

File tree

1 file changed

+35
-7
lines changed

1 file changed

+35
-7
lines changed

test/urlRouterSpec.ts

+35-7
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,18 @@ let _anything = jasmine.anything();
1212
describe("UrlRouter", function () {
1313
let router: UIRouter;
1414
let urlRouter: UrlRouter,
15-
urlService: UrlService,
16-
urlMatcherFactory: UrlMatcherFactory,
17-
stateService: StateService,
18-
stateRegistry: StateRegistry,
19-
locationService: LocationServices;
15+
urlService: UrlService,
16+
urlMatcherFactory: UrlMatcherFactory,
17+
stateService: StateService,
18+
stateRegistry: StateRegistry,
19+
locationService: LocationServices;
2020

2121
const matcher = (...strings: string[]) =>
22-
strings.reduce((prev: UrlMatcher, str) =>
23-
prev ? prev.append(urlMatcherFactory.compile(str)) : urlMatcherFactory.compile(str), undefined);
22+
strings.reduce((prev: UrlMatcher, str) =>
23+
prev ? prev.append(urlMatcherFactory.compile(str)) : urlMatcherFactory.compile(str), undefined);
24+
25+
const matcherRule = (...strings: string[]) =>
26+
urlRouter.urlRuleFactory.create(matcher(...strings));
2427

2528
beforeEach(function() {
2629
router = new UIRouter();
@@ -302,6 +305,31 @@ describe("UrlRouter", function () {
302305
expect(matchlog).toEqual(['AAA', 'p1']);
303306
});
304307

308+
// Tests for https://github.com/ui-router/core/issues/66
309+
it("should sort shorter paths before longer paths, all else equal", () => {
310+
const cmp = (urlRouter as any)._sortFn;
311+
expect(cmp(matcherRule('/'), matcherRule('/a'))).toBeLessThan(0);
312+
expect(cmp(matcherRule('/a'), matcherRule('/a/b'))).toBeLessThan(0);
313+
expect(cmp(matcherRule('/a'), matcherRule('/a/:id'))).toBeLessThan(0);
314+
expect(cmp(matcherRule('/a/b'), matcherRule('/a/b/c'))).toBeLessThan(0);
315+
});
316+
317+
it("should sort static strings before params", () => {
318+
const cmp = (urlRouter as any)._sortFn;
319+
expect(cmp(matcherRule('/a'), matcherRule('/:id'))).toBeLessThan(0);
320+
expect(cmp(matcherRule('/a/:id'), matcherRule('/:id2/:id3'))).toBeLessThan(0);
321+
expect(cmp(matcherRule('/a/:id/:id2'), matcherRule('/:id3/:id4/:id5'))).toBeLessThan(0);
322+
expect(cmp(matcherRule('/a/:id/b/c'), matcherRule('/d/:id2/e/:id3'))).toBeLessThan(0);
323+
});
324+
325+
it("should sort same-level paths equally", () => {
326+
const cmp = (urlRouter as any)._sortFn;
327+
expect(cmp(matcherRule('/a'), matcherRule('/b'))).toBe(0);
328+
expect(cmp(matcherRule('/a/x'), matcherRule('/b/x'))).toBe(0);
329+
expect(cmp(matcherRule('/:id1'), matcherRule('/:id2'))).toBe(0);
330+
expect(cmp(matcherRule('/a/:id1'), matcherRule('/b/:id2'))).toBe(0);
331+
});
332+
305333
it("should prioritize a path with a static string over a param 6", () => {
306334
urlRouter.when(matcher('/foo/:p1/:p2/tail'), log('p1'));
307335
urlRouter.when(matcher('/foo', '/:p1/AAA', '/:p2'), log('AAA'));

0 commit comments

Comments
 (0)