|
6 | 6 | import { MatcherPatternPathStar } from './matcher-pattern-path-star' |
7 | 7 | import { miss } from './errors' |
8 | 8 | import { definePathParamParser } from './param-parsers' |
| 9 | +import { mockWarn } from '../../../../__tests__/vitest-mock-warn' |
9 | 10 |
|
10 | 11 | describe('MatcherPatternPathStatic', () => { |
11 | 12 | describe('match()', () => { |
@@ -129,6 +130,8 @@ describe('MatcherPatternPathStar', () => { |
129 | 130 | }) |
130 | 131 |
|
131 | 132 | describe('MatcherPatternPathDynamic', () => { |
| 133 | + mockWarn() |
| 134 | + |
132 | 135 | it('single param', () => { |
133 | 136 | const pattern = new MatcherPatternPathDynamic( |
134 | 137 | /^\/teams\/([^/]+?)\/b$/i, |
@@ -376,6 +379,48 @@ describe('MatcherPatternPathDynamic', () => { |
376 | 379 | ) |
377 | 380 | }) |
378 | 381 |
|
| 382 | + it('repeatable param in a sub segment', () => { |
| 383 | + // produced by file-based routing for `set.[ids]+.other` |
| 384 | + const pattern = new MatcherPatternPathDynamic( |
| 385 | + /^\/set\/(.+?)\/other$/, |
| 386 | + { |
| 387 | + ids: [{}, true], |
| 388 | + }, |
| 389 | + [['set/', 1, '/other']] |
| 390 | + ) |
| 391 | + |
| 392 | + expect(pattern.match('/set/123/other')).toEqual({ ids: ['123'] }) |
| 393 | + expect(pattern.match('/set/123/456/other')).toEqual({ |
| 394 | + ids: ['123', '456'], |
| 395 | + }) |
| 396 | + expect(pattern.build({ ids: ['123'] })).toBe('/set/123/other') |
| 397 | + expect(pattern.build({ ids: ['123', '456'] })).toBe('/set/123/456/other') |
| 398 | + }) |
| 399 | + |
| 400 | + it('multiple repeatable params in a sub segment', () => { |
| 401 | + // produced by file-based routing for `before.[ids]+.middle.[other]+.after` |
| 402 | + const pattern = new MatcherPatternPathDynamic( |
| 403 | + /^\/before\/(.+?)\/middle\/(.+?)\/after$/, |
| 404 | + { |
| 405 | + ids: [{}, true], |
| 406 | + other: [{}, true], |
| 407 | + }, |
| 408 | + [['before/', 1, '/middle/', 1, '/after']] |
| 409 | + ) |
| 410 | + |
| 411 | + expect(pattern.match('/before/a/middle/c/after')).toEqual({ |
| 412 | + ids: ['a'], |
| 413 | + other: ['c'], |
| 414 | + }) |
| 415 | + expect(pattern.match('/before/a/b/middle/c/d/after')).toEqual({ |
| 416 | + ids: ['a', 'b'], |
| 417 | + other: ['c', 'd'], |
| 418 | + }) |
| 419 | + expect(pattern.build({ ids: ['a', 'b'], other: ['c', 'd'] })).toBe( |
| 420 | + '/before/a/b/middle/c/d/after' |
| 421 | + ) |
| 422 | + }) |
| 423 | + |
379 | 424 | it('can have a trailing slash after a single param', () => { |
380 | 425 | const pattern = new MatcherPatternPathDynamic( |
381 | 426 | /^\/teams\/([^/]+?)\/$/i, |
|
0 commit comments