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

createMemoryHistory.test.ts

213 lines6.6 KB

1import { describe, expect, test, vi } from 'vitest'
2import { createMemoryHistory } from '../src'
3​
4describe('createMemoryHistory', () => {
5 test.each([
6 ['//example.com/path', '/example.com/path'],
7 ['/\\example.com/path', '/example.com/path'],
8 ['\\/example.com/path', '/example.com/path'],
9 ['\\\\example.com/path', '/example.com/path'],
10 [' \t/\r\\example.com/path', '/example.com/path'],
11 ['/\t/example.com/path', '/example.com/path'],
12 ['/a\tb?q=a\nb#section\r', '/ab?q=ab#section'],
13 ['/a\u0000b?q=a\u0001b#section\u007f', '/a%00b?q=a%01b#section%7F'],
14 ['/', '/'],
15 ['/posts/123?sort=new#comments', '/posts/123?sort=new#comments'],
16 ['/caf%C3%A9?q=%2F#//section', '/caf%C3%A9?q=%2F#//section'],
17 ['/a//b?q=//value#\\/section', '/a//b?q=//value#\\/section'],
18 ['/%2Fexample.com/path', '/%2Fexample.com/path'],
19 ])('creates href %j consistent with navigation', (href, expected) => {
20 const history = createMemoryHistory()
21 const location = history.location
22 const createdHref = history.createHref(href)
23​
24 expect(createdHref).toBe(expected)
25 expect(history.location).toBe(location)
26 expect(new URL(createdHref, 'https://app.example').origin).toBe(
27 'https://app.example',
28 )
29​
30 history.push(href)
31 expect(history.location.href).toBe(createdHref)
32 })
33​
34 test('exposes the current blocker registry after registration and removal', () => {
35 const history = createMemoryHistory()
36 const first = { blockerFn: vi.fn(() => true) }
37 const second = { blockerFn: vi.fn(() => false) }
38​
39 expect(history._getBlockers()).toEqual([])
40​
41 const removeFirst = history.block(first)
42 const removeSecond = history.block(second)
43 expect(history._getBlockers()).toEqual([first, second])
44​
45 removeFirst()
46 expect(history._getBlockers()).toEqual([second])
47​
48 removeSecond()
49 expect(history._getBlockers()).toEqual([])
50 })
51​
52 test('back', () => {
53 const initialEntry = '/initial'
54 const history = createMemoryHistory({ initialEntries: [initialEntry] })
55 history.push('/a')
56 history.push('/b')
57 history.push('/c')
58 history.back()
59 expect(history.location.pathname).toBe('/b')
60 history.back()
61 expect(history.location.pathname).toBe('/a')
62 history.back()
63 expect(history.location.pathname).toBe(initialEntry)
64 // check that back does nothing when there is no back history
65 history.back()
66 expect(history.location.pathname).toBe(initialEntry)
67 })
68​
69 test('forward', () => {
70 const history = createMemoryHistory()
71 history.push('/a')
72 history.push('/b')
73 history.push('/c')
74 history.back()
75 history.back()
76 expect(history.location.pathname).toBe('/a')
77 history.forward()
78 expect(history.location.pathname).toBe('/b')
79 history.forward()
80 expect(history.location.pathname).toBe('/c')
81 // check that forward does nothing when there is no forward history
82 history.forward()
83 expect(history.location.pathname).toBe('/c')
84 })
85​
86 test('push and back #1916', () => {
87 const history = createMemoryHistory()
88 history.push('/a')
89 expect(history.location.pathname).toBe('/a')
90 history.push('/b')
91 expect(history.location.pathname).toBe('/b')
92 history.push('/c')
93 expect(history.location.pathname).toBe('/c')
94 history.back()
95 expect(history.location.pathname).toBe('/b')
96 history.push('/d')
97 expect(history.location.pathname).toBe('/d')
98 history.back()
99 expect(history.location.pathname).toBe('/b')
100 })
101​
102 test('discards forward entries and their state when pushing a new branch', () => {
103 const initialEntries = ['/']
104 const history = createMemoryHistory({ initialEntries })
105 history.push('/kept', { marker: 'kept' })
106 const keptState = history.location.state
107 history.push('/discarded-first', { marker: 'discarded-first' })
108 history.push('/discarded-last', { marker: 'discarded-last' })
109 history.go(-2)
110 history.push('/new', { marker: 'new' })
111​
112 expect(initialEntries).toEqual(['/', '/kept', '/new'])
113 expect(history.length).toBe(3)
114 expect(history.location.pathname).toBe('/new')
115 expect(history.location.state).toMatchObject({
116 __TSR_index: 2,
117 marker: 'new',
118 })
119 const newState = history.location.state
120​
121 history.forward()
122 expect(history.location.pathname).toBe('/new')
123 expect(history.location.state).toBe(newState)
124​
125 history.back()
126 expect(history.location.pathname).toBe('/kept')
127 expect(history.location.state).toBe(keptState)
128​
129 history.forward()
130 expect(history.location.pathname).toBe('/new')
131 expect(history.location.state).toBe(newState)
132 })
133​
134 test('length', () => {
135 const history = createMemoryHistory()
136 expect(history.length).toBe(1)
137 history.push('/a')
138 expect(history.length).toBe(2)
139 history.replace('/b')
140 expect(history.length).toBe(2)
141 history.back()
142 expect(history.length).toBe(2)
143 history.push('/c')
144 expect(history.length).toBe(2)
145 })
146​
147 test('state', () => {
148 const history = createMemoryHistory()
149 history.push('/a', { i: 1 })
150 expect((history.location.state as any).i).toBe(1)
151 history.replace('/b', { i: 2 })
152 expect((history.location.state as any).i).toBe(2)
153 history.back()
154 expect((history.location.state as any).i).toBeUndefined()
155 history.push('/c', { i: 3 })
156 expect((history.location.state as any).i).toBe(3)
157 })
158​
159 test('block prevents navigation', async () => {
160 const history = createMemoryHistory({ initialEntries: ['/'] })
161 const blockerFn = vi.fn(() => true) // Always block
162​
163 const unblock = history.block({
164 blockerFn,
165 enableBeforeUnload: false,
166 })
167​
168 await history.push('/a')
169​
170 // Navigation should be blocked
171 expect(history.location.pathname).toBe('/')
172 expect(blockerFn).toHaveBeenCalled()
173​
174 unblock()
175 })
176​
177 test('block allows navigation when blockerFn returns false', async () => {
178 const history = createMemoryHistory({ initialEntries: ['/'] })
179 const blockerFn = vi.fn(() => false) // Never block
180​
181 const unblock = history.block({
182 blockerFn,
183 enableBeforeUnload: false,
184 })
185​
186 await history.push('/a')
187​
188 // Navigation should proceed
189 expect(history.location.pathname).toBe('/a')
190 expect(blockerFn).toHaveBeenCalled()
191​
192 unblock()
193 })
194​
195 test('unblock removes blocker', async () => {
196 const history = createMemoryHistory({ initialEntries: ['/'] })
197 const blockerFn = vi.fn(() => true) // Always block
198​
199 const unblock = history.block({
200 blockerFn,
201 enableBeforeUnload: false,
202 })
203​
204 // Unblock immediately
205 unblock()
206​
207 await history.push('/a')
208​
209 // Navigation should proceed since blocker was removed
210 expect(history.location.pathname).toBe('/a')
211 expect(blockerFn).not.toHaveBeenCalled()
212 })
213})