/
generator.init.test.js
242 lines (202 loc) · 8.09 KB
/
generator.init.test.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import consola from 'consola'
import fsExtra from 'fs-extra'
import { flatRoutes, isString, isUrl, promisifyRoute } from '@nuxt/utils'
import Generator from '../src/generator'
import { createNuxt, hookCalls } from './__utils__'
jest.mock('fs-extra')
jest.mock('@nuxt/utils')
describe('generator: initialize', () => {
beforeAll(() => {
isString.mockImplementation(str => typeof str === 'string')
})
beforeEach(() => {
jest.clearAllMocks()
})
test('should construct Generator', () => {
isUrl.mockReturnValueOnce(true)
const nuxt = createNuxt()
nuxt.options = {
...nuxt.options,
build: { publicPath: 'http://localhost:3000' }
}
const builder = jest.fn()
const generator = new Generator(nuxt, builder)
expect(generator.nuxt).toBe(nuxt)
expect(generator.options).toBe(nuxt.options)
expect(generator.builder).toBe(builder)
expect(generator.staticRoutes).toBePath(
'/var/nuxt/static',
'C:\\nuxt\\static'
)
expect(generator.srcBuiltPath).toBePath(
'/var/nuxt/build/dist/client',
'C:\\nuxt\\build\\dist\\client'
)
expect(generator.distPath).toBePath(
'/var/nuxt/generate',
'C:\\nuxt\\generate'
)
expect(generator.distNuxtPath).toBePath(
'/var/nuxt/generate',
'C:\\nuxt\\generate'
)
})
test('should append publicPath to distPath if publicPath is not url', () => {
isUrl.mockReturnValueOnce(false)
const nuxt = createNuxt()
nuxt.options = {
...nuxt.options,
build: { publicPath: '__public' }
}
const builder = jest.fn()
const generator = new Generator(nuxt, builder)
expect(generator.distNuxtPath).toBePath(
'/var/nuxt/generate/__public',
'C:\\nuxt\\generate\\__public'
)
})
test('should initiate with build and init by default', async () => {
const nuxt = createNuxt()
const builder = { forGenerate: jest.fn(), build: jest.fn() }
const generator = new Generator(nuxt, builder)
generator.initDist = jest.fn()
await generator.initiate()
expect(nuxt.ready).toBeCalledTimes(1)
expect(hookCalls(nuxt, 'generate:before')[0]).toMatchObject([generator, { dir: generator.distPath }])
expect(builder.forGenerate).toBeCalledTimes(1)
expect(builder.build).toBeCalledTimes(1)
expect(generator.initDist).toBeCalledTimes(1)
})
test('should initiate without build and init if disabled', async () => {
const nuxt = createNuxt()
const builder = { forGenerate: jest.fn(), build: jest.fn() }
const generator = new Generator(nuxt, builder)
generator.initDist = jest.fn()
fsExtra.exists.mockReturnValueOnce(true)
generator.getBuildConfig = jest.fn(() => ({ ssr: true, target: 'static' }))
await generator.initiate({ build: false, init: false })
expect(nuxt.ready).toBeCalledTimes(1)
expect(hookCalls(nuxt, 'generate:before')[0]).toMatchObject([generator, { dir: generator.distPath }])
expect(builder.forGenerate).not.toBeCalled()
expect(builder.build).not.toBeCalled()
expect(generator.initDist).not.toBeCalled()
})
test('should throw error when build is not disabled, but Builder instance is omitted', async () => {
const nuxt = createNuxt()
const generator = new Generator(nuxt)
await expect(generator.initiate()).rejects.toThrow('Could not generate')
})
test('should init routes with generate.routes and routes.json', async () => {
const nuxt = createNuxt()
nuxt.options = {
...nuxt.options,
generate: {
...nuxt.options.generate,
exclude: [/test/],
routes: ['/foo', '/foo/bar']
},
router: {
mode: 'history'
}
}
const generator = new Generator(nuxt)
flatRoutes.mockImplementationOnce(routes => routes)
promisifyRoute.mockImplementationOnce(routes => routes)
generator.getAppRoutes = jest.fn(() => ['/index', '/about', '/test'])
generator.decorateWithPayloads = jest.fn(() => 'decoratedRoutes')
const routes = await generator.initRoutes()
expect(promisifyRoute).toBeCalledTimes(1)
expect(promisifyRoute).toBeCalledWith(['/foo', '/foo/bar'])
expect(flatRoutes).toBeCalledTimes(1)
expect(flatRoutes).toBeCalledWith(['/index', '/about', '/test'])
expect(generator.decorateWithPayloads).toBeCalledTimes(1)
expect(generator.decorateWithPayloads).toBeCalledWith(['/index', '/about'], ['/foo', '/foo/bar'])
expect(hookCalls(nuxt, 'generate:extendRoutes')[0][0]).toBe('decoratedRoutes')
expect(routes).toEqual('decoratedRoutes')
})
test('should init routes with hash mode', async () => {
const nuxt = createNuxt()
nuxt.options = {
...nuxt.options,
generate: {
...nuxt.options.generate,
exclude: [/test/],
routes: ['/foo', '/foo/bar']
},
router: {
mode: 'hash'
}
}
const generator = new Generator(nuxt)
flatRoutes.mockImplementationOnce(routes => routes)
promisifyRoute.mockImplementationOnce(routes => routes)
generator.decorateWithPayloads = jest.fn(() => 'decoratedRoutes')
const routes = await generator.initRoutes()
expect(promisifyRoute).not.toBeCalled()
expect(flatRoutes).not.toBeCalled()
expect(generator.decorateWithPayloads).toBeCalledTimes(1)
expect(generator.decorateWithPayloads).toBeCalledWith(['/'], [])
expect(hookCalls(nuxt, 'generate:extendRoutes')[0][0]).toBe('decoratedRoutes')
expect(routes).toEqual('decoratedRoutes')
promisifyRoute.mockReset()
flatRoutes.mockReset()
})
test('should throw error when route can not be resolved', async () => {
const nuxt = createNuxt()
nuxt.options.router = { mode: 'history' }
const generator = new Generator(nuxt)
promisifyRoute.mockImplementationOnce(() => {
throw new Error('promisifyRoute failed')
})
await expect(generator.initRoutes()).rejects.toThrow('promisifyRoute failed')
expect(promisifyRoute).toBeCalledTimes(1)
expect(promisifyRoute).toBeCalledWith([])
expect(consola.error).toBeCalledTimes(1)
expect(consola.error).toBeCalledWith('Could not resolve routes')
})
test('should initialize destination folder', async () => {
const nuxt = createNuxt()
nuxt.options.generate.fallback = 'fallback.html'
nuxt.options.generate.nojekyll = true
const generator = new Generator(nuxt)
fsExtra.exists.mockReturnValueOnce(false)
await generator.initDist()
expect(fsExtra.emptyDir).toBeCalledTimes(1)
expect(fsExtra.emptyDir).toBeCalledWith(generator.distPath)
expect(hookCalls(nuxt, 'generate:distRemoved')[0][0]).toMatchObject(generator)
expect(fsExtra.exists).toBeCalledTimes(1)
expect(fsExtra.exists).toBeCalledWith(generator.staticRoutes)
expect(fsExtra.copy).toBeCalledTimes(1)
expect(fsExtra.copy).toBeCalledWith(generator.srcBuiltPath, generator.distNuxtPath)
expect(fsExtra.writeFile).toBeCalledTimes(1)
expect(fsExtra.writeFile.mock.calls[0][0]).toBePath(
'/var/nuxt/generate/.nojekyll',
'C:\\nuxt\\generate\\.nojekyll'
)
expect(hookCalls(nuxt, 'generate:distCopied')[0][0]).toMatchObject(generator)
})
test('should copy static routes if path exists', async () => {
const nuxt = createNuxt()
nuxt.options.generate.fallback = 'fallback.html'
const generator = new Generator(nuxt)
fsExtra.exists.mockReturnValueOnce(true)
await generator.initDist()
expect(fsExtra.copy).toBeCalledTimes(2)
expect(fsExtra.copy).nthCalledWith(1, generator.staticRoutes, generator.distPath)
expect(fsExtra.copy).nthCalledWith(2, generator.srcBuiltPath, generator.distNuxtPath)
})
test('should decorate routes with payloads', () => {
const nuxt = createNuxt()
const generator = new Generator(nuxt)
const routes = ['/index', '/about', '/test']
const generateRoutes = ['/foo', { route: '/foo/bar', payload: { param: 'foo bar' } }]
const routeMap = generator.decorateWithPayloads(routes, generateRoutes)
expect(routeMap).toEqual([
{ payload: null, route: '/index' },
{ payload: null, route: '/about' },
{ payload: null, route: '/test' },
{ payload: null, route: '/foo' },
{ payload: { param: 'foo bar' }, route: '/foo/bar' }
])
})
})