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

createMemoryHistory.test.ts

79 lines2.4 KB

1import { describe, expect, test } from 'vitest'
2import { createMemoryHistory } from '../src'
3​
4describe('createMemoryHistory', () => {
5 test('back', () => {
6 const initialEntry = '/initial'
7 const history = createMemoryHistory({ initialEntries: [initialEntry] })
8 history.push('/a')
9 history.push('/b')
10 history.push('/c')
11 history.back()
12 expect(history.location.pathname).toBe('/b')
13 history.back()
14 expect(history.location.pathname).toBe('/a')
15 history.back()
16 expect(history.location.pathname).toBe(initialEntry)
17 // check that back does nothing when there is no back history
18 history.back()
19 expect(history.location.pathname).toBe(initialEntry)
20 })
21​
22 test('forward', () => {
23 const history = createMemoryHistory()
24 history.push('/a')
25 history.push('/b')
26 history.push('/c')
27 history.back()
28 history.back()
29 expect(history.location.pathname).toBe('/a')
30 history.forward()
31 expect(history.location.pathname).toBe('/b')
32 history.forward()
33 expect(history.location.pathname).toBe('/c')
34 // check that forward does nothing when there is no forward history
35 history.forward()
36 expect(history.location.pathname).toBe('/c')
37 })
38​
39 test('push and back #1916', () => {
40 const history = createMemoryHistory()
41 history.push('/a')
42 expect(history.location.pathname).toBe('/a')
43 history.push('/b')
44 expect(history.location.pathname).toBe('/b')
45 history.push('/c')
46 expect(history.location.pathname).toBe('/c')
47 history.back()
48 expect(history.location.pathname).toBe('/b')
49 history.push('/d')
50 expect(history.location.pathname).toBe('/d')
51 history.back()
52 expect(history.location.pathname).toBe('/b')
53 })
54​
55 test('length', () => {
56 const history = createMemoryHistory()
57 expect(history.length).toBe(1)
58 history.push('/a')
59 expect(history.length).toBe(2)
60 history.replace('/b')
61 expect(history.length).toBe(2)
62 history.back()
63 expect(history.length).toBe(2)
64 history.push('/c')
65 expect(history.length).toBe(2)
66 })
67​
68 test('state', () => {
69 const history = createMemoryHistory()
70 history.push('/a', { i: 1 })
71 expect((history.location.state as any).i).toBe(1)
72 history.replace('/b', { i: 2 })
73 expect((history.location.state as any).i).toBe(2)
74 history.back()
75 expect((history.location.state as any).i).toBeUndefined()
76 history.push('/c', { i: 3 })
77 expect((history.location.state as any).i).toBe(3)
78 })
79})