From: Eduardo San Martin Morote Date: Sat, 14 Dec 2019 16:01:36 +0000 (+0100) Subject: test(parser): fix tests X-Git-Tag: v4.0.0-alpha.0~147 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=18446684d72ca0a870b55e1ce95802cf7142c308;p=thirdparty%2Fvuejs%2Frouter.git test(parser): fix tests --- diff --git a/__tests__/matcher/path-parser.spec.ts b/__tests__/matcher/path-parser.spec.ts index 631a75b3..d0c6573a 100644 --- a/__tests__/matcher/path-parser.spec.ts +++ b/__tests__/matcher/path-parser.spec.ts @@ -317,6 +317,7 @@ describe('Path parser', () => { ], ]) }) + // end of describe token }) describe('tokensToParser', () => { @@ -390,7 +391,7 @@ describe('Path parser', () => { }) it('param*', () => { - matchRegExp('^/((?:\\d+)(?:/(?:\\d+))*)?$', [ + matchRegExp('^(?:/((?:\\d+)(?:/(?:\\d+))*))?$', [ [ { type: TokenType.Param, @@ -404,7 +405,27 @@ describe('Path parser', () => { }) it('param?', () => { - matchRegExp('^/(\\d+)?$', [ + matchRegExp('^(?:/(\\d+))?$', [ + [ + { + type: TokenType.Param, + value: 'id', + regexp: '\\d+', + repeatable: false, + optional: true, + }, + ], + ]) + }) + + it('static and param?', () => { + matchRegExp('^/ab(?:/(\\d+))?$', [ + [ + { + type: TokenType.Static, + value: 'ab', + }, + ], [ { type: TokenType.Param, @@ -430,6 +451,26 @@ describe('Path parser', () => { ], ]) }) + + it('static and param+', () => { + matchRegExp('^/ab/((?:\\d+)(?:/(?:\\d+))*)$', [ + [ + { + type: TokenType.Static, + value: 'ab', + }, + ], + [ + { + type: TokenType.Param, + value: 'id', + regexp: '\\d+', + repeatable: true, + optional: false, + }, + ], + ]) + }) // end of describe }) @@ -547,8 +588,9 @@ describe('Path parser', () => { it('param optional followed by static', () => { matchParams('/:a?/one', '/two/one', { a: 'two' }) - // the second one is never matched - matchParams('/:a?/one', '/one', null) + // since the first one is optional + matchParams('/:a?/one', '/one', { a: '' }) + matchParams('/:a?/one', '/two', null) // can only match one time matchParams('/:a?/one', '/two/three/one', null) matchParams('/:a*/one', '/two/one', { a: ['two'] }) @@ -588,9 +630,10 @@ describe('Path parser', () => { ReturnType['parse']>, null >, - expectedUrl: string + expectedUrl: string, + options?: Parameters[1] ) { - const pathParser = tokensToParser(tokenizePath(path)) + const pathParser = tokensToParser(tokenizePath(path), options) expect(pathParser.stringify(params)).toEqual(expectedUrl) } @@ -599,6 +642,11 @@ describe('Path parser', () => { matchStringify('/home', {}, '/home') }) + it('works with trailing slash', () => { + matchStringify('/home/', {}, '/home/') + matchStringify('/home/', {}, '/home/', { strict: true }) + }) + it('single param one segment', () => { matchStringify('/:id', { id: 'one' }, '/one') })