Open History to see the last commit
1import { describe, it, expect, vi } from 'vitest'
2
3import * as clack from '@clack/prompts'
4
5import {
6 getProjectName,
7 selectAddOns,
8 selectGit,
9 selectPackageManager,
10 selectRouterType,
11 selectTailwind,
12 selectToolchain,
13} from '../src/ui-prompts'
14
15import type { AddOn, Framework } from '@tanstack/cta-engine'
16
17vi.mock('@clack/prompts')
18
19vi.spyOn(process, 'exit').mockImplementation((number) => {
20 throw new Error(`process.exit: ${number}`)
21})
22
23describe('getProjectName', () => {
24 it('should return the project name', async () => {
25 vi.spyOn(clack, 'text').mockImplementation(async () => 'my-app')
26 vi.spyOn(clack, 'isCancel').mockImplementation(() => false)
27
28 const projectName = await getProjectName()
29 expect(projectName).toBe('my-app')
30 })
31
32 it('should exit on cancel', async () => {
33 vi.spyOn(clack, 'text').mockImplementation(async () => 'Cancelled')
34 vi.spyOn(clack, 'isCancel').mockImplementation(() => true)
35
36 await expect(getProjectName()).rejects.toThrowError(/exit/)
37 })
38})
39
40describe('selectRouterType', () => {
41 it('should select the file router', async () => {
42 vi.spyOn(clack, 'select').mockImplementation(async () => 'file-router')
43 vi.spyOn(clack, 'isCancel').mockImplementation(() => false)
44
45 const routerType = await selectRouterType()
46 expect(routerType).toBe('file-router')
47 })
48
49 it('should exit on cancel', async () => {
50 vi.spyOn(clack, 'select').mockImplementation(async () => 'Cancelled')
51 vi.spyOn(clack, 'isCancel').mockImplementation(() => true)
52
53 await expect(() => selectRouterType()).rejects.toThrowError(/exit/)
54 })
55})
56
57describe('selectTailwind', () => {
58 it('should select tailwind', async () => {
59 vi.spyOn(clack, 'confirm').mockImplementation(async () => true)
60 vi.spyOn(clack, 'isCancel').mockImplementation(() => false)
61
62 const tailwind = await selectTailwind()
63 expect(tailwind).toBe(true)
64 })
65
66 it('should exit on cancel', async () => {
67 vi.spyOn(clack, 'confirm').mockImplementation(async () =>
68 Symbol.for('cancel'),
69 )
70 vi.spyOn(clack, 'isCancel').mockImplementation(() => true)
71
72 await expect(selectTailwind()).rejects.toThrowError(/exit/)
73 })
74})
75
76describe('selectPackageManager', () => {
77 it('should select pnpm', async () => {
78 vi.spyOn(clack, 'select').mockImplementation(async () => 'pnpm')
79 vi.spyOn(clack, 'isCancel').mockImplementation(() => false)
80
81 const packageManager = await selectPackageManager()
82 expect(packageManager).toBe('pnpm')
83 })
84
85 it('should exit on cancel', async () => {
86 vi.spyOn(clack, 'select').mockImplementation(async () =>
87 Symbol.for('cancel'),
88 )
89 vi.spyOn(clack, 'isCancel').mockImplementation(() => true)
90
91 await expect(selectPackageManager()).rejects.toThrowError(/exit/)
92 })
93})
94
95describe('selectAddOns', () => {
96 it('should show keyboard shortcuts help and select add-ons', async () => {
97 const noteSpy = vi.spyOn(clack, 'note').mockImplementation(() => {})
98 vi.spyOn(clack, 'multiselect').mockImplementation(async () => ['add-on-1'])
99 vi.spyOn(clack, 'isCancel').mockImplementation(() => false)
100
101 const packageManager = await selectAddOns(
102 {
103 getAddOns: () =>
104 [
105 {
106 id: 'add-on-1',
107 name: 'Add-on 1',
108 description: 'Add-on 1 description',
109 type: 'add-on',
110 modes: ['file-router'],
111 },
112 ] as Array<AddOn>,
113 } as Framework,
114 'file-router',
115 'add-on',
116 'Select add-ons',
117 )
118
119 expect(packageManager).toEqual(['add-on-1'])
120 expect(noteSpy).toHaveBeenCalledWith(
121 'Use ↑/↓ to navigate • Space to select/deselect • Enter to confirm',
122 'Keyboard Shortcuts',
123 )
124 })
125
126 it('should exit on cancel', async () => {
127 vi.spyOn(clack, 'select').mockImplementation(async () =>
128 Symbol.for('cancel'),
129 )
130 vi.spyOn(clack, 'isCancel').mockImplementation(() => true)
131
132 await expect(
133 selectAddOns(
134 {
135 getAddOns: () =>
136 [
137 {
138 id: 'add-on-1',
139 name: 'Add-on 1',
140 description: 'Add-on 1 description',
141 type: 'add-on',
142 modes: ['file-router'],
143 },
144 ] as Array<AddOn>,
145 } as Framework,
146 'file-router',
147 'add-on',
148 'Select add-ons',
149 ),
150 ).rejects.toThrowError(/exit/)
151 })
152})
153
154describe('selectGit', () => {
155 it('should select git', async () => {
156 vi.spyOn(clack, 'confirm').mockImplementation(async () => true)
157 vi.spyOn(clack, 'isCancel').mockImplementation(() => false)
158
159 const git = await selectGit()
160 expect(git).toBe(true)
161 })
162
163 it('should exit on cancel', async () => {
164 vi.spyOn(clack, 'confirm').mockImplementation(async () =>
165 Symbol.for('cancel'),
166 )
167 vi.spyOn(clack, 'isCancel').mockImplementation(() => true)
168
169 await expect(selectGit()).rejects.toThrowError(/exit/)
170 })
171})
172
173describe('selectToolchain', () => {
174 it('should select a toolchain', async () => {
175 vi.spyOn(clack, 'select').mockImplementation(async () => 'biome')
176 vi.spyOn(clack, 'isCancel').mockImplementation(() => false)
177
178 const packageManager = await selectToolchain({
179 getAddOns: () =>
180 [
181 {
182 id: 'biome',
183 name: 'Biome',
184 description: 'Biome description',
185 type: 'toolchain',
186 modes: ['file-router'],
187 },
188 ] as Array<AddOn>,
189 } as Framework)
190 expect(packageManager).toEqual('biome')
191 })
192 it('should select a toolchain', async () => {
193 const selectSpy = vi
194 .spyOn(clack, 'select')
195 .mockImplementation(async () => 'biome')
196 vi.spyOn(clack, 'isCancel').mockImplementation(() => false)
197
198 const packageManager = await selectToolchain(
199 {
200 getAddOns: () =>
201 [
202 {
203 id: 'biome',
204 name: 'Biome',
205 description: 'Biome description',
206 type: 'toolchain',
207 modes: ['file-router'],
208 },
209 ] as Array<AddOn>,
210 } as Framework,
211 'biome',
212 )
213 expect(packageManager).toEqual('biome')
214 expect(selectSpy).not.toHaveBeenCalled()
215 })
216
217 it('should exit on cancel', async () => {
218 vi.spyOn(clack, 'select').mockImplementation(async () =>
219 Symbol.for('cancel'),
220 )
221 vi.spyOn(clack, 'isCancel').mockImplementation(() => true)
222
223 await expect(
224 selectToolchain({
225 getAddOns: () =>
226 [
227 {
228 id: 'biome',
229 name: 'Biome',
230 description: 'Biome description',
231 type: 'toolchain',
232 modes: ['file-router'],
233 },
234 ] as Array<AddOn>,
235 } as Framework),
236 ).rejects.toThrowError(/exit/)
237 })
238})