forked from posva/vue-router-mock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnavigations.spec.ts
211 lines (181 loc) · 6.04 KB
/
navigations.spec.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import { mount } from '@vue/test-utils'
import { NavigationFailureType } from 'vue-router'
import {
injectRouterMock,
createRouterMock,
EmptyView,
getRouter,
} from '../src'
import Test from './fixtures/Test'
describe('Navigations', () => {
it('can check calls on push', async () => {
const wrapper = mount(Test)
wrapper.vm.$router.push('/hey')
expect(wrapper.vm.$router.push).toHaveBeenCalledWith('/hey')
expect(wrapper.vm.$router.push).toHaveBeenCalledTimes(1)
})
it('can check calls on replace', async () => {
const wrapper = mount(Test)
wrapper.vm.$router.replace('/hey')
expect(wrapper.vm.$router.replace).toHaveBeenCalledWith('/hey')
expect(wrapper.vm.$router.replace).toHaveBeenCalledTimes(1)
})
it('reset calls between tests', async () => {
const wrapper = mount(Test)
expect(wrapper.vm.$router.push).toHaveBeenCalledTimes(0)
})
it('rejects next navigation with an error', async () => {
const wrapper = mount(Test)
const router = getRouter()
const error = new Error('fail')
router.setNextGuardReturn(error)
await expect(wrapper.vm.$router.push('/foo')).rejects.toBe(error)
})
it('can abort the next navigation', async () => {
const wrapper = mount(Test)
const router = getRouter()
router.setNextGuardReturn(false)
await expect(wrapper.vm.$router.push('/foo')).resolves.toMatchObject({
type: NavigationFailureType.aborted,
})
})
describe('per-router guards', () => {
it('ignored by default', async () => {
const beforeEnter = jest.fn()
const router = createRouterMock()
injectRouterMock(router)
router.addRoute({ path: '/foo', beforeEnter, component: EmptyView })
mount(Test)
await router.push('/foo')
expect(beforeEnter).not.toHaveBeenCalled()
})
it('ignore them with other setNextGuardReturn', async () => {
const beforeEnter = jest.fn()
const router = createRouterMock()
injectRouterMock(router)
router.addRoute({ path: '/foo', beforeEnter, component: EmptyView })
mount(Test)
router.setNextGuardReturn(true)
await router.push('/foo')
expect(beforeEnter).not.toHaveBeenCalled()
})
})
describe('in-component guards', () => {
async function factory(
options: Parameters<typeof createRouterMock>[0] = {}
) {
const leaveGuard = jest.fn()
const updateGuard = jest.fn()
const beforeRouteEnter = jest.fn()
const beforeRouteUpdate = jest.fn()
const beforeRouteLeave = jest.fn()
const RouteComponent = {
...Test,
beforeRouteEnter,
beforeRouteUpdate,
beforeRouteLeave,
}
const router = createRouterMock(options)
injectRouterMock(router)
router.addRoute({ path: '/test', component: RouteComponent })
await router.push('/test')
const wrapper = mount(RouteComponent, {
props: { leaveGuard, updateGuard },
})
return {
router,
wrapper,
leaveGuard,
updateGuard,
beforeRouteEnter,
beforeRouteUpdate,
beforeRouteLeave,
}
}
it('ignores guards by default with no guard', async () => {
const {
router,
leaveGuard,
updateGuard,
beforeRouteEnter,
beforeRouteUpdate,
beforeRouteLeave,
} = await factory()
await router.push('/test#two')
expect(beforeRouteEnter).not.toHaveBeenCalled()
expect(updateGuard).not.toHaveBeenCalled()
expect(beforeRouteUpdate).not.toHaveBeenCalled()
await router.push('/foo')
expect(leaveGuard).not.toHaveBeenCalled()
expect(beforeRouteLeave).not.toHaveBeenCalled()
})
it('ignores guards by default with a guard', async () => {
const {
router,
leaveGuard,
updateGuard,
beforeRouteEnter,
beforeRouteUpdate,
beforeRouteLeave,
} = await factory()
router.setNextGuardReturn(true)
await router.push('/test#two')
expect(beforeRouteEnter).not.toHaveBeenCalled()
expect(updateGuard).not.toHaveBeenCalled()
expect(beforeRouteUpdate).not.toHaveBeenCalled()
router.setNextGuardReturn(true)
await router.push('/foo')
expect(leaveGuard).not.toHaveBeenCalled()
expect(beforeRouteLeave).not.toHaveBeenCalled()
})
it('runs guards without a guard return set', async () => {
const {
router,
wrapper,
leaveGuard,
updateGuard,
beforeRouteEnter,
beforeRouteUpdate,
beforeRouteLeave,
} = await factory({
runInComponentGuards: true,
})
expect(beforeRouteEnter).toHaveBeenCalled()
await router.push('/test#two')
expect(updateGuard).toHaveBeenCalled()
expect(beforeRouteUpdate).toHaveBeenCalled()
expect(beforeRouteUpdate.mock.instances[0]).toBe(wrapper.vm)
await router.push('/foo')
expect(leaveGuard).toHaveBeenCalled()
expect(beforeRouteLeave).toHaveBeenCalled()
expect(beforeRouteLeave.mock.instances[0]).toBe(wrapper.vm)
})
it('runs guards with a guard', async () => {
const { router, leaveGuard, updateGuard } = await factory({
runInComponentGuards: true,
})
router.setNextGuardReturn(true)
await router.push('/test#two')
expect(updateGuard).toHaveBeenCalled()
router.setNextGuardReturn(true)
await router.push('/foo')
expect(leaveGuard).toHaveBeenCalled()
})
})
it('can redirect the next navigation', async () => {
const wrapper = mount(Test)
const router = getRouter()
router.setNextGuardReturn('/bar')
await expect(wrapper.vm.$router.push('/foo')).resolves.toBe(undefined)
expect(wrapper.text()).toBe('/bar')
})
it('can wait for an ongoing navigation', async () => {
const wrapper = mount(Test)
const router = getRouter()
// to force async navigation
router.setNextGuardReturn('/bar')
wrapper.vm.$router.push('/foo')
await expect(router.getPendingNavigation()).resolves.toBe(undefined)
expect(wrapper.text()).toBe('/bar')
})
})