-
-
Notifications
You must be signed in to change notification settings - Fork 433
Expand file tree
/
Copy pathrouter.options.spec.ts
More file actions
73 lines (61 loc) · 2.1 KB
/
router.options.spec.ts
File metadata and controls
73 lines (61 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import { describe, expect, it } from 'vitest'
import routerOptions from '~/router.options'
type ScrollBehavior = NonNullable<typeof routerOptions.scrollBehavior>
type RouteArg = Parameters<ScrollBehavior>[0]
function createRoute(overrides: Partial<RouteArg> = {}) {
return {
path: '/',
hash: '',
query: {},
meta: {},
...overrides,
} as RouteArg
}
describe('router scrollBehavior', () => {
it('restores saved position when available', () => {
const savedPosition = { left: 12, top: 345 }
expect(routerOptions.scrollBehavior(createRoute(), createRoute(), savedPosition)).toEqual(
savedPosition,
)
})
it('preserves scroll on query-only updates for pages that opt in', () => {
const to = createRoute({
path: '/compare',
query: { packages: 'vue,nuxt', facets: 'downloads,license' },
meta: { preserveScrollOnQuery: true },
})
const from = createRoute({
path: '/compare',
query: { packages: 'vue', facets: 'downloads' },
meta: { preserveScrollOnQuery: true },
})
expect(routerOptions.scrollBehavior(to, from, null)).toBe(false)
})
it('does not preserve scroll on query-only updates without opt-in', () => {
const to = createRoute({
path: '/compare',
query: { packages: 'vue,nuxt', facets: 'downloads,license' },
})
const from = createRoute({
path: '/compare',
query: { packages: 'vue', facets: 'downloads' },
})
expect(routerOptions.scrollBehavior(to, from, null)).toEqual({ left: 0, top: 0 })
})
it('scrolls to hash anchors', () => {
const to = createRoute({
hash: '#section-function',
meta: { scrollMargin: 96 },
})
expect(routerOptions.scrollBehavior(to, createRoute(), null)).toEqual({
el: '#section-function',
behavior: 'smooth',
top: 96,
})
})
it('scrolls to top for regular navigations', () => {
const to = createRoute({ path: '/compare', meta: { preserveScrollOnQuery: true } })
const from = createRoute({ path: '/search' })
expect(routerOptions.scrollBehavior(to, from, null)).toEqual({ left: 0, top: 0 })
})
})