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

createHashHistory.test.ts

44 lines1.4 KB

1import { describe, expect, test } from 'vitest'
2​
3import { createHashHistory } from '../src'
4​
5describe('createHashHistory', () => {
6 describe('parseLocation', () => {
7 describe.each([
8 ['/', { pathname: '/', search: '' }, 'neither search params nor hash'],
9 [
10 '/#/hello',
11 { pathname: '/hello', search: '' },
12 'hash present, no search params',
13 ],
14 [
15 '/?search=params',
16 { pathname: '/', search: '?search=params' },
17 'search params present, no hash',
18 ],
19 [
20 '/#/hello?search=params',
21 { pathname: '/hello', search: '?search=params' },
22 'both hash and search params present, in that order',
23 ],
24 [
25 '/?search=params#/hello',
26 { pathname: '/hello', search: '?search=params' },
27 'both search params and hash present, in that order',
28 ],
29 ])('check for %s', (...[path, exp, desc]) => {
30 test(`onLoad with ${path} (${desc})`, () => {
31 window.history.replaceState({}, '', path)
32 const history = createHashHistory()
33 expect(history.location.pathname).toBe(exp.pathname)
34 expect(history.location.search).toBe(exp.search)
35 })
36 test(`onNavigate with ${path} (${desc})`, () => {
37 const history = createHashHistory()
38 window.history.pushState({}, '', path)
39 expect(history.location.pathname).toBe(exp.pathname)
40 expect(history.location.search).toBe(exp.search)
41 })
42 })
43 })
44})