Open History to see the last commit
1import { describe, expect, it } from 'vitest'
2
3import { createMemoryEnvironment } from '../src/environment.js'
4import { createTemplateFile } from '../src/template-file.js'
5import { FILE_ROUTER } from '../src/constants.js'
6
7import type { AddOn, Integration, Options } from '../src/types.js'
8
9const simpleOptions = {
10 projectName: 'test',
11 targetDir: '/test',
12 framework: {
13 id: 'test',
14 name: 'Test',
15 },
16 chosenAddOns: [],
17 packageManager: 'pnpm',
18 typescript: true,
19 tailwind: true,
20 mode: FILE_ROUTER,
21} as unknown as Options
22
23describe('createTemplateFile', () => {
24 it('should template a simple file', async () => {
25 const { environment, output } = createMemoryEnvironment()
26 const templateFile = createTemplateFile(environment, simpleOptions)
27 environment.startRun()
28 await templateFile('./test.ts', 'let a = 1')
29 environment.finishRun()
30
31 expect(output.files['/test/test.ts'].trim()).toEqual('let a = 1')
32 })
33
34 it('should handle ignore files', async () => {
35 const { environment, output } = createMemoryEnvironment()
36 const templateFile = createTemplateFile(environment, {
37 ...simpleOptions,
38 } as unknown as Options)
39 environment.startRun()
40 await templateFile('./test.ts.ejs', '<% ignoreFile() %>let a = 1')
41 environment.finishRun()
42
43 expect(output.files['/test/test.ts']).toBeUndefined()
44 })
45
46 it('should handle append files', async () => {
47 const { environment, output } = createMemoryEnvironment()
48 const templateFile = createTemplateFile(environment, simpleOptions)
49 environment.startRun()
50 await templateFile('./test.txt.ejs', 'Line 1\n')
51 await templateFile('./test.txt.append', 'Line 2\n')
52 environment.finishRun()
53
54 expect(output.files['/test/test.txt']).toEqual('Line 1\nLine 2\n')
55 })
56
57 it('should handle enabled add-ons', async () => {
58 const { environment, output } = createMemoryEnvironment()
59 const templateFile = createTemplateFile(environment, {
60 ...simpleOptions,
61 chosenAddOns: [
62 {
63 id: 'test1',
64 name: 'Test 1',
65 },
66 {
67 id: 'test2',
68 name: 'Test 2',
69 },
70 ] as Array<AddOn>,
71 })
72 environment.startRun()
73 await templateFile(
74 './test.txt.ejs',
75 "Addons: <%= Object.keys(addOnEnabled).join(', ') %>",
76 )
77 environment.finishRun()
78
79 expect(output.files['/test/test.txt']).toEqual('Addons: test1, test2')
80 })
81
82 it('should handle relative paths', async () => {
83 const { environment, output } = createMemoryEnvironment()
84 const templateFile = createTemplateFile(environment, simpleOptions)
85 environment.startRun()
86 await templateFile(
87 './src/test/test.txt.ejs',
88 "import { foo } from '<%= relativePath('./foo.ts') %>'",
89 )
90 environment.finishRun()
91
92 expect(output.files['/test/src/test/test.txt']).toEqual(
93 "import { foo } from '../../foo.ts'",
94 )
95 })
96
97 it('should handle routes', async () => {
98 const { environment, output } = createMemoryEnvironment()
99 const templateFile = createTemplateFile(environment, {
100 ...simpleOptions,
101 chosenAddOns: [
102 {
103 id: 'test',
104 name: 'Test',
105 customProperties: {
106 routes: [
107 {
108 path: '/test',
109 name: 'Test',
110 url: '/test',
111 jsName: 'test',
112 },
113 ],
114 },
115 } as AddOn,
116 ],
117 })
118
119 environment.startRun()
120 await templateFile(
121 './test.txt.ejs',
122 "<%= routes.map((route) => route.url).join(', ') %>",
123 )
124 environment.finishRun()
125
126 expect(output.files['/test/test.txt']).toEqual('/test')
127 })
128
129 it('should handle integrations', async () => {
130 const { environment, output } = createMemoryEnvironment()
131 const templateFile = createTemplateFile(environment, {
132 ...simpleOptions,
133 chosenAddOns: [
134 {
135 id: 'test',
136 name: 'Test',
137 customProperties: {
138 integrations: [
139 {
140 type: 'header-user',
141 path: '/test',
142 jsName: 'test',
143 } as Integration,
144 ],
145 },
146 } as AddOn,
147 ],
148 })
149
150 environment.startRun()
151 await templateFile(
152 './test.txt.ejs',
153 "<%= integrations.map((integration) => integration.path).join(', ') %>",
154 )
155 environment.finishRun()
156
157 expect(output.files['/test/test.txt']).toEqual('/test')
158 })
159
160 it('should handle package manager', async () => {
161 const { environment, output } = createMemoryEnvironment()
162 const templateFile = createTemplateFile(environment, simpleOptions)
163 environment.startRun()
164 await templateFile(
165 './foo.txt.ejs',
166 "<%= getPackageManagerAddScript('foo') %>",
167 )
168 await templateFile(
169 './foo-dev.txt.ejs',
170 "<%= getPackageManagerAddScript('foo', true) %>",
171 )
172 await templateFile(
173 './run-dev.txt.ejs',
174 "<%= getPackageManagerRunScript('dev') %>",
175 )
176 environment.finishRun()
177
178 expect(output.files['/test/foo.txt']).toEqual('pnpm add foo')
179 expect(output.files['/test/foo-dev.txt']).toEqual('pnpm add foo --dev')
180 expect(output.files['/test/run-dev.txt']).toEqual('pnpm dev')
181 })
182})