Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 12 additions & 21 deletions src/__tests__/lib/command/util/virtualdevices.test.ts
Original file line number Diff line number Diff line change
@@ -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<typeof inquirer.prompt>()
jest.unstable_mockModule('inquirer', () => ({
default: {
prompt: promptMock,
},
const optionalStringInputMock = jest.fn<typeof optionalStringInput>()
const stringInputMock = jest.fn<typeof stringInput>()
jest.unstable_mockModule('../../../../lib/user-query.js', () => ({
optionalStringInput: optionalStringInputMock,
stringInput: stringInputMock,
}))

const fileInputProcessorMock = jest.fn<typeof fileInputProcessor<DeviceProfile>>()
Expand Down Expand Up @@ -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')
})
Expand Down Expand Up @@ -296,7 +291,7 @@ describe('chooseDevicePrototype function', () => {
})

test('numeric value', async () => {
promptMock.mockResolvedValue({ value: '72' })
stringInputMock.mockResolvedValue('72')
const attribute = {
schema: {
type: 'object',
Expand All @@ -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')
})
Expand Down
25 changes: 7 additions & 18 deletions src/lib/command/util/virtualdevices.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import inquirer from 'inquirer'

import {
type CapabilityAttribute,
type CapabilityReference,
Expand All @@ -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'
Expand Down Expand Up @@ -67,14 +66,11 @@ export type DeviceProfileDefinition = {
}

export const chooseDeviceName = async (preselectedName?: string): Promise<string | undefined> => {
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 (
Expand Down Expand Up @@ -208,7 +204,6 @@ export const chooseValue = async (
attribute: CapabilityAttribute,
name: string,
): Promise<string> => {
let value
const values = attribute.schema.properties.value.enum
if (values) {
const config: SelectFromListConfig<CapabilityValueItem> = {
Expand All @@ -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:`)
}