Skip to content

Commit 985e1f1

Browse files
committed
test: earlier routes should have matching priority
1 parent 3025e82 commit 985e1f1

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

packages/router/__tests__/matcher/addingRemoving.spec.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,64 @@ describe('Matcher: adding and removing records', () => {
418418
)
419419
})
420420

421+
it('should give priority to earlier routes', () => {
422+
const matcher = createRouterMatcher([], {})
423+
matcher.addRoute({ path: '/:id(123\\d*)', component, name: 'first' })
424+
matcher.addRoute({ path: '/:id(12\\d*)', component, name: 'second' })
425+
matcher.addRoute({ path: '/:id(1\\d*)', component, name: 'third' })
426+
matcher.addRoute({ path: '/:id(\\d+)', component, name: 'fourth' })
427+
expect(matcher.resolve({ path: '/1239' }, currentLocation)).toMatchObject({
428+
name: 'first',
429+
})
430+
expect(matcher.resolve({ path: '/1299' }, currentLocation)).toMatchObject({
431+
name: 'second',
432+
})
433+
expect(matcher.resolve({ path: '/1999' }, currentLocation)).toMatchObject({
434+
name: 'third',
435+
})
436+
expect(matcher.resolve({ path: '/9999' }, currentLocation)).toMatchObject({
437+
name: 'fourth',
438+
})
439+
})
440+
441+
it('should give priority to earlier child routes', () => {
442+
const matcher = createRouterMatcher([], {})
443+
matcher.addRoute({
444+
path: '/user',
445+
name: 'parent',
446+
children: [
447+
{ path: '', component, name: 'root' },
448+
{ path: ':id(123\\d*)', component, name: 'first' },
449+
{ path: ':id(12\\d*)', component, name: 'second' },
450+
{ path: ':id(1\\d*)', component, name: 'third' },
451+
{ path: ':id(\\d+)', component, name: 'fourth' },
452+
],
453+
})
454+
expect(matcher.resolve({ path: '/user/' }, currentLocation)).toMatchObject({
455+
name: 'root',
456+
})
457+
expect(
458+
matcher.resolve({ path: '/user/1239' }, currentLocation)
459+
).toMatchObject({
460+
name: 'first',
461+
})
462+
expect(
463+
matcher.resolve({ path: '/user/1299' }, currentLocation)
464+
).toMatchObject({
465+
name: 'second',
466+
})
467+
expect(
468+
matcher.resolve({ path: '/user/1999' }, currentLocation)
469+
).toMatchObject({
470+
name: 'third',
471+
})
472+
expect(
473+
matcher.resolve({ path: '/user/9999' }, currentLocation)
474+
).toMatchObject({
475+
name: 'fourth',
476+
})
477+
})
478+
421479
describe('warnings', () => {
422480
mockWarn()
423481

0 commit comments

Comments
 (0)