-
-
Notifications
You must be signed in to change notification settings - Fork 166
Expand file tree
/
Copy pathtemplate-file.test.ts
More file actions
219 lines (190 loc) · 6.75 KB
/
template-file.test.ts
File metadata and controls
219 lines (190 loc) · 6.75 KB
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
import { describe, expect, it } from 'vitest'
import { createMemoryEnvironment } from '../src/environment.js'
import { createTemplateFile } from '../src/template-file.js'
import { FILE_ROUTER } from '../src/constants.js'
import type { AddOn, Integration, Options } from '../src/types.js'
const simpleOptions = {
projectName: 'test',
targetDir: '/test',
framework: {
id: 'test',
name: 'Test',
},
chosenAddOns: [],
packageManager: 'pnpm',
typescript: true,
tailwind: true,
mode: FILE_ROUTER,
} as unknown as Options
describe('createTemplateFile', () => {
it('should template a simple file', async () => {
const { environment, output } = createMemoryEnvironment()
const templateFile = createTemplateFile(environment, simpleOptions)
environment.startRun()
await templateFile('test.ts', 'let a = 1')
environment.finishRun()
expect(output.files['/test/test.ts'].trim()).toEqual('let a = 1')
})
it('should handle ignore files', async () => {
const { environment, output } = createMemoryEnvironment()
const templateFile = createTemplateFile(environment, {
...simpleOptions,
} as unknown as Options)
environment.startRun()
await templateFile('test.ts.ejs', '<% ignoreFile() %>let a = 1')
environment.finishRun()
expect(output.files['/test/test.ts']).toBeUndefined()
})
it('should handle append files', async () => {
const { environment, output } = createMemoryEnvironment()
const templateFile = createTemplateFile(environment, simpleOptions)
environment.startRun()
await templateFile('test.txt.ejs', 'Line 1\n')
await templateFile('test.txt.append', 'Line 2\n')
environment.finishRun()
expect(output.files['/test/test.txt']).toEqual('Line 1\n\nLine 2\n')
})
it('should handle enabled add-ons', async () => {
const { environment, output } = createMemoryEnvironment()
const templateFile = createTemplateFile(environment, {
...simpleOptions,
chosenAddOns: [
{
id: 'test1',
name: 'Test 1',
},
{
id: 'test2',
name: 'Test 2',
},
] as Array<AddOn>,
})
environment.startRun()
await templateFile(
'test.txt.ejs',
"Addons: <%= Object.keys(addOnEnabled).join(', ') %>",
)
environment.finishRun()
expect(output.files['/test/test.txt']).toEqual('Addons: test1, test2')
})
it('should handle relative paths', async () => {
const { environment, output } = createMemoryEnvironment()
const templateFile = createTemplateFile(environment, simpleOptions)
environment.startRun()
await templateFile(
'src/test/test.txt.ejs',
"import { foo } from '<%= relativePath('foo.ts') %>'",
)
environment.finishRun()
expect(output.files['/test/src/test/test.txt']).toEqual(
"import { foo } from '../../foo.ts'",
)
})
it('should handle routes', async () => {
const { environment, output } = createMemoryEnvironment()
const templateFile = createTemplateFile(environment, {
...simpleOptions,
chosenAddOns: [
{
id: 'test',
name: 'Test',
routes: [
{
path: '/test',
name: 'Test',
url: '/test',
jsName: 'test',
},
],
} as AddOn,
],
})
environment.startRun()
await templateFile(
'test.txt.ejs',
"<%= routes.map((route) => route.url).join(', ') %>",
)
environment.finishRun()
expect(output.files['/test/test.txt']).toEqual('/test')
})
it('should handle integrations', async () => {
const { environment, output } = createMemoryEnvironment()
const templateFile = createTemplateFile(environment, {
...simpleOptions,
chosenAddOns: [
{
id: 'test',
name: 'Test',
integrations: [
{
type: 'header-user',
path: '/test',
jsName: 'test',
} as Integration,
],
} as AddOn,
],
})
environment.startRun()
await templateFile(
'test.txt.ejs',
"<%= integrations.map((integration) => integration.path).join(', ') %>",
)
environment.finishRun()
expect(output.files['/test/test.txt']).toEqual('/test')
})
it('should handle package manager', async () => {
const { environment, output } = createMemoryEnvironment()
const templateFile = createTemplateFile(environment, simpleOptions)
environment.startRun()
await templateFile(
'foo.txt.ejs',
"<%= getPackageManagerAddScript('foo') %>",
)
await templateFile(
'foo-dev.txt.ejs',
"<%= getPackageManagerAddScript('foo', true) %>",
)
await templateFile(
'run-dev.txt.ejs',
"<%= getPackageManagerRunScript('dev') %>",
)
environment.finishRun()
expect(output.files['/test/foo.txt']).toEqual('pnpm add foo')
expect(output.files['/test/foo-dev.txt']).toEqual('pnpm add foo --dev')
expect(output.files['/test/run-dev.txt']).toEqual('pnpm dev')
})
it('should handle package manager execute script', async () => {
const { environment, output } = createMemoryEnvironment()
const templateFile = createTemplateFile(environment, simpleOptions)
environment.startRun()
await templateFile(
'exec.txt.ejs',
"<%= getPackageManagerExecuteScript('shadcn', ['add', 'button']) %>",
)
environment.finishRun()
expect(output.files['/test/exec.txt']).toEqual('pnpm dlx shadcn add button')
})
it('should normalize src js files to ts when typescript is enabled', async () => {
const { environment, output } = createMemoryEnvironment()
const templateFile = createTemplateFile(environment, simpleOptions)
environment.startRun()
await templateFile('src/lib/auth.js', 'export const auth = true')
await templateFile('src/lib/auth-client.js', 'export const authClient = true')
await templateFile('src/db.js', 'export const db = true')
await templateFile('vite.config.js', 'export default {}')
environment.finishRun()
expect(output.files['/test/src/lib/auth.ts']).toBeDefined()
expect(output.files['/test/src/lib/auth-client.ts']).toBeDefined()
expect(output.files['/test/src/db.ts']).toBeDefined()
expect(output.files['/test/vite.config.js']).toBeDefined()
})
it('should normalize src jsx files to tsx when typescript is enabled', async () => {
const { environment, output } = createMemoryEnvironment()
const templateFile = createTemplateFile(environment, simpleOptions)
environment.startRun()
await templateFile('src/components/auth-button.jsx', 'export default function AuthButton() { return <button>Auth</button> }')
environment.finishRun()
expect(output.files['/test/src/components/auth-button.tsx']).toBeDefined()
})
})