forked from vuejs/router
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.ts
171 lines (157 loc) · 3.81 KB
/
utils.ts
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import { JSDOM, ConstructorOptions } from 'jsdom'
import {
NavigationGuard,
RouteRecordMultipleViews,
MatcherLocation,
RouteLocationNormalized,
_RouteRecordBase,
RouteComponent,
RouteRecordRaw,
RouteRecordName,
_RouteRecordProps,
} from '../src/types'
import { h, ComponentOptions } from 'vue'
import {
RouterOptions,
createWebHistory,
createRouter,
Router,
RouterView,
} from '../src'
export const tick = (time?: number) =>
new Promise(resolve => {
if (time) setTimeout(resolve, time)
else process.nextTick(resolve)
})
export async function ticks(n: number) {
for (let i = 0; i < n; i++) {
await tick()
}
}
export const delay = (t: number) => new Promise(r => setTimeout(r, t))
export function nextNavigation(router: Router) {
return new Promise((resolve, reject) => {
let removeAfter = router.afterEach((_to, _from, failure) => {
removeAfter()
removeError()
resolve(failure)
})
let removeError = router.onError(err => {
removeAfter()
removeError()
reject(err)
})
})
}
export interface RouteRecordViewLoose
extends Pick<
RouteRecordMultipleViews,
'path' | 'name' | 'components' | 'children' | 'meta' | 'beforeEnter'
> {
leaveGuards?: any
instances: Record<string, any>
enterCallbacks: Record<string, Function[]>
props: Record<string, _RouteRecordProps>
aliasOf: RouteRecordViewLoose | undefined
}
// @ts-expect-error we are intentionally overriding the type
export interface RouteLocationNormalizedLoose extends RouteLocationNormalized {
name: RouteRecordName | null | undefined
path: string
// record?
params: any
redirectedFrom?: Partial<MatcherLocation>
meta: any
matched: Partial<RouteRecordViewLoose>[]
}
export interface MatcherLocationNormalizedLoose {
name: string
path: string
// record?
params: any
redirectedFrom?: Partial<MatcherLocation>
meta: any
matched: Partial<RouteRecordViewLoose>[]
instances: Record<string, any>
}
declare global {
namespace NodeJS {
interface Global {
window: JSDOM['window']
location: JSDOM['window']['location']
history: JSDOM['window']['history']
document: JSDOM['window']['document']
before?: Function
}
}
}
export function createDom(options?: ConstructorOptions) {
const dom = new JSDOM(
`<!DOCTYPE html><html><head></head><body></body></html>`,
{
url: 'https://example.com/',
referrer: 'https://example.com/',
contentType: 'text/html',
...options,
}
)
// @ts-expect-error: needed for jsdom
global.window = dom.window
global.location = dom.window.location
global.history = dom.window.history
global.document = dom.window.document
return dom
}
export const noGuard: NavigationGuard = (to, from, next) => {
next()
}
export const components = {
Home: { render: () => h('div', {}, 'Home') },
Foo: { render: () => h('div', {}, 'Foo') },
Bar: { render: () => h('div', {}, 'Bar') },
User: {
props: {
id: {
default: 'default',
},
},
render() {
return h('div', {}, 'User: ' + this.id)
},
} as ComponentOptions,
WithProps: {
props: {
id: {
default: 'default',
},
other: {
default: 'other',
},
},
render() {
return h('div', {}, `id:${this.id};other:${this.other}`)
},
} as RouteComponent,
Nested: {
render: () => {
return h('div', {}, [
h('h2', {}, 'Nested'),
RouterView ? h(RouterView) : [],
])
},
},
BeforeLeave: {
render: () => h('div', {}, 'before leave'),
beforeRouteLeave(to, from, next) {
next()
},
} as RouteComponent,
}
export function newRouter(
options: Partial<RouterOptions> & { routes: RouteRecordRaw[] }
) {
return createRouter({
history: options.history || createWebHistory(),
...options,
})
}