diff --git a/src/__tests__/lib/command/util/virtualdevices.test.ts b/src/__tests__/lib/command/util/virtualdevices.test.ts index 2b318b1e..64df68e7 100644 --- a/src/__tests__/lib/command/util/virtualdevices.test.ts +++ b/src/__tests__/lib/command/util/virtualdevices.test.ts @@ -1,20 +1,19 @@ import { jest } from '@jest/globals' -import type inquirer from 'inquirer' - import { type DeviceProfile, DeviceProfileStatus } from '@smartthings/core-sdk' +import type { optionalStringInput, stringInput } from '../../../../lib/user-query.js' import type { APICommand } from '../../../../lib/command/api-command.js' import type { fileInputProcessor, InputProcessor } from '../../../../lib/command/input-processor.js' import type { selectFromList } from '../../../../lib/command/select.js' import type { chooseDeviceProfile } from '../../../../lib/command/util/deviceprofiles-choose.js' -const promptMock = jest.fn() -jest.unstable_mockModule('inquirer', () => ({ - default: { - prompt: promptMock, - }, +const optionalStringInputMock = jest.fn() +const stringInputMock = jest.fn() +jest.unstable_mockModule('../../../../lib/user-query.js', () => ({ + optionalStringInput: optionalStringInputMock, + stringInput: stringInputMock, })) const fileInputProcessorMock = jest.fn>() @@ -50,23 +49,19 @@ const command = { client } as unknown as APICommand describe('chooseDeviceName function', () => { test('choose with from prompt', async () => { - promptMock.mockResolvedValue({ deviceName: 'Device Name' }) + optionalStringInputMock.mockResolvedValue('Device Name') const value = await chooseDeviceName() - expect(promptMock).toHaveBeenCalledTimes(1) - expect(promptMock).toHaveBeenCalledWith({ - type: 'input', name: 'deviceName', - message: 'Device Name:', - }) + expect(optionalStringInputMock).toHaveBeenCalledExactlyOnceWith('Device Name:') expect(value).toBeDefined() expect(value).toBe('Device Name') }) test('choose with default', async () => { - promptMock.mockResolvedValue({ deviceName: 'Another Device Name' }) + optionalStringInputMock.mockResolvedValue('Another Device Name') const value = await chooseDeviceName('Device Name') - expect(promptMock).toHaveBeenCalledTimes(0) + expect(optionalStringInputMock).not.toHaveBeenCalled() expect(value).toBeDefined() expect(value).toBe('Device Name') }) @@ -296,7 +291,7 @@ describe('chooseDevicePrototype function', () => { }) test('numeric value', async () => { - promptMock.mockResolvedValue({ value: '72' }) + stringInputMock.mockResolvedValue('72') const attribute = { schema: { type: 'object', @@ -321,11 +316,7 @@ describe('chooseDevicePrototype function', () => { const value = await chooseValue(command, attribute, 'temperature') expect(selectFromListMock).toHaveBeenCalledTimes(0) - expect(promptMock).toHaveBeenCalledTimes(1) - expect(promptMock).toHaveBeenCalledWith({ - type: 'input', name: 'value', - message: 'Enter \'temperature\' attribute value:', - }) + expect(stringInputMock).toHaveBeenCalledExactlyOnceWith('Enter \'temperature\' attribute value:') expect(value).toBeDefined() expect(value).toBe('72') }) diff --git a/src/lib/command/util/virtualdevices.ts b/src/lib/command/util/virtualdevices.ts index 801345f1..fd74101a 100644 --- a/src/lib/command/util/virtualdevices.ts +++ b/src/lib/command/util/virtualdevices.ts @@ -1,5 +1,3 @@ -import inquirer from 'inquirer' - import { type CapabilityAttribute, type CapabilityReference, @@ -8,6 +6,7 @@ import { type DeviceProfileCreateRequest, } from '@smartthings/core-sdk' +import { optionalStringInput, stringInput } from '../../user-query.js' import { type APICommand } from '../api-command.js' import { fileInputProcessor } from '../input-processor.js' import { selectFromList, type SelectFromListConfig } from '../select.js' @@ -67,14 +66,11 @@ export type DeviceProfileDefinition = { } export const chooseDeviceName = async (preselectedName?: string): Promise => { - if (!preselectedName) { - preselectedName = (await inquirer.prompt({ - type: 'input', - name: 'deviceName', - message: 'Device Name:', - })).deviceName + if (preselectedName) { + return preselectedName } - return preselectedName + + return optionalStringInput('Device Name:') } export const chooseDeviceProfileDefinition = async ( @@ -208,7 +204,6 @@ export const chooseValue = async ( attribute: CapabilityAttribute, name: string, ): Promise => { - let value const values = attribute.schema.properties.value.enum if (values) { const config: SelectFromListConfig = { @@ -222,13 +217,7 @@ export const chooseValue = async ( return { value } })) - value = await selectFromList(command, config, { listItems }) - } else { - value = (await inquirer.prompt({ - type: 'input', - name: 'value', - message: `Enter '${name}' attribute value:`, - })).value + return selectFromList(command, config, { listItems }) } - return value + return stringInput(`Enter '${name}' attribute value:`) }