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

options.test.ts

299 lines7.5 KB

1import { beforeEach, describe, it, expect, vi } from 'vitest'
2​
3import { promptForCreateOptions } from '../src/options'
4import {
5 __testClearFrameworks,
6 __testRegisterFramework,
7} from '@tanstack/cta-engine/dist/frameworks'
8​
9import * as prompts from '../src/ui-prompts'
10​
11import type { Framework } from '@tanstack/cta-engine'
12​
13import type { CliOptions } from '../src/types'
14​
15vi.mock('../src/ui-prompts')
16​
17beforeEach(() => {
18 __testClearFrameworks()
19 __testRegisterFramework({
20 id: 'react-cra',
21 name: 'react',
22 getAddOns: () => [
23 {
24 id: 'react-query',
25 type: 'add-on',
26 modes: ['file-router', 'code-router'],
27 },
28 {
29 id: 'tanstack-chat',
30 type: 'add-on',
31 modes: ['file-router', 'code-router'],
32 },
33 {
34 id: 'biome',
35 type: 'toolchain',
36 modes: ['file-router', 'code-router'],
37 },
38 ],
39 supportedModes: {
40 'code-router': {
41 displayName: 'Code Router',
42 description: 'TanStack Router using code to define the routes',
43 forceTypescript: false,
44 },
45 'file-router': {
46 displayName: 'File Router',
47 description: 'TanStack Router using files to define the routes',
48 forceTypescript: true,
49 },
50 },
51 } as unknown as Framework)
52​
53 __testRegisterFramework({
54 id: 'solid',
55 name: 'solid',
56 getAddOns: () => [],
57 } as unknown as Framework)
58})
59​
60const baseCliOptions: CliOptions = {
61 framework: 'react-cra',
62 addOns: [],
63 toolchain: undefined,
64 projectName: undefined,
65 git: undefined,
67​
68function setBasicSpies() {
69 vi.spyOn(prompts, 'getProjectName').mockImplementation(async () => 'hello')
70 vi.spyOn(prompts, 'selectRouterType').mockImplementation(
71 async () => 'file-router',
72 )
73 vi.spyOn(prompts, 'selectTypescript').mockImplementation(async () => true)
74 vi.spyOn(prompts, 'selectTailwind').mockImplementation(async () => true)
75 vi.spyOn(prompts, 'selectPackageManager').mockImplementation(
76 async () => 'npm',
77 )
78 vi.spyOn(prompts, 'selectToolchain').mockImplementation(async () => undefined)
79 vi.spyOn(prompts, 'selectAddOns').mockImplementation(async () => [])
81​
82describe('promptForCreateOptions', () => {
83 //// Project name
84​
85 it('prompt for a project name', async () => {
86 setBasicSpies()
87​
88 const options = await promptForCreateOptions(baseCliOptions, {})
89​
90 expect(options?.projectName).toBe('hello')
91 })
92​
93 it('accept incoming project name', async () => {
94 setBasicSpies()
95​
96 const options = await promptForCreateOptions(
97 { ...baseCliOptions, projectName: 'override' },
98 {},
99 )
100​
101 expect(options?.projectName).toBe('override')
102 })
103​
104 //// Mode (router type)
105​
106 it('forceMode should override template', async () => {
107 setBasicSpies()
108​
109 const options = await promptForCreateOptions(
110 { ...baseCliOptions, template: 'javascript' },
111 { forcedMode: 'file-router' },
112 )
113​
114 expect(options?.mode).toBe('file-router')
115 expect(options?.typescript).toBe(true)
116 })
117​
118 it('takes template from cli options - code-router', async () => {
119 setBasicSpies()
120​
121 vi.spyOn(prompts, 'selectRouterType').mockImplementation(
122 async () => 'code-router',
123 )
124​
125 const options = await promptForCreateOptions(
126 { ...baseCliOptions, template: 'javascript' },
127 {},
128 )
129​
130 expect(options?.mode).toBe('code-router')
131 })
132​
133 it('takes template from cli options - file-router', async () => {
134 setBasicSpies()
135​
136 vi.spyOn(prompts, 'selectRouterType').mockImplementation(
137 async () => 'code-router',
138 )
139​
140 const options = await promptForCreateOptions(
141 { ...baseCliOptions, template: 'file-router' },
142 {},
143 )
144​
145 expect(options?.mode).toBe('file-router')
146 })
147​
148 it('prompt for router type when unspecified', async () => {
149 setBasicSpies()
150​
151 vi.spyOn(prompts, 'selectRouterType').mockImplementation(
152 async () => 'code-router',
153 )
154​
155 const options = await promptForCreateOptions(
156 { ...baseCliOptions, tailwind: false, framework: undefined },
157 {},
158 )
159​
160 expect(options?.mode).toBe('code-router')
161 })
162​
163 //// Tailwind
164​
165 it('prompt for tailwind when unspecified in react-cra', async () => {
166 setBasicSpies()
167 vi.spyOn(prompts, 'selectTailwind').mockImplementation(async () => false)
168 const options = await promptForCreateOptions(
169 { ...baseCliOptions, tailwind: undefined },
170 {},
171 )
172​
173 expect(options?.tailwind).toBe(false)
174 })
175​
176 it('prompt for tailwind when unspecified in react-cra - true', async () => {
177 setBasicSpies()
178 vi.spyOn(prompts, 'selectTailwind').mockImplementation(async () => true)
179 const options = await promptForCreateOptions(
180 { ...baseCliOptions, tailwind: undefined },
181 {},
182 )
183​
184 expect(options?.tailwind).toBe(true)
185 })
186​
187 it('set tailwind when solid', async () => {
188 setBasicSpies()
189 const options = await promptForCreateOptions(
190 { ...baseCliOptions, tailwind: undefined, framework: 'solid' },
191 {},
192 )
193​
194 expect(options?.tailwind).toBe(true)
195 })
196​
197 //// Package manager
198​
199 it('uses the package manager from the cli options', async () => {
200 setBasicSpies()
201​
202 const options = await promptForCreateOptions(
203 { ...baseCliOptions, packageManager: 'bun' },
204 {},
205 )
206​
207 expect(options?.packageManager).toBe('bun')
208 })
209​
210 it('uses the package manager from the cli options', async () => {
211 setBasicSpies()
212​
213 process.env.npm_config_userconfig = 'blarg'
214​
215 const options = await promptForCreateOptions(
216 { ...baseCliOptions, packageManager: undefined },
217 {},
218 )
219​
220 expect(options?.packageManager).toBe('pnpm')
221 })
222​
223 //// Add-ons
224 it('should be clean when no add-ons are selected', async () => {
225 setBasicSpies()
226​
227 const options = await promptForCreateOptions({ ...baseCliOptions }, {})
228​
229 expect(options?.chosenAddOns).toEqual([])
230 })
231​
232 it('should select biome when toolchain is specified', async () => {
233 setBasicSpies()
234​
235 vi.spyOn(prompts, 'selectToolchain').mockImplementation(async () => 'biome')
236​
237 const options = await promptForCreateOptions(
238 { ...baseCliOptions, toolchain: 'biome' },
239 {},
240 )
241​
242 expect(options?.chosenAddOns.map((a) => a.id).sort()).toEqual(['biome'])
243 })
244​
245 it('should handle forced add-ons', async () => {
246 setBasicSpies()
247​
248 vi.spyOn(prompts, 'selectToolchain').mockImplementation(
249 async () => undefined,
250 )
251​
252 const options = await promptForCreateOptions(
253 { ...baseCliOptions },
254 { forcedAddOns: ['react-query'] },
255 )
256​
257 expect(options?.chosenAddOns.map((a) => a.id).sort()).toEqual([
258 'react-query',
259 ])
260 expect(options?.tailwind).toBe(true)
261 expect(options?.typescript).toBe(true)
262 })
263​
264 it('should handle add-ons from the CLI', async () => {
265 setBasicSpies()
266​
267 const options = await promptForCreateOptions(
268 { ...baseCliOptions, addOns: ['biome', 'react-query'] },
269 {},
270 )
271​
272 expect(options?.chosenAddOns.map((a) => a.id).sort()).toEqual([
273 'biome',
274 'react-query',
275 ])
276 expect(options?.tailwind).toBe(true)
277 expect(options?.typescript).toBe(true)
278 })
279​
280 it('should handle user-selected add-ons', async () => {
281 setBasicSpies()
282​
283 vi.spyOn(prompts, 'selectAddOns').mockImplementation(async () =>
284 Promise.resolve(['biome', 'react-query']),
285 )
286​
287 const options = await promptForCreateOptions(
288 { ...baseCliOptions, addOns: undefined },
289 {},
290 )
291​
292 expect(options?.chosenAddOns.map((a) => a.id).sort()).toEqual([
293 'biome',
294 'react-query',
295 ])
296 expect(options?.tailwind).toBe(true)
297 expect(options?.typescript).toBe(true)
298 })
299})