Open History to see the last commit
1import { beforeEach, describe, expect, it } from 'vitest'
2
3import { normalizeOptions } from '../src/command-line.js'
4import {
5 __testRegisterFramework,
6 __testClearFrameworks,
7} from '@tanstack/cta-engine/dist/frameworks.js'
8
9beforeEach(() => {
10 __testClearFrameworks()
11})
12
13describe('normalizeOptions', () => {
14 it('should return undefined if project name is not provided', async () => {
15 const options = await normalizeOptions({})
16 expect(options).toBeUndefined()
17 })
18
19 it('should return enable typescript based on the framework', async () => {
20 const jsOptions = await normalizeOptions({
21 projectName: 'test',
22 template: 'javascript',
23 })
24 expect(jsOptions?.typescript).toBe(false)
25 expect(jsOptions?.mode).toBe('code-router')
26
27 const tsOptions = await normalizeOptions({
28 projectName: 'test',
29 template: 'typescript',
30 })
31 expect(tsOptions?.typescript).toBe(true)
32 expect(tsOptions?.mode).toBe('code-router')
33
34 const frOptions = await normalizeOptions({
35 projectName: 'test',
36 template: 'file-router',
37 })
38 expect(frOptions?.typescript).toBe(true)
39 expect(frOptions?.mode).toBe('file-router')
40 })
41
42 it('should return enable tailwind if the framework is solid', async () => {
43 const solidOptions = await normalizeOptions({
44 projectName: 'test',
45 framework: 'solid',
46 })
47 expect(solidOptions?.tailwind).toBe(true)
48
49 const twOptions = await normalizeOptions({
50 projectName: 'test',
51 tailwind: true,
52 })
53 expect(twOptions?.tailwind).toBe(true)
54
55 const noOptions = await normalizeOptions({
56 projectName: 'test',
57 })
58 expect(noOptions?.tailwind).toBe(false)
59 })
60
61 it('should handle a starter url', async () => {
62 __testRegisterFramework({
63 id: 'solid',
64 name: 'Solid',
65 getAddOns: () => [
66 {
67 id: 'nitro',
68 name: 'nitro',
69 modes: ['file-router'],
70 default: true,
71 },
72 ],
73 supportedModes: {
74 'code-router': {
75 displayName: 'Code Router',
76 description: 'TanStack Router using code to define the routes',
77 forceTypescript: false,
78 },
79 'file-router': {
80 displayName: 'File Router',
81 description: 'TanStack Router using files to define the routes',
82 forceTypescript: true,
83 },
84 },
85 })
86 fetch.mockResponseOnce(
87 JSON.stringify({
88 id: 'https://github.com/cta-dev/cta-starter-solid',
89 tailwind: true,
90 typescript: false,
91 framework: 'solid',
92 mode: 'file-router',
93 type: 'starter',
94 description: 'A starter for Solid',
95 name: 'My Solid Starter',
96 dependsOn: [],
97 files: {},
98 deletedFiles: [],
99 }),
100 )
101
102 const options = await normalizeOptions({
103 projectName: 'test',
104 starter: 'https://github.com/cta-dev/cta-starter-solid',
105 deployment: 'nitro',
106 })
107 expect(options?.mode).toBe('file-router')
108 expect(options?.tailwind).toBe(true)
109 expect(options?.typescript).toBe(true)
110 expect(options?.framework?.id).toBe('solid')
111 })
112
113 it('should default to react-cra if no framework is provided', async () => {
114 __testRegisterFramework({
115 id: 'react-cra',
116 name: 'react',
117 })
118 const options = await normalizeOptions({
119 projectName: 'test',
120 })
121 expect(options?.framework?.id).toBe('react-cra')
122 })
123
124 it('should handle forced addons', async () => {
125 __testRegisterFramework({
126 id: 'react-cra',
127 name: 'react',
128 getAddOns: () => [
129 {
130 id: 'foo',
131 name: 'foobar',
132 modes: ['file-router'],
133 },
134 {
135 id: 'nitro',
136 name: 'nitro',
137 modes: ['file-router'],
138 default: true,
139 },
140 ],
141 })
142 const options = await normalizeOptions(
143 {
144 projectName: 'test',
145 framework: 'react-cra',
146 },
147 'file-router',
148 ['foo'],
149 )
150 expect(options?.chosenAddOns.map((a) => a.id).includes('foo')).toBe(true)
151 })
152
153 it('should handle additional addons from the CLI', async () => {
154 __testRegisterFramework({
155 id: 'react-cra',
156 name: 'react',
157 getAddOns: () => [
158 {
159 id: 'foo',
160 name: 'foobar',
161 modes: ['file-router'],
162 },
163 {
164 id: 'baz',
165 name: 'baz',
166 modes: ['file-router'],
167 },
168 {
169 id: 'nitro',
170 name: 'nitro',
171 modes: ['file-router'],
172 default: true,
173 },
174 ],
175 })
176 const options = await normalizeOptions(
177 {
178 projectName: 'test',
179 addOns: ['baz'],
180 framework: 'react-cra',
181 template: 'file-router',
182 },
183 'file-router',
184 ['foo'],
185 )
186 expect(options?.chosenAddOns.map((a) => a.id).includes('foo')).toBe(true)
187 expect(options?.chosenAddOns.map((a) => a.id).includes('baz')).toBe(true)
188 expect(options?.tailwind).toBe(true)
189 expect(options?.typescript).toBe(true)
190 })
191
192 it('should handle toolchain as an addon', async () => {
193 __testRegisterFramework({
194 id: 'react-cra',
195 name: 'react',
196 getAddOns: () => [
197 {
198 id: 'biome',
199 name: 'Biome',
200 modes: ['file-router', 'code-router'],
201 },
202 {
203 id: 'nitro',
204 name: 'nitro',
205 modes: ['file-router', 'code-router'],
206 default: true,
207 },
208 ],
209 })
210 const options = await normalizeOptions({
211 projectName: 'test',
212 toolchain: 'biome',
213 })
214 expect(options?.chosenAddOns.map((a) => a.id).includes('biome')).toBe(true)
215 expect(options?.tailwind).toBe(true)
216 expect(options?.typescript).toBe(true)
217 })
218
219 it('should handle the funky Windows edge case with CLI parsing', async () => {
220 __testRegisterFramework({
221 id: 'react-cra',
222 name: 'react',
223 getAddOns: () => [
224 {
225 id: 'foo',
226 name: 'foobar',
227 modes: ['file-router', 'code-router'],
228 },
229 {
230 id: 'baz',
231 name: 'baz',
232 modes: ['file-router', 'code-router'],
233 },
234 {
235 id: 'nitro',
236 name: 'nitro',
237 modes: ['file-router', 'code-router'],
238 default: true,
239 },
240 ],
241 })
242 const options = await normalizeOptions({
243 projectName: 'test',
244 addOns: ['baz foo'],
245 })
246 expect(options?.chosenAddOns.map((a) => a.id).includes('foo')).toBe(true)
247 expect(options?.chosenAddOns.map((a) => a.id).includes('baz')).toBe(true)
248 })
249})