@@ -12,15 +12,18 @@ let _anything = jasmine.anything();
12
12
describe ( "UrlRouter" , function ( ) {
13
13
let router : UIRouter ;
14
14
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 ;
20
20
21
21
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 ) ) ;
24
27
25
28
beforeEach ( function ( ) {
26
29
router = new UIRouter ( ) ;
@@ -302,6 +305,31 @@ describe("UrlRouter", function () {
302
305
expect ( matchlog ) . toEqual ( [ 'AAA' , 'p1' ] ) ;
303
306
} ) ;
304
307
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
+
305
333
it ( "should prioritize a path with a static string over a param 6" , ( ) => {
306
334
urlRouter . when ( matcher ( '/foo/:p1/:p2/tail' ) , log ( 'p1' ) ) ;
307
335
urlRouter . when ( matcher ( '/foo' , '/:p1/AAA' , '/:p2' ) , log ( 'AAA' ) ) ;
0 commit comments