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

createServerHistory.test.ts

95 lines2.9 KB

1import { describe, expect, test, vi } from 'vitest'
2import {
3 createBrowserHistory,
4 createMemoryHistory,
5 createServerHistory,
6} from '../src'
7​
8describe('server history', () => {
9 test.each([
10 '/',
11 '/posts/123?sort=new#comments',
12 '/caf%C3%A9?q=%2F#//section',
13 '//example.com/path',
14 '/\\example.com/path',
15 '/path\nname?value=\u0000',
16 ])('parses and sanitizes %j like memory history', (href) => {
17 const history = createServerHistory(href)
18 const memory = createMemoryHistory({ initialEntries: [href] })
19 expect(history.location).toEqual({
20 ...memory.location,
21 state: {
22 key: expect.any(String),
23 __TSR_key: expect.any(String),
24 __TSR_index: 0,
25 },
26 })
27 expect(history.location.state.key).toBe(history.location.state.__TSR_key)
28 })
29​
30 test.each([
31 '//evil.example/path',
32 '/\\evil.example/path',
33 '\\/evil.example/path',
34 '\\\\evil.example/path',
35 ' \t/\r\\evil.example/path',
36 '/\t/evil.example/path',
37 '/a\tb?q=a\nb#section\r',
38 '/a\u0000b?q=a\u0001b#section\u007f',
39 '/',
40 '/posts/123?sort=new#comments',
41 '/caf%C3%A9?q=%2F#//section',
42 '/a//b?q=//value#\\/section',
43 '/%2Fevil.example/path',
44 ])('normalizes href %j like browser history', (href) => {
45 const history = createServerHistory('/request')
46 const browser = createBrowserHistory()
47 try {
48 expect(history.createHref(href)).toBe(browser.createHref(href))
49 expect(history.location.href).toBe('/request')
50 } finally {
51 browser.destroy()
52 }
53 })
54​
55 test('ignores navigation, subscriptions, and blockers', () => {
56 const history = createServerHistory('/request?q=1')
57 const location = history.location
58 const subscriber = vi.fn()
59 const blockerFn = vi.fn(() => true)
60 const unsubscribe = history.subscribe(subscriber)
61 const unblock = history.block({ blockerFn })
62​
63 history.push('/pushed', { value: 1 })
64 history.replace('/replaced', { value: 2 })
65 history.go(-1)
66 history.back()
67 history.forward()
68 history.notify({ type: 'PUSH' })
69 history.flush()
70 history.destroy()
71 unsubscribe()
72 unblock()
73​
74 expect(history.location).toBe(location)
75 expect(history.length).toBe(1)
76 expect(history.canGoBack()).toBe(false)
77 expect(history.subscribers.size).toBe(0)
78 expect(history._getBlockers()).toEqual([])
79 expect(subscriber).not.toHaveBeenCalled()
80 expect(blockerFn).not.toHaveBeenCalled()
81 })
82​
83 test('isolates state and exposed collections between requests', () => {
84 const first = createServerHistory('/first')
85 const second = createServerHistory('/second')
86 first.location.state.__TSR_index = 10
87 first.subscribers.add(vi.fn())
88 first._getBlockers().push({ blockerFn: () => true })
89​
90 expect(second.location.pathname).toBe('/second')
91 expect(second.location.state.__TSR_index).toBe(0)
92 expect(second.subscribers.size).toBe(0)
93 expect(second._getBlockers()).toEqual([])
94 })
95})