-
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
/
Copy pathapi.spec.js
187 lines (162 loc) · 4.1 KB
/
api.spec.js
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
import Router from '../../../src/index'
describe('router.onReady', () => {
it('should work', done => {
const calls = []
const router = new Router({
mode: 'abstract',
routes: [
{
path: '/a',
component: {
name: 'A',
beforeRouteEnter: (to, from, next) => {
setTimeout(() => {
calls.push(2)
next()
}, 1)
}
}
}
]
})
router.beforeEach((to, from, next) => {
setTimeout(() => {
calls.push(1)
next()
}, 1)
})
router.onReady(() => {
expect(calls).toEqual([1, 2])
// sync call when already ready
router.onReady(() => {
calls.push(3)
})
expect(calls).toEqual([1, 2, 3])
done()
})
router.push('/a')
expect(calls).toEqual([])
})
})
describe('route matching', () => {
it('resolves parent params when using current route', () => {
const router = new Router({
mode: 'abstract',
routes: [
{
path: '/a/:id',
component: { name: 'A' },
children: [{ name: 'b', path: 'b', component: { name: 'B' }}]
}
]
})
router.push('/a/1')
const { route, resolved } = router.resolve({ name: 'b' })
expect(route.params).toEqual({ id: '1' })
expect(resolved.params).toEqual({ id: '1' })
})
it('can override currentRoute', () => {
const router = new Router({
mode: 'abstract',
routes: [
{
path: '/a/:id',
component: { name: 'A' },
children: [{ name: 'b', path: 'b', component: { name: 'B' }}]
}
]
})
router.push('/a/1')
const { route, resolved } = router.resolve({ name: 'b' }, { params: { id: '2' }, path: '/a/2' })
expect(route.params).toEqual({ id: '2' })
expect(resolved.params).toEqual({ id: '2' })
})
})
describe('router.addRoutes', () => {
it('should work', () => {
const router = new Router({
mode: 'abstract',
routes: [
{ path: '/a', component: { name: 'A' }}
]
})
router.push('/a')
let components = router.getMatchedComponents()
expect(components.length).toBe(1)
expect(components[0].name).toBe('A')
router.push('/b')
components = router.getMatchedComponents()
expect(components.length).toBe(0)
router.addRoutes([
{ path: '/b', component: { name: 'B' }}
])
components = router.getMatchedComponents()
expect(components.length).toBe(1)
expect(components[0].name).toBe('B')
// make sure it preserves previous routes
router.push('/a')
components = router.getMatchedComponents()
expect(components.length).toBe(1)
expect(components[0].name).toBe('A')
})
})
describe('router.push/replace callbacks', () => {
let calls = []
let router, spy1, spy2
const Foo = {
beforeRouteEnter (to, from, next) {
calls.push(3)
setTimeout(() => {
calls.push(4)
next()
}, 1)
}
}
beforeEach(() => {
calls = []
spy1 = jasmine.createSpy('complete')
spy2 = jasmine.createSpy('abort')
router = new Router({
routes: [
{ path: '/foo', component: Foo }
]
})
router.beforeEach((to, from, next) => {
calls.push(1)
setTimeout(() => {
calls.push(2)
next()
}, 1)
})
})
it('push complete', done => {
router.push('/foo', () => {
expect(calls).toEqual([1, 2, 3, 4])
done()
})
})
it('push abort', done => {
router.push('/foo', spy1, spy2)
router.push('/bar', () => {
expect(calls).toEqual([1, 1, 2, 2])
expect(spy1).not.toHaveBeenCalled()
expect(spy2).toHaveBeenCalled()
done()
})
})
it('replace complete', done => {
router.replace('/foo', () => {
expect(calls).toEqual([1, 2, 3, 4])
done()
})
})
it('replace abort', done => {
router.replace('/foo', spy1, spy2)
router.replace('/bar', () => {
expect(calls).toEqual([1, 1, 2, 2])
expect(spy1).not.toHaveBeenCalled()
expect(spy2).toHaveBeenCalled()
done()
})
})
})