Skip to content
Source
Sign inSign up
TanStack/router
Open History to see the last commit

parse-segment.test.ts

52 lines1.8 KB

1import { expect, test } from 'vitest'
2import {
3 SEGMENT_TYPE_OPTIONAL_PARAM,
4 SEGMENT_TYPE_PARAM,
5 SEGMENT_TYPE_PATHNAME,
6 SEGMENT_TYPE_WILDCARD,
7 parseSegment,
8} from '../src/new-process-route-tree'
9​
10test('keeps offsets beyond the former 16-bit scratch buffer range', () => {
11 const path = `/${'x'.repeat(70_000)}/$id`
12 const literal = parseSegment(path, 1, 70_001)
13 expect(typeof literal).toBe('string')
14 expect(literal.length).toBe(70_000)
15 expect(parseSegment(path, 70_002, path.length)).toEqual([
16 SEGMENT_TYPE_PARAM,
17 'id',
18 '',
19 '',
20 ])
21})
22​
23test.each([
24 [SEGMENT_TYPE_PATHNAME, 'pre{bad}{$id}', '', 'pre{bad}{$id}', ''],
25 [SEGMENT_TYPE_PATHNAME, '{-$}', '', '{-$}', ''],
26 [SEGMENT_TYPE_PATHNAME, '{--$id}', '', '{--$id}', ''],
27 [SEGMENT_TYPE_PARAM, '}pre{$a{b}.txt', '}pre', 'a{b', '.txt'],
28 [SEGMENT_TYPE_PARAM, '$id{suffix}', '', 'id{suffix}', ''],
29 [SEGMENT_TYPE_PARAM, '{$$}', '', '$', ''],
30 [SEGMENT_TYPE_OPTIONAL_PARAM, 'pre{-$a$b}suffix', 'pre', 'a$b', 'suffix'],
31 [SEGMENT_TYPE_WILDCARD, 'pre{$}suffix/tail', 'pre', '_splat', 'suffix/tail'],
32 [SEGMENT_TYPE_WILDCARD, '$/tail', '', '_splat', ''],
33] as const)(
34 'preserves the first segment grammar for %s: %s',
35 (kind, source, prefix, value, suffix) => {
36 const path = `/base/${source}`
37 const next = path.indexOf('/', 6)
38 const parsed = parseSegment(path, 6, next === -1 ? path.length : next)
39 expect(
40 typeof parsed === 'string' ? SEGMENT_TYPE_PATHNAME : parsed[0 /* kind */],
41 ).toBe(kind)
42 expect(typeof parsed === 'string' ? '' : parsed[2 /* prefix */]).toBe(
43 prefix,
44 )
45 expect(typeof parsed === 'string' ? parsed : parsed[1 /* key */]).toBe(
46 value,
47 )
48 expect(
49 typeof parsed === 'string' ? '' : (parsed[3 /* suffix */] ?? ''),
50 ).toBe(suffix)
51 },