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

createHashHistory.test.ts

198 lines5.5 KB

1import { describe, expect, test, vi } from 'vitest'
2​
3import { createHashHistory } from '../src'
4​
5describe('createHashHistory', () => {
6 test.each(['\x00/route', '/route\x01', '/route?q=a\x1fb#c\x7fd'])(
7 'keeps logical control data stable after reading the browser URL again: %j',
8 (href) => {
9 const originalHref = window.location.href
10 window.history.replaceState(null, '', '/shell')
11 const history = createHashHistory()
12 let reloaded: ReturnType<typeof createHashHistory> | undefined
13 try {
14 history.push(href)
15 history.flush()
16 const location = history.location
17 history.destroy()
18 reloaded = createHashHistory()
19 expect(reloaded.location).toEqual(location)
20 } finally {
21 history.destroy()
22 reloaded?.destroy()
23 window.history.replaceState(null, '', originalHref)
24 }
25 },
26 )
27​
28 test('reads a hash route that starts with a query without leaking it into the pathname', () => {
29 const window = {
30 location: { pathname: '/', search: '', hash: '#?x=1' },
31 history: {
32 state: { __TSR_index: 0, __TSR_key: 'initial' },
33 length: 1,
34 pushState: vi.fn(),
35 replaceState: vi.fn(),
36 back: vi.fn(),
37 forward: vi.fn(),
38 go: vi.fn(),
39 },
40 addEventListener: vi.fn(),
41 removeEventListener: vi.fn(),
42 }
43 const history = createHashHistory({ window })
44​
45 expect(history.location).toMatchObject({
46 href: '?x=1',
47 pathname: '',
48 search: '?x=1',
49 hash: '',
50 })
51 history.destroy()
52 })
53​
54 test('normalizes the final browser href without changing the logical hash location', async () => {
55 const pushState = vi.fn()
56 const window = {
57 location: {
58 pathname: '//nested/path',
59 search: '',
60 hash: '',
61 },
62 history: {
63 state: { __TSR_index: 0, __TSR_key: 'initial' },
64 length: 1,
65 pushState,
66 replaceState: vi.fn(),
67 back: vi.fn(),
68 forward: vi.fn(),
69 go: vi.fn(),
70 },
71 addEventListener: vi.fn(),
72 removeEventListener: vi.fn(),
73 }
74 const history = createHashHistory({ window })
75​
76 history.push('/logical')
77 await Promise.resolve()
78​
79 expect(pushState).toHaveBeenCalledWith(
80 expect.anything(),
81 '',
82 '/nested/path#/logical',
83 )
84 expect(history.location.href).toBe('/logical')
85 history.destroy()
86 })
87​
88 test.each([
89 [
90 '/?shell=1#/hello#section#tail',
91 {
92 href: '/hello?shell=1#section#tail',
93 pathname: '/hello',
94 search: '?shell=1',
95 hash: '#section#tail',
96 },
97 ],
98 [
99 '/?shell=1#/hello?route=2#section',
100 {
101 href: '/hello?route=2?shell=1#section',
102 pathname: '/hello',
103 search: '?route=2?shell=1',
104 hash: '#section',
105 },
106 ],
107 [
108 '/#/hello##tail#',
109 {
110 href: '/hello##tail#',
111 pathname: '/hello',
112 search: '',
113 hash: '##tail#',
114 },
115 ],
116 [
117 '/#/hello%23nested?value=%23#anchor%23tail',
118 {
119 href: '/hello%23nested?value=%23#anchor%23tail',
120 pathname: '/hello%23nested',
121 search: '?value=%23',
122 hash: '#anchor%23tail',
123 },
124 ],
125 [
126 '/?shell=1#//evil.example/path#fragment',
127 {
128 href: '/evil.example/path?shell=1#fragment',
129 pathname: '/evil.example/path',
130 search: '?shell=1',
131 hash: '#fragment',
132 },
133 ],
134 [
135 '/?shell=1#',
136 {
137 href: '/?shell=1',
138 pathname: '/',
139 search: '?shell=1',
140 hash: '',
141 },
142 ],
143 ])('preserves the logical URL when reading %s', (href, expected) => {
144 const originalHref = window.location.href
145 const originalState = window.history.state
146 window.history.replaceState(null, '', href)
147 const history = createHashHistory()
148​
149 try {
150 expect(history.location).toMatchObject(expected)
151 window.history.replaceState(window.history.state, '', '/shell')
152 window.history.pushState(window.history.state, '', href)
153 expect(history.location).toMatchObject(expected)
154 } finally {
155 history.destroy()
156 window.history.replaceState(originalState, '', originalHref)
157 }
158 })
159​
160 describe('parseLocation', () => {
161 describe.each([
162 ['/', { pathname: '/', search: '' }, 'neither search params nor hash'],
163 [
164 '/#/hello',
165 { pathname: '/hello', search: '' },
166 'hash present, no search params',
167 ],
168 [
169 '/?search=params',
170 { pathname: '/', search: '?search=params' },
171 'search params present, no hash',
172 ],
173 [
174 '/#/hello?search=params',
175 { pathname: '/hello', search: '?search=params' },
176 'both hash and search params present, in that order',
177 ],
178 [
179 '/?search=params#/hello',
180 { pathname: '/hello', search: '?search=params' },
181 'both search params and hash present, in that order',
182 ],
183 ])('check for %s', (...[path, exp, desc]) => {
184 test(`onLoad with ${path} (${desc})`, () => {
185 window.history.replaceState({}, '', path)
186 const history = createHashHistory()
187 expect(history.location.pathname).toBe(exp.pathname)
188 expect(history.location.search).toBe(exp.search)
189 })
190 test(`onNavigate with ${path} (${desc})`, () => {
191 const history = createHashHistory()
192 window.history.pushState({}, '', path)
193 expect(history.location.pathname).toBe(exp.pathname)
194 expect(history.location.search).toBe(exp.search)
195 })
196 })
197 })
198})