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

ui-prompts.test.ts

232 lines6.4 KB

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 select some add-ons', async () => {
97 vi.spyOn(clack, 'multiselect').mockImplementation(async () => ['add-on-1'])
98 vi.spyOn(clack, 'isCancel').mockImplementation(() => false)
99​
100 const packageManager = await selectAddOns(
101 {
102 getAddOns: () =>
103 [
104 {
105 id: 'add-on-1',
106 name: 'Add-on 1',
107 description: 'Add-on 1 description',
108 type: 'add-on',
109 modes: ['file-router'],
110 },
111 ] as Array<AddOn>,
112 } as Framework,
113 'file-router',
114 'add-on',
115 'Select add-ons',
116 )
117 expect(packageManager).toEqual(['add-on-1'])
118 })
119​
120 it('should exit on cancel', async () => {
121 vi.spyOn(clack, 'select').mockImplementation(async () =>
122 Symbol.for('cancel'),
123 )
124 vi.spyOn(clack, 'isCancel').mockImplementation(() => true)
125​
126 await expect(
127 selectAddOns(
128 {
129 getAddOns: () =>
130 [
131 {
132 id: 'add-on-1',
133 name: 'Add-on 1',
134 description: 'Add-on 1 description',
135 type: 'add-on',
136 modes: ['file-router'],
137 },
138 ] as Array<AddOn>,
139 } as Framework,
140 'file-router',
141 'add-on',
142 'Select add-ons',
143 ),
144 ).rejects.toThrowError(/exit/)
145 })
146})
147​
148describe('selectGit', () => {
149 it('should select git', async () => {
150 vi.spyOn(clack, 'confirm').mockImplementation(async () => true)
151 vi.spyOn(clack, 'isCancel').mockImplementation(() => false)
152​
153 const git = await selectGit()
154 expect(git).toBe(true)
155 })
156​
157 it('should exit on cancel', async () => {
158 vi.spyOn(clack, 'confirm').mockImplementation(async () =>
159 Symbol.for('cancel'),
160 )
161 vi.spyOn(clack, 'isCancel').mockImplementation(() => true)
162​
163 await expect(selectGit()).rejects.toThrowError(/exit/)
164 })
165})
166​
167describe('selectToolchain', () => {
168 it('should select a toolchain', async () => {
169 vi.spyOn(clack, 'select').mockImplementation(async () => 'biome')
170 vi.spyOn(clack, 'isCancel').mockImplementation(() => false)
171​
172 const packageManager = await selectToolchain({
173 getAddOns: () =>
174 [
175 {
176 id: 'biome',
177 name: 'Biome',
178 description: 'Biome description',
179 type: 'toolchain',
180 modes: ['file-router'],
181 },
182 ] as Array<AddOn>,
183 } as Framework)
184 expect(packageManager).toEqual('biome')
185 })
186 it('should select a toolchain', async () => {
187 const selectSpy = vi
188 .spyOn(clack, 'select')
189 .mockImplementation(async () => 'biome')
190 vi.spyOn(clack, 'isCancel').mockImplementation(() => false)
191​
192 const packageManager = await selectToolchain(
193 {
194 getAddOns: () =>
195 [
196 {
197 id: 'biome',
198 name: 'Biome',
199 description: 'Biome description',
200 type: 'toolchain',
201 modes: ['file-router'],
202 },
203 ] as Array<AddOn>,
204 } as Framework,
205 'biome',
206 )
207 expect(packageManager).toEqual('biome')
208 expect(selectSpy).not.toHaveBeenCalled()
209 })
210​
211 it('should exit on cancel', async () => {
212 vi.spyOn(clack, 'select').mockImplementation(async () =>
213 Symbol.for('cancel'),
214 )
215 vi.spyOn(clack, 'isCancel').mockImplementation(() => true)
216​
217 await expect(
218 selectToolchain({
219 getAddOns: () =>
220 [
221 {
222 id: 'biome',
223 name: 'Biome',
224 description: 'Biome description',
225 type: 'toolchain',
226 modes: ['file-router'],
227 },
228 ] as Array<AddOn>,
229 } as Framework),
230 ).rejects.toThrowError(/exit/)
231 })
232})