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 supportedModes: {
67 'code-router': {
68 displayName: 'Code Router',
69 description: 'TanStack Router using code to define the routes',
70 forceTypescript: false,
71 },
72 'file-router': {
73 displayName: 'File Router',
74 description: 'TanStack Router using files to define the routes',
75 forceTypescript: true,
76 },
77 },
78 })
79 fetch.mockResponseOnce(
80 JSON.stringify({
81 id: 'https://github.com/cta-dev/cta-starter-solid',
82 tailwind: true,
83 typescript: false,
84 framework: 'solid',
85 mode: 'file-router',
86 type: 'starter',
87 description: 'A starter for Solid',
88 name: 'My Solid Starter',
89 dependsOn: [],
90 files: {},
91 deletedFiles: [],
92 }),
93 )
94
95 const options = await normalizeOptions({
96 projectName: 'test',
97 starter: 'https://github.com/cta-dev/cta-starter-solid',
98 })
99 expect(options?.mode).toBe('file-router')
100 expect(options?.tailwind).toBe(true)
101 expect(options?.typescript).toBe(false)
102 expect(options?.framework?.id).toBe('solid')
103 })
104
105 it('should default to react-cra if no framework is provided', async () => {
106 __testRegisterFramework({
107 id: 'react-cra',
108 name: 'react',
109 })
110 const options = await normalizeOptions({
111 projectName: 'test',
112 })
113 expect(options?.framework?.id).toBe('react-cra')
114 })
115
116 it('should handle forced addons', async () => {
117 __testRegisterFramework({
118 id: 'react-cra',
119 name: 'react',
120 getAddOns: () => [
121 {
122 id: 'foo',
123 name: 'foobar',
124 modes: ['file-router'],
125 },
126 ],
127 })
128 const options = await normalizeOptions(
129 {
130 projectName: 'test',
131 framework: 'react-cra',
132 },
133 'file-router',
134 ['foo'],
135 )
136 expect(options?.chosenAddOns.map((a) => a.id).includes('foo')).toBe(true)
137 })
138
139 it('should handle additional addons from the CLI', async () => {
140 __testRegisterFramework({
141 id: 'react-cra',
142 name: 'react',
143 getAddOns: () => [
144 {
145 id: 'foo',
146 name: 'foobar',
147 modes: ['file-router'],
148 },
149 {
150 id: 'baz',
151 name: 'baz',
152 modes: ['file-router'],
153 },
154 ],
155 })
156 const options = await normalizeOptions(
157 {
158 projectName: 'test',
159 addOns: ['baz'],
160 framework: 'react-cra',
161 template: 'file-router',
162 },
163 'file-router',
164 ['foo'],
165 )
166 expect(options?.chosenAddOns.map((a) => a.id).includes('foo')).toBe(true)
167 expect(options?.chosenAddOns.map((a) => a.id).includes('baz')).toBe(true)
168 expect(options?.tailwind).toBe(true)
169 expect(options?.typescript).toBe(true)
170 })
171
172 it('should handle toolchain as an addon', async () => {
173 __testRegisterFramework({
174 id: 'react-cra',
175 name: 'react',
176 getAddOns: () => [
177 {
178 id: 'biome',
179 name: 'Biome',
180 modes: ['file-router', 'code-router'],
181 },
182 ],
183 })
184 const options = await normalizeOptions({
185 projectName: 'test',
186 toolchain: 'biome',
187 })
188 expect(options?.chosenAddOns.map((a) => a.id).includes('biome')).toBe(true)
189 expect(options?.tailwind).toBe(true)
190 expect(options?.typescript).toBe(true)
191 })
192
193 it('should handle the funky Windows edge case with CLI parsing', async () => {
194 __testRegisterFramework({
195 id: 'react-cra',
196 name: 'react',
197 getAddOns: () => [
198 {
199 id: 'foo',
200 name: 'foobar',
201 modes: ['file-router', 'code-router'],
202 },
203 {
204 id: 'baz',
205 name: 'baz',
206 modes: ['file-router', 'code-router'],
207 },
208 ],
209 })
210 const options = await normalizeOptions({
211 projectName: 'test',
212 addOns: ['baz foo'],
213 })
214 expect(options?.chosenAddOns.map((a) => a.id).includes('foo')).toBe(true)
215 expect(options?.chosenAddOns.map((a) => a.id).includes('baz')).toBe(true)
216 })
217})