-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathpage.test.js
149 lines (130 loc) · 4.08 KB
/
page.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
globalThis.__dirname = __dirname
vi.mock('fs-extra')
vi.mock('../../../../lib', async (importOriginal) => {
const originalLib = await importOriginal()
return {
...originalLib,
generateTemplate: () => '',
}
})
import fs from 'fs-extra'
import { vol } from 'memfs'
import { vi, beforeEach, afterEach, test, expect } from 'vitest'
import '../../../../lib/mockTelemetry'
vi.mock('@redwoodjs/project-config', async (importOriginal) => {
const path = require('path')
const originalProjectConfig = await importOriginal()
return {
getPaths: () => {
const BASE_PATH = '/path/to/project'
return {
base: BASE_PATH,
web: {
generators: path.join(BASE_PATH, './web/generators'),
routes: path.join(BASE_PATH, 'web/src/Routes.js'),
pages: path.join(BASE_PATH, '/web/src/pages'),
},
}
},
getConfig: () => ({}),
findUp: originalProjectConfig.findUp,
ensurePosixPath: originalProjectConfig.ensurePosixPath,
resolveFile: originalProjectConfig.resolveFile,
}
})
vi.mock('@redwoodjs/cli-helpers', async (importOriginal) => {
const originalCliHelpers = await importOriginal()
return {
...originalCliHelpers,
isTypeScriptProject: () => false,
}
})
vi.mock('@redwoodjs/internal/dist/generate/generate', () => {
return {
generate: () => {
return { errors: [] }
},
}
})
import { getPaths } from '../../../../lib/index.js'
import { files } from '../../../generate/page/page.js'
import { tasks } from '../page.js'
beforeEach(async () => {
const f = await files({ name: 'About' })
vol.fromJSON({
...f,
[getPaths().web.routes]: [
'<Routes>',
' <Route path="/about" page={AboutPage} name="about" />',
' <Route path="/" page={HomePage} name="home" />',
' <Route notfound page={NotFoundPage} />',
'</Routes>',
].join('\n'),
})
})
afterEach(() => {
vol.reset()
vi.spyOn(fs, 'unlinkSync').mockClear()
})
test('destroys page files', async () => {
const unlinkSpy = vi.spyOn(fs, 'unlinkSync')
const t = tasks({ name: 'About' })
t.options.renderer = 'silent'
return t.tasks[0].run().then(() => {
const generatedFiles = Object.keys(files({ name: 'About' }))
expect(generatedFiles.length).toEqual(unlinkSpy.mock.calls.length)
generatedFiles.forEach((f) => expect(unlinkSpy).toHaveBeenCalledWith(f))
})
})
test('destroys page files with stories and tests', async () => {
const fileOptions = { name: 'About', stories: true, tests: true }
const f = await files(fileOptions)
vol.fromJSON({
...f,
[getPaths().web.routes]: [
'<Routes>',
' <Route path="/about" page={AboutPage} name="about" />',
' <Route path="/" page={HomePage} name="home" />',
' <Route notfound page={NotFoundPage} />',
'</Routes>',
].join('\n'),
})
const unlinkSpy = vi.spyOn(fs, 'unlinkSync')
const t = tasks(fileOptions)
t.options.renderer = 'silent'
return t.tasks[0].run().then(() => {
const generatedFiles = Object.keys(files(fileOptions))
expect(generatedFiles.length).toEqual(unlinkSpy.mock.calls.length)
generatedFiles.forEach((f) => expect(unlinkSpy).toHaveBeenCalledWith(f))
})
})
test('cleans up route from Routes.js', async () => {
const t = tasks({ name: 'About' })
t.options.renderer = 'silent'
return t.tasks[1].run().then(() => {
const routes = fs.readFileSync(getPaths().web.routes, 'utf-8')
expect(routes).toEqual(
[
'<Routes>',
' <Route path="/" page={HomePage} name="home" />',
' <Route notfound page={NotFoundPage} />',
'</Routes>',
].join('\n'),
)
})
})
test('cleans up route with a custom path from Routes.js', async () => {
const t = tasks({ name: 'About', path: '/about-us' })
t.options.renderer = 'silent'
return t.tasks[1].run().then(() => {
const routes = fs.readFileSync(getPaths().web.routes, 'utf-8')
expect(routes).toEqual(
[
'<Routes>',
' <Route path="/" page={HomePage} name="home" />',
' <Route notfound page={NotFoundPage} />',
'</Routes>',
].join('\n'),
)
})
})