Skip to content

Commit 1dd0d07

Browse files
committed
refactor: convert more code to use new inquirer
1 parent c3ebcc9 commit 1dd0d07

File tree

6 files changed

+79
-101
lines changed

6 files changed

+79
-101
lines changed

src/__tests__/lib/command/command-util.test.ts

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
import { jest } from '@jest/globals'
22

3-
import inquirer from 'inquirer'
3+
import type { sort } from '../../../lib/command/output.js'
4+
import type { ListDataFunction, Sorting } from '../../../lib/command/io-defs.js'
5+
import { stringInput } from '../../../lib/user-query.js'
6+
import type { SimpleType } from '../../test-lib/simple-type.js'
47

5-
import { sort } from '../../../lib/command/output.js'
6-
import { ListDataFunction, Sorting } from '../../../lib/command/io-defs.js'
7-
import { SimpleType } from '../../test-lib/simple-type.js'
88

9-
10-
const promptMock = jest.fn<typeof inquirer.prompt>()
11-
jest.unstable_mockModule('inquirer', () => ({
12-
default: {
13-
prompt: promptMock,
14-
},
9+
const stringInputMock = jest.fn<typeof stringInput>()
10+
jest.unstable_mockModule('../../../lib/user-query.js', () => ({
11+
stringInput: stringInputMock,
1512
}))
1613

1714
const sortMock = jest.fn<typeof sort>()
@@ -162,55 +159,46 @@ describe('convertToId', () => {
162159

163160
describe('stringGetIdFromUser', () => {
164161
it('accepts id input from user', async () => {
165-
promptMock.mockResolvedValue({ itemIdOrIndex: 'string-id-a' })
162+
stringInputMock.mockResolvedValue('string-id-a')
166163

167164
const chosenId = await stringGetIdFromUser(config, list)
168165

169166
expect(chosenId).toBe('string-id-a')
170167

171-
expect(promptMock).toHaveBeenCalledExactlyOnceWith({
172-
type: 'input', name: 'itemIdOrIndex',
173-
message: 'Enter id or index', validate: expect.anything(),
174-
})
175-
const validateFunction = (promptMock.mock.calls[0][0] as { validate: (input: string) => true | string }).validate
168+
expect(stringInputMock).toHaveBeenCalledExactlyOnceWith('Enter id or index', { validate: expect.anything() })
169+
const validateFunction = (stringInputMock.mock.calls[0][1] as { validate: (input: string) => true | string }).validate
176170

177171
expect(validateFunction('string-id-a')).toBe(true)
178172
})
179173

180174
it('validation returns error when unable to convert', async () => {
181-
promptMock.mockResolvedValue({ itemIdOrIndex: 'string-id-a' })
175+
stringInputMock.mockResolvedValue('string-id-a')
182176

183177
const chosenId = await stringGetIdFromUser(config, list)
184178

185179
expect(chosenId).toBe('string-id-a')
186180

187-
expect(promptMock).toHaveBeenCalledExactlyOnceWith({
188-
type: 'input', name: 'itemIdOrIndex',
189-
message: 'Enter id or index', validate: expect.anything(),
190-
})
191-
const validateFunction = (promptMock.mock.calls[0][0] as { validate: (input: string) => true | string }).validate
181+
expect(stringInputMock).toHaveBeenCalledExactlyOnceWith('Enter id or index', { validate: expect.anything() })
182+
const validateFunction = (stringInputMock.mock.calls[0][1] as { validate: (input: string) => true | string }).validate
192183

193184
expect(validateFunction('invalid-id')).toBe('Invalid id or index "invalid-id". Please enter an index or valid id.')
194185
})
195186

196187
it('throws error when unable to convert entered value to a valid id', async () => {
197-
promptMock.mockResolvedValue({ itemIdOrIndex: 'invalid-id' })
188+
stringInputMock.mockResolvedValue('invalid-id')
198189

199190
await expect(stringGetIdFromUser(config, list)).rejects.toThrow('unable to convert invalid-id to id')
200191
})
201192

202193
it('handles non-default prompt', async () => {
203-
promptMock.mockResolvedValue({ itemIdOrIndex: 'string-id-a' })
194+
stringInputMock.mockResolvedValue('string-id-a')
204195

205196
const chosenId = await stringGetIdFromUser(config, list, 'give me an id')
206197

207198
expect(chosenId).toBe('string-id-a')
208199

209-
expect(promptMock).toHaveBeenCalledExactlyOnceWith({
210-
type: 'input', name: 'itemIdOrIndex',
211-
message: 'give me an id', validate: expect.anything(),
212-
})
213-
const validateFunction = (promptMock.mock.calls[0][0] as { validate: (input: string) => true | string }).validate
200+
expect(stringInputMock).toHaveBeenCalledExactlyOnceWith('give me an id', { validate: expect.anything() })
201+
const validateFunction = (stringInputMock.mock.calls[0][1] as { validate: (input: string) => true | string }).validate
214202

215203
expect(validateFunction('string-id-a')).toBe(true)
216204
})

src/__tests__/lib/command/select.test.ts

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { jest } from '@jest/globals'
22

3-
import inquirer from 'inquirer'
3+
import type { select } from '@inquirer/prompts'
44
import log4js from 'log4js'
55

66
import type {
@@ -25,13 +25,12 @@ import type {
2525
import type { SimpleType } from '../../test-lib/simple-type.js'
2626

2727

28-
const promptMock = jest.fn<typeof inquirer.prompt>()
29-
jest.unstable_mockModule('inquirer', () => ({
30-
default: {
31-
prompt: promptMock,
32-
},
28+
const selectMock = jest.fn<typeof select>()
29+
jest.unstable_mockModule('@inquirer/prompts', () => ({
30+
select: selectMock,
3331
}))
3432

33+
3534
const resetManagedConfigKeyMock = jest.fn<typeof resetManagedConfigKey>()
3635
const setConfigKeyMock = jest.fn<typeof setConfigKey>()
3736
jest.unstable_mockModule('../../../lib/cli-config.js', () => ({
@@ -235,7 +234,7 @@ describe('selectFromList', () => {
235234
expect(stringGetIdFromUserMock).not.toHaveBeenCalled()
236235

237236
expect(booleanConfigValueMock).not.toHaveBeenCalled()
238-
expect(promptMock).not.toHaveBeenCalled()
237+
expect(selectMock).not.toHaveBeenCalled()
239238
expect(setConfigKeyMock).not.toHaveBeenCalled()
240239
expect(getItemMock).not.toHaveBeenCalled()
241240
expect(userMessageMock).not.toHaveBeenCalled()
@@ -251,7 +250,7 @@ describe('selectFromList', () => {
251250
expect(stringGetIdFromUserMock).toHaveBeenCalledTimes(1)
252251

253252
expect(booleanConfigValueMock).not.toHaveBeenCalled()
254-
expect(promptMock).not.toHaveBeenCalled()
253+
expect(selectMock).not.toHaveBeenCalled()
255254
expect(setConfigKeyMock).not.toHaveBeenCalled()
256255
expect(getItemMock).not.toHaveBeenCalled()
257256
expect(userMessageMock).not.toHaveBeenCalled()
@@ -275,7 +274,7 @@ describe('selectFromList', () => {
275274
expect(stringGetIdFromUserMock).not.toHaveBeenCalled()
276275

277276
expect(booleanConfigValueMock).not.toHaveBeenCalled()
278-
expect(promptMock).not.toHaveBeenCalled()
277+
expect(selectMock).not.toHaveBeenCalled()
279278
expect(setConfigKeyMock).not.toHaveBeenCalled()
280279
expect(userMessageMock).toHaveBeenCalledExactlyOnceWith(item1)
281280
expect(resetManagedConfigKeyMock).not.toHaveBeenCalled()
@@ -286,7 +285,7 @@ describe('selectFromList', () => {
286285
defaultItem: 'default-item-id',
287286
})
288287
getItemMock.mockResolvedValueOnce(undefined as unknown as SimpleType)
289-
promptMock.mockResolvedValue({ answer: 'No' })
288+
selectMock.mockResolvedValue('no')
290289

291290
const options = { listItems: listItemsMock, defaultValue }
292291
expect(await selectFromList(commandWithDefault, config, options)).toBe('chosen-id')
@@ -296,7 +295,7 @@ describe('selectFromList', () => {
296295
expect(stringGetIdFromUserMock).toHaveBeenCalledTimes(1)
297296

298297
expect(booleanConfigValueMock).toHaveBeenCalledTimes(1)
299-
expect(promptMock).toHaveBeenCalledTimes(1)
298+
expect(selectMock).toHaveBeenCalledTimes(1)
300299
expect(setConfigKeyMock).not.toHaveBeenCalled()
301300
expect(getItemMock).toHaveBeenCalledExactlyOnceWith('default-item-id')
302301
expect(userMessageMock).not.toHaveBeenCalled()
@@ -311,7 +310,7 @@ describe('selectFromList', () => {
311310
defaultItem: 'default-item-id',
312311
})
313312
getItemMock.mockRejectedValueOnce({ response: { status: statusCode } })
314-
promptMock.mockResolvedValue({ answer: 'No' })
313+
selectMock.mockResolvedValue('no')
315314

316315
const options = { listItems: listItemsMock, defaultValue }
317316
expect(await selectFromList(commandWithDefault, config, options)).toBe('chosen-id')
@@ -321,7 +320,7 @@ describe('selectFromList', () => {
321320
expect(stringGetIdFromUserMock).toHaveBeenCalledTimes(1)
322321

323322
expect(booleanConfigValueMock).toHaveBeenCalledTimes(1)
324-
expect(promptMock).toHaveBeenCalledTimes(1)
323+
expect(selectMock).toHaveBeenCalledTimes(1)
325324
expect(setConfigKeyMock).not.toHaveBeenCalled()
326325
expect(getItemMock).toHaveBeenCalledExactlyOnceWith('default-item-id')
327326
expect(userMessageMock).not.toHaveBeenCalled()
@@ -335,7 +334,7 @@ describe('selectFromList', () => {
335334
defaultItem: 'default-item-id',
336335
})
337336
getItemMock.mockRejectedValueOnce(Error('unexpected error'))
338-
promptMock.mockResolvedValue({ answer: 'No' })
337+
selectMock.mockResolvedValue('no')
339338

340339
const options = { listItems: listItemsMock, defaultValue }
341340
await expect(selectFromList(commandWithDefault, config, options))
@@ -346,7 +345,7 @@ describe('selectFromList', () => {
346345
expect(stringGetIdFromUserMock).not.toHaveBeenCalled()
347346

348347
expect(booleanConfigValueMock).not.toHaveBeenCalled()
349-
expect(promptMock).not.toHaveBeenCalled()
348+
expect(selectMock).not.toHaveBeenCalled()
350349
expect(setConfigKeyMock).not.toHaveBeenCalled()
351350
expect(getItemMock).toHaveBeenCalledExactlyOnceWith('default-item-id')
352351
expect(userMessageMock).not.toHaveBeenCalled()
@@ -355,7 +354,7 @@ describe('selectFromList', () => {
355354

356355
it('does not save default when user asked not to', async () => {
357356
booleanConfigValueMock.mockReturnValueOnce(false)
358-
promptMock.mockResolvedValue({ answer: 'No' })
357+
selectMock.mockResolvedValue('no')
359358

360359
expect(await selectFromList(command, config,
361360
{ listItems: listItemsMock, defaultValue })).toBe('chosen-id')
@@ -366,8 +365,8 @@ describe('selectFromList', () => {
366365

367366
expect(booleanConfigValueMock)
368367
.toHaveBeenCalledExactlyOnceWith('defaultItem::neverAskForSaveAgain')
369-
expect(promptMock).toHaveBeenCalledExactlyOnceWith(
370-
expect.objectContaining({ type: 'list', name: 'answer' }),
368+
expect(selectMock).toHaveBeenCalledExactlyOnceWith(
369+
expect.objectContaining({ message: 'Do you want to save this as the default?' }),
371370
)
372371
expect(setConfigKeyMock).not.toHaveBeenCalled()
373372
})
@@ -384,13 +383,13 @@ describe('selectFromList', () => {
384383

385384
expect(booleanConfigValueMock)
386385
.toHaveBeenCalledExactlyOnceWith('defaultItem::neverAskForSaveAgain')
387-
expect(promptMock).not.toHaveBeenCalled()
386+
expect(selectMock).not.toHaveBeenCalled()
388387
expect(setConfigKeyMock).not.toHaveBeenCalled()
389388
})
390389

391390
it('saves selected item as default', async () => {
392391
booleanConfigValueMock.mockReturnValueOnce(false)
393-
promptMock.mockResolvedValue({ answer: 'yes' })
392+
selectMock.mockResolvedValue('yes')
394393

395394
expect(await selectFromList(command, config,
396395
{ listItems: listItemsMock, defaultValue })).toBe('chosen-id')
@@ -401,8 +400,8 @@ describe('selectFromList', () => {
401400

402401
expect(booleanConfigValueMock)
403402
.toHaveBeenCalledExactlyOnceWith('defaultItem::neverAskForSaveAgain')
404-
expect(promptMock).toHaveBeenCalledExactlyOnceWith(
405-
expect.objectContaining({ type: 'list', name: 'answer' }),
403+
expect(selectMock).toHaveBeenCalledExactlyOnceWith(
404+
expect.objectContaining({ message: 'Do you want to save this as the default?' }),
406405
)
407406
expect(setConfigKeyMock)
408407
.toHaveBeenCalledExactlyOnceWith(command.cliConfig, 'defaultItem', 'chosen-id')
@@ -412,7 +411,7 @@ describe('selectFromList', () => {
412411

413412
it('saves "never ask again" response"', async () => {
414413
booleanConfigValueMock.mockReturnValueOnce(false)
415-
promptMock.mockResolvedValue({ answer: 'never' })
414+
selectMock.mockResolvedValue('never')
416415

417416
expect(await selectFromList(command, config,
418417
{ listItems: listItemsMock, defaultValue })).toBe('chosen-id')
@@ -423,8 +422,8 @@ describe('selectFromList', () => {
423422

424423
expect(booleanConfigValueMock).toHaveBeenCalledTimes(1)
425424
expect(booleanConfigValueMock).toHaveBeenCalledWith('defaultItem::neverAskForSaveAgain')
426-
expect(promptMock).toHaveBeenCalledExactlyOnceWith(
427-
expect.objectContaining({ type: 'list', name: 'answer' }),
425+
expect(selectMock).toHaveBeenCalledExactlyOnceWith(
426+
expect.objectContaining({ message: 'Do you want to save this as the default?' }),
428427
)
429428
expect(setConfigKeyMock).toHaveBeenCalledExactlyOnceWith(
430429
command.cliConfig,

0 commit comments

Comments
 (0)