Skip to content
Source
Sign inSign up
TanStack/cli
Open History to see the last commit

template-file.test.ts

178 lines5.0 KB

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 routes: [
106 {
107 path: '/test',
108 name: 'Test',
109 url: '/test',
110 jsName: 'test',
111 },
112 ],
113 } as AddOn,
114 ],
115 })
116​
117 environment.startRun()
118 await templateFile(
119 './test.txt.ejs',
120 "<%= routes.map((route) => route.url).join(', ') %>",
121 )
122 environment.finishRun()
123​
124 expect(output.files['/test/test.txt']).toEqual('/test')
125 })
126​
127 it('should handle integrations', async () => {
128 const { environment, output } = createMemoryEnvironment()
129 const templateFile = createTemplateFile(environment, {
130 ...simpleOptions,
131 chosenAddOns: [
132 {
133 id: 'test',
134 name: 'Test',
135 integrations: [
136 {
137 type: 'header-user',
138 path: '/test',
139 jsName: 'test',
140 } as Integration,
141 ],
142 } as AddOn,
143 ],
144 })
145​
146 environment.startRun()
147 await templateFile(
148 './test.txt.ejs',
149 "<%= integrations.map((integration) => integration.path).join(', ') %>",
150 )
151 environment.finishRun()
152​
153 expect(output.files['/test/test.txt']).toEqual('/test')
154 })
155​
156 it('should handle package manager', async () => {
157 const { environment, output } = createMemoryEnvironment()
158 const templateFile = createTemplateFile(environment, simpleOptions)
159 environment.startRun()
160 await templateFile(
161 './foo.txt.ejs',
162 "<%= getPackageManagerAddScript('foo') %>",
163 )
164 await templateFile(
165 './foo-dev.txt.ejs',
166 "<%= getPackageManagerAddScript('foo', true) %>",
167 )
168 await templateFile(
169 './run-dev.txt.ejs',
170 "<%= getPackageManagerRunScript('dev') %>",
171 )
172 environment.finishRun()
173​
174 expect(output.files['/test/foo.txt']).toEqual('pnpm add foo')
175 expect(output.files['/test/foo-dev.txt']).toEqual('pnpm add foo --dev')
176 expect(output.files['/test/run-dev.txt']).toEqual('pnpm dev')
177 })
178})