forked from redwoodjs/redwood
/
page.test.js
104 lines (90 loc) · 2.88 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
global.__dirname = __dirname
jest.mock('fs')
jest.mock('../../../../lib', () => {
return {
...jest.requireActual('../../../../lib'),
generateTemplate: () => '',
}
})
import fs from 'fs'
import '../../../../lib/test'
import { getPaths } from '../../../../lib'
import { files } from '../../../generate/page/page'
import { tasks } from '../page'
beforeEach(() => {
fs.__setMockFiles({
...files({ name: 'About' }),
[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(() => {
fs.__setMockFiles({})
jest.spyOn(fs, 'unlinkSync').mockClear()
})
test('destroys page files', async () => {
const unlinkSpy = jest.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 }
fs.__setMockFiles({
...files(fileOptions),
[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 = jest.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)
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)
expect(routes).toEqual(
[
'<Routes>',
' <Route path="/" page={HomePage} name="home" />',
' <Route notfound page={NotFoundPage} />',
'</Routes>',
].join('\n')
)
})
})