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

path-decoder.test.ts

92 lines3.4 KB

1import { afterEach, expect, test, vi } from 'vitest'
2import { createMemoryHistory } from '@tanstack/history'
3import { BaseRootRoute, BaseRoute } from '../src'
4import * as pathUtils from '../src/path'
5import { createTestRouter } from './routerTestUtils'
6​
7afterEach(() => vi.restoreAllMocks())
8​
9test.each([
10 { allowed: ['.', '@'], input: 'x%40.y', expected: 'x@.y' },
11 { allowed: ['*', '@'], input: 'pre%40*post', expected: 'pre@*post' },
12 { allowed: ['(', ')', '@'], input: '(%40)', expected: '(@)' },
13 { allowed: ['|', '@'], input: '%7C%40%2F', expected: '|@%2F' },
14 { allowed: ['12@', '1'], input: '12%40', expected: '12@' },
15 { allowed: ['%', '/'], input: '%252F/%2F', expected: '%2F//' },
16 { allowed: ['@', '@', '+'], input: '%40%2B', expected: '@+' },
17 { allowed: [], input: '%40', expected: '%40' },
18])(
19 'compiles literal decoder alternatives: $allowed',
20 ({ allowed, input, expected }) => {
21 expect(pathUtils.compileDecodeCharMap(allowed)(input)).toBe(expected)
22 },
24​
25function setup(allowed?: Array<'@' | '+'>) {
26 const root = new BaseRootRoute({})
27 const item = new BaseRoute({ getParentRoute: () => root, path: '/items/$id' })
28 const router = createTestRouter({
29 routeTree: root.addChildren([item]),
30 history: createMemoryHistory({ initialEntries: ['/'] }),
31 pathParamsAllowedCharacters: allowed,
32 scrollRestoration: false,
33 })
34 router.history.destroy()
35 const build = () =>
36 router.buildLocation({ to: '/items/$id', params: { id: '@+' } }).href
37 return { router, build }
39​
40test('keeps the decoder across provider option updates', () => {
41 const allowed: Array<'@' | '+'> = ['@']
42 const { router, build } = setup(allowed)
43 const decoder = router.pathParamsDecoder
44 expect(build()).toBe('/items/@%2B')
45 const compile = vi.spyOn(pathUtils, 'compileDecodeCharMap')
46 for (let count = 0; count < 3; count++) {
47 router.update({
48 ...router.options,
49 context: { count },
50 })
51 expect(build()).toBe('/items/@%2B')
52 expect(router.pathParamsDecoder).toBe(decoder)
53 }
54 expect(compile).not.toHaveBeenCalled()
55})
56​
57test('requires a new router to apply changes to the original character array', () => {
58 const allowed: Array<'@' | '+'> = ['@']
59 const { router, build } = setup(allowed)
60 const decoder = router.pathParamsDecoder
61 expect(build()).toBe('/items/@%2B')
62 allowed[0] = '+'
63 router.update({ context: {} })
64 expect(router.pathParamsDecoder).toBe(decoder)
65 expect(build()).toBe('/items/@%2B')
66 expect(
67 router.buildLocation({ to: '/items/$id', params: { id: 'new@+' } }).href,
68 ).toBe('/items/new@%2B')
69 expect(setup(allowed).build()).toBe('/items/%40+')
70})
71​
72test('does not change encoding through unsupported option updates', () => {
73 const { router, build } = setup(['@'])
74 const compile = vi.spyOn(pathUtils, 'compileDecodeCharMap')
75 expect(build()).toBe('/items/@%2B')
76 // @ts-expect-error Path encoding can only be configured at construction.
77 router.update({ pathParamsAllowedCharacters: undefined })
78 expect(
79 router.buildLocation({ to: '/items/$id', params: { id: 'new@+' } }).href,
80 ).toBe('/items/new@%2B')
81 expect(compile).not.toHaveBeenCalled()
82})
83​
84test('does not create a decoder for empty or omitted allowed characters', () => {
85 const compile = vi.spyOn(pathUtils, 'compileDecodeCharMap')
86 for (const allowed of [undefined, []]) {
87 const { router, build } = setup(allowed)
88 expect(router.pathParamsDecoder).toBeUndefined()
89 expect(build()).toBe('/items/%40%2B')
90 }
91 expect(compile).not.toHaveBeenCalled()
92})