-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
/
Copy pathrouterGenerator.spec.js
138 lines (121 loc) · 4.05 KB
/
routerGenerator.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
const generateWithPlugin = require('@vue/cli-test-utils/generateWithPlugin')
test('base', async () => {
const { files, pkg } = await generateWithPlugin({
id: 'router',
apply: require('../generator'),
options: {}
})
expect(files['src/router/index.js']).toBeTruthy()
expect(files['src/router/index.js']).not.toMatch('history')
expect(files['src/views/About.vue']).toBeTruthy()
expect(files['src/views/Home.vue']).toBeTruthy()
expect(files['src/App.vue']).toMatch('<router-link to="/">Home</router-link>')
expect(files['src/App.vue']).not.toMatch('<script>')
expect(files['src/App.vue']).toMatch('#nav a.router-link-exact-active')
expect(pkg.dependencies).toHaveProperty('vue-router')
})
test('history mode', async () => {
const { files, pkg } = await generateWithPlugin({
id: 'router',
apply: require('../generator'),
options: {
historyMode: true
}
})
expect(files['src/router/index.js']).toBeTruthy()
expect(files['src/router/index.js']).toMatch('history')
expect(files['src/views/About.vue']).toBeTruthy()
expect(files['src/views/Home.vue']).toBeTruthy()
expect(files['src/App.vue']).toMatch('<router-link to="/">Home</router-link>')
expect(files['src/App.vue']).not.toMatch('<script>')
expect(files['src/App.vue']).toMatch('#nav a.router-link-exact-active')
expect(pkg.dependencies).toHaveProperty('vue-router')
})
test('use with Babel', async () => {
const { pkg, files } = await generateWithPlugin([
{
id: 'babel',
apply: require('@vue/cli-plugin-babel/generator'),
options: {}
},
{
id: 'router',
apply: require('../generator'),
options: {}
}
])
expect(files['src/router/index.js']).toBeTruthy()
expect(files['src/router/index.js']).toMatch('component: () => import')
expect(files['src/views/About.vue']).toBeTruthy()
expect(files['src/views/Home.vue']).toBeTruthy()
expect(files['src/App.vue']).toMatch('<router-link to="/">Home</router-link>')
expect(files['src/App.vue']).not.toMatch('<script>')
expect(files['src/App.vue']).toMatch('#nav a.router-link-exact-active')
expect(pkg.dependencies).toHaveProperty('vue-router')
})
test('use with Vue 3', async () => {
const { files, pkg } = await generateWithPlugin([
{
id: '@vue/cli-service',
apply: require('@vue/cli-service/generator'),
options: {
vueVersion: '3'
}
},
{
id: '@vue/cli-plugin-router',
apply: require('../generator'),
options: {}
}
])
expect(files['src/router/index.js']).toBeTruthy()
expect(files['src/router/index.js']).toMatch('createRouter')
expect(files['src/router/index.js']).toMatch('history: createWebHashHistory()')
expect(files['src/main.js']).toMatch('.use(router)')
expect(pkg.dependencies).toHaveProperty('vue-router')
expect(pkg.dependencies['vue-router']).toMatch('^4')
})
test('Vue 3 + History Mode', async () => {
const { files } = await generateWithPlugin([
{
id: '@vue/cli-service',
apply: require('@vue/cli-service/generator'),
options: {
vueVersion: '3'
}
},
{
id: '@vue/cli-plugin-router',
apply: require('../generator'),
options: {
historyMode: true
}
}
])
expect(files['src/router/index.js']).toMatch(/import {.*createWebHistory/)
expect(files['src/router/index.js']).toMatch('history: createWebHistory(process.env.BASE_URL)')
})
test('Vue 3 + TypeScript', async () => {
const { files } = await generateWithPlugin([
{
id: '@vue/cli-service',
apply: require('@vue/cli-service/generator'),
options: {
vueVersion: '3'
}
},
{
id: '@vue/cli-plugin-router',
apply: require('../generator'),
options: {}
},
{
id: '@vue/cli-plugin-typescript',
apply: require('@vue/cli-plugin-typescript/generator'),
options: {}
}
])
expect(files['src/router/index.ts']).toBeTruthy()
expect(files['src/router/index.ts']).toMatch(/import {.*RouteRecordRaw/)
expect(files['src/router/index.ts']).toMatch('const routes: Array<RouteRecordRaw> =')
})