Skip to content

Commit a021d5c

Browse files
committed
miscellaneous enhancements to createChooseFn
1 parent 7d50f47 commit a021d5c

28 files changed

+270
-175
lines changed

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

+12-9
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,17 @@ import {
1010
} from '@smartthings/core-sdk'
1111

1212
import type { addPermission } from '../../../../lib/aws-util.js'
13-
import {
13+
import type {
1414
PropertyTableFieldDefinition,
1515
TableGenerator,
1616
ValueTableFieldDefinition,
1717
} from '../../../../lib/table-generator.js'
1818
import type { fatalError } from '../../../../lib/util.js'
19-
import { stringTranslateToId } from '../../../../lib/command/command-util.js'
20-
import {
19+
import type { APICommand } from '../../../../lib/command/api-command.js'
20+
import type { stringTranslateToId } from '../../../../lib/command/command-util.js'
21+
import type {
2122
createChooseFn,
22-
type ChooseFunction,
23+
ChooseFunction,
2324
} from '../../../../lib/command/util/util-util.js'
2425
import {
2526
mockedTableOutput,
@@ -181,13 +182,15 @@ test('chooseAppFn uses correct endpoint to list apps', async () => {
181182
const apiAppsListMock = jest.fn<typeof AppsEndpoint.prototype.list>()
182183
.mockResolvedValueOnce(appList)
183184
const listItems = createChooseFnMock.mock.calls[0][1]
184-
const client = {
185-
apps: {
186-
list: apiAppsListMock,
185+
const command = {
186+
client: {
187+
apps: {
188+
list: apiAppsListMock,
189+
},
187190
},
188-
} as unknown as SmartThingsClient
191+
} as unknown as APICommand
189192

190-
expect(await listItems(client)).toBe(appList)
193+
expect(await listItems(command)).toBe(appList)
191194

192195
expect(apiAppsListMock).toHaveBeenCalledExactlyOnceWith()
193196
})

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

+9-10
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
import { jest } from '@jest/globals'
22

3-
import type {
4-
DevicePreference,
5-
DevicePreferencesEndpoint,
6-
SmartThingsClient,
7-
} from '@smartthings/core-sdk'
3+
import type { DevicePreference, DevicePreferencesEndpoint } from '@smartthings/core-sdk'
84

95
import type { ValueTableFieldDefinition } from '../../../../lib/table-generator.js'
6+
import type { APICommand } from '../../../../lib/command/api-command.js'
107
import type { ChooseFunction, createChooseFn } from '../../../../lib/command/util/util-util.js'
118

129

@@ -58,13 +55,15 @@ test('chooseDevicePreference', async () => {
5855
const apiDevicePreferencesListMock = jest.fn<typeof DevicePreferencesEndpoint.prototype.list>()
5956
.mockResolvedValueOnce(devicePreferenceList)
6057
const listItems = createChooseFnMock.mock.calls[0][1]
61-
const client = {
62-
devicePreferences: {
63-
list: apiDevicePreferencesListMock,
58+
const command = {
59+
client: {
60+
devicePreferences: {
61+
list: apiDevicePreferencesListMock,
62+
},
6463
},
65-
} as unknown as SmartThingsClient
64+
} as unknown as APICommand
6665

67-
expect(await listItems(client)).toBe(devicePreferenceList)
66+
expect(await listItems(command)).toBe(devicePreferenceList)
6867

6968
expect(apiDevicePreferencesListMock).toHaveBeenCalledExactlyOnceWith()
7069
})

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

+13-11
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ import type {
44
DeviceProfile,
55
DeviceProfilesEndpoint,
66
LocaleReference,
7-
SmartThingsClient,
87
} from '@smartthings/core-sdk'
98

109
import type { WithLocales } from '../../../../lib/api-helpers.js'
11-
import { createChooseFn, ChooseFunction } from '../../../../lib/command/util/util-util.js'
10+
import type { APICommand } from '../../../../lib/command/api-command.js'
11+
import { type createChooseFn, ChooseFunction } from '../../../../lib/command/util/util-util.js'
1212

1313

1414
const createChooseFnMock = jest.fn<typeof createChooseFn<DeviceProfile & WithLocales>>()
@@ -29,12 +29,14 @@ describe('chooseDeviceProfileFn', () => {
2929
const locales = [{ tag: 'es_MX' }, { tag: 'en_CA' }, { tag: 'fr_CA' }] as LocaleReference[]
3030
const apiDeviceProfilesListLocalesMock = jest.fn<typeof DeviceProfilesEndpoint.prototype.listLocales>()
3131
.mockResolvedValue(locales)
32-
const client = {
33-
deviceProfiles: {
34-
list: apiDeviceProfilesListMock,
35-
listLocales: apiDeviceProfilesListLocalesMock,
32+
const command = {
33+
client: {
34+
deviceProfiles: {
35+
list: apiDeviceProfilesListMock,
36+
listLocales: apiDeviceProfilesListLocalesMock,
37+
},
3638
},
37-
} as unknown as SmartThingsClient
39+
} as unknown as APICommand
3840

3941
it('uses correct endpoint to list device profiles', async () => {
4042
const chooseDeviceProfile = chooseDeviceProfileFn()
@@ -51,7 +53,7 @@ describe('chooseDeviceProfileFn', () => {
5153

5254
const listItems = createChooseFnMock.mock.calls[0][1]
5355

54-
expect(await listItems(client)).toBe(deviceProfiles)
56+
expect(await listItems(command)).toBe(deviceProfiles)
5557

5658
expect(apiDeviceProfilesListMock).toHaveBeenCalledExactlyOnceWith()
5759

@@ -73,7 +75,7 @@ describe('chooseDeviceProfileFn', () => {
7375

7476
const listItems = createChooseFnMock.mock.calls[0][1]
7577

76-
expect(await listItems(client)).toStrictEqual([{
78+
expect(await listItems(command)).toStrictEqual([{
7779
id: 'device-profile-id',
7880
locales: 'en_CA, es_MX, fr_CA',
7981
}])
@@ -99,7 +101,7 @@ describe('chooseDeviceProfileFn', () => {
99101
const listItems = createChooseFnMock.mock.calls[0][1]
100102
apiDeviceProfilesListLocalesMock.mockImplementationOnce(() => { throw { message: 'status code 404' } })
101103

102-
expect(await listItems(client)).toStrictEqual([{
104+
expect(await listItems(command)).toStrictEqual([{
103105
id: 'device-profile-id',
104106
}])
105107

@@ -123,7 +125,7 @@ describe('chooseDeviceProfileFn', () => {
123125
const listItems = createChooseFnMock.mock.calls[0][1]
124126
apiDeviceProfilesListLocalesMock.mockImplementationOnce(() => { throw Error('other error') })
125127

126-
await expect(listItems(client)).rejects.toThrow('other error')
128+
await expect(listItems(command)).rejects.toThrow('other error')
127129

128130
expect(apiDeviceProfilesListMock).toHaveBeenCalledExactlyOnceWith()
129131
expect(apiDeviceProfilesListLocalesMock).toHaveBeenCalledExactlyOnceWith('device-profile-id')

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

+15-14
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,15 @@ import type {
55
Device,
66
DeviceListOptions,
77
DevicesEndpoint,
8-
SmartThingsClient,
98
} from '@smartthings/core-sdk'
109

11-
import { APICommand } from '../../../../lib/command/api-command.js'
10+
import type { ValueTableFieldDefinition } from '../../../../lib/table-generator.js'
11+
import type { fatalError } from '../../../../lib/util.js'
12+
import type { APICommand } from '../../../../lib/command/api-command.js'
1213
import type { stringTranslateToId } from '../../../../lib/command/command-util.js'
13-
import { TableCommonListOutputProducer } from '../../../../lib/command/format.js'
14-
import { BuildOutputFormatterFlags } from '../../../../lib/command/output-builder.js'
14+
import type { TableCommonListOutputProducer } from '../../../../lib/command/format.js'
15+
import type { BuildOutputFormatterFlags } from '../../../../lib/command/output-builder.js'
1516
import type { createChooseFn, ChooseFunction } from '../../../../lib/command/util/util-util.js'
16-
import { ValueTableFieldDefinition } from '../../../../lib/table-generator.js'
17-
import { fatalError } from '../../../../lib/util.js'
1817

1918

2019
const stringTranslateToIdMock = jest.fn<typeof stringTranslateToId>()
@@ -43,11 +42,13 @@ describe('chooseDeviceFn', () => {
4342
const deviceList = [{ deviceId: 'listed-device-id' } as Device]
4443
const apiDevicesListMock = jest.fn<typeof DevicesEndpoint.prototype.list>()
4544
.mockResolvedValue(deviceList)
46-
const client = {
47-
devices: {
48-
list: apiDevicesListMock,
45+
const command = {
46+
client: {
47+
devices: {
48+
list: apiDevicesListMock,
49+
},
4950
},
50-
} as unknown as SmartThingsClient
51+
} as unknown as APICommand
5152

5253
it('uses correct endpoint to list devices', async () => {
5354
createChooseFnMock.mockReturnValueOnce(chooseDeviceMock)
@@ -63,7 +64,7 @@ describe('chooseDeviceFn', () => {
6364

6465
const listItems = createChooseFnMock.mock.calls[0][1]
6566

66-
expect(await listItems(client)).toBe(deviceList)
67+
expect(await listItems(command)).toBe(deviceList)
6768

6869
expect(apiDevicesListMock).toHaveBeenCalledExactlyOnceWith()
6970
})
@@ -83,7 +84,7 @@ describe('chooseDeviceFn', () => {
8384

8485
const listItems = createChooseFnMock.mock.calls[0][1]
8586

86-
expect(await listItems(client)).toBe(deviceList)
87+
expect(await listItems(command)).toBe(deviceList)
8788

8889
expect(apiDevicesListMock).toHaveBeenCalledExactlyOnceWith(deviceListOptions)
8990
})
@@ -111,8 +112,8 @@ describe('chooseComponentFn', () => {
111112

112113
const listItems = createChooseFnMockForComponent.mock.calls[0][1]
113114

114-
const client = {} as unknown as SmartThingsClient
115-
expect(await listItems(client)).toBe(components)
115+
const command = { client: {} } as unknown as APICommand
116+
expect(await listItems(command)).toBe(components)
116117
})
117118

118119
it('includes " (default)" for main component', async () => {

src/__tests__/lib/command/util/edge/channels-choose.test.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,13 @@ describe('chooseChannel', () => {
121121
)).toBe('chosen-channel-id')
122122

123123
expect(chooseOptionsWithDefaultsMock).toHaveBeenCalledExactlyOnceWith({ listItems: listItemsMock })
124-
expect(selectFromListMock).toHaveBeenCalledExactlyOnceWith(command,
124+
expect(selectFromListMock).toHaveBeenCalledExactlyOnceWith(
125+
command,
125126
expect.objectContaining({ primaryKeyName: 'channelId', sortKeyName: 'name' }),
126-
expect.objectContaining({ listItems: listItemsMock }))
127+
expect.objectContaining({ listItems: expect.any(Function) }),
128+
)
129+
130+
// expect listItemsMock to get called when listItems passed to third argument of selectFromListMock is called
127131
})
128132

129133
it('defaults to listChannels for listing channels', async () => {

src/__tests__/lib/command/util/edge/drivers-choose.test.ts

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { jest } from '@jest/globals'
22

3+
import type { EdgeDriver } from '@smartthings/core-sdk'
4+
5+
import type { APICommand } from '../../../../../lib/command/api-command.js'
36
import type { DriverChoice } from '../../../../../lib/command/util/drivers-choose.js'
47
import type { ChooseFunction, createChooseFn } from '../../../../../lib/command/util/util-util.js'
58
import type { listDrivers } from '../../../../../lib/command/util/edge/drivers-util.js'
6-
import { EdgeDriver, SmartThingsClient } from '@smartthings/core-sdk'
79

810

911
const createChooseFnMock = jest.fn<typeof createChooseFn<DriverChoice>>()
@@ -26,7 +28,7 @@ describe('createDriverFn', () => {
2628

2729
const drivers = [] as EdgeDriver[]
2830

29-
const client = { drivers: {} } as SmartThingsClient
31+
const command = { client: { drivers: {} } } as APICommand
3032

3133
it('does not include all organizations by default', async () => {
3234
expect(chooseDriverFn()).toBe(createDriverMock)
@@ -40,9 +42,9 @@ describe('createDriverFn', () => {
4042

4143
listDriversMock.mockResolvedValueOnce(drivers)
4244

43-
expect(await listItems(client)).toBe(drivers)
45+
expect(await listItems(command)).toBe(drivers)
4446

45-
expect(listDriversMock).toHaveBeenCalledExactlyOnceWith(client, undefined)
47+
expect(listDriversMock).toHaveBeenCalledExactlyOnceWith(command.client, undefined)
4648
})
4749

4850
it('requests all organizations when specified', async () => {
@@ -57,8 +59,8 @@ describe('createDriverFn', () => {
5759

5860
listDriversMock.mockResolvedValueOnce(drivers)
5961

60-
expect(await listItems(client)).toBe(drivers)
62+
expect(await listItems(command)).toBe(drivers)
6163

62-
expect(listDriversMock).toHaveBeenCalledExactlyOnceWith(client, true)
64+
expect(listDriversMock).toHaveBeenCalledExactlyOnceWith(command.client, true)
6365
})
6466
})

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

+10-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { jest } from '@jest/globals'
22

3-
import { DeviceIntegrationType, type Device, type DevicesEndpoint, type SmartThingsClient } from '@smartthings/core-sdk'
3+
import { DeviceIntegrationType, type Device, type DevicesEndpoint } from '@smartthings/core-sdk'
44

5+
import { APICommand } from '../../../../lib/command/api-command.js'
56
import { createChooseFn, ChooseFunction } from '../../../../lib/command/util/util-util.js'
67

78

@@ -20,11 +21,13 @@ describe('chooseHubFn', () => {
2021
const devices = [{ deviceId: 'hub-device-id' } as Device]
2122
const apiDevicesListMock = jest.fn<typeof DevicesEndpoint.prototype.list>()
2223
.mockResolvedValue(devices)
23-
const client = {
24-
devices: {
25-
list: apiDevicesListMock,
24+
const command = {
25+
client: {
26+
devices: {
27+
list: apiDevicesListMock,
28+
},
2629
},
27-
} as unknown as SmartThingsClient
30+
} as unknown as APICommand
2831

2932
it('limits to hub devices', async () => {
3033
const chooseHub = chooseHubFn()
@@ -40,7 +43,7 @@ describe('chooseHubFn', () => {
4043

4144
const listItems = createChooseFnMock.mock.calls[0][1]
4245

43-
expect(await listItems(client)).toBe(devices)
46+
expect(await listItems(command)).toBe(devices)
4447

4548
expect(apiDevicesListMock).toHaveBeenCalledExactlyOnceWith(
4649
{ type: DeviceIntegrationType.HUB, locationId: undefined },
@@ -54,7 +57,7 @@ describe('chooseHubFn', () => {
5457

5558
const listItems = createChooseFnMock.mock.calls[0][1]
5659

57-
expect(await listItems(client)).toBe(devices)
60+
expect(await listItems(command)).toBe(devices)
5861

5962
expect(apiDevicesListMock).toHaveBeenCalledExactlyOnceWith(
6063
{ type: DeviceIntegrationType.HUB, locationId: 'location-filter-id' },

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

+11-8
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { jest } from '@jest/globals'
22

3-
import { InstalledApp, InstalledAppListOptions, InstalledAppsEndpoint, type SmartThingsClient } from '@smartthings/core-sdk'
3+
import { InstalledApp, InstalledAppListOptions, InstalledAppsEndpoint } from '@smartthings/core-sdk'
44

55
import type { WithLocation, withLocations, WithNamedLocation } from '../../../../lib/api-helpers.js'
6+
import type { APICommand } from '../../../../lib/command/api-command.js'
67
import type { ChooseFunction, createChooseFn } from '../../../../lib/command/util/util-util.js'
78

89

@@ -26,11 +27,13 @@ describe('chooseInstalledAppFn', () => {
2627
const installedApps = [installedApp1, installedApp2]
2728
const apiInstalledAppsListMock = jest.fn<typeof InstalledAppsEndpoint.prototype.list>()
2829
.mockResolvedValue(installedApps)
29-
const client = {
30-
installedApps: {
31-
list: apiInstalledAppsListMock,
30+
const command = {
31+
client: {
32+
installedApps: {
33+
list: apiInstalledAppsListMock,
34+
},
3235
},
33-
} as unknown as SmartThingsClient
36+
} as unknown as APICommand
3437

3538
const chooseInstalledAppMock = jest.fn<ChooseFunction<InstalledApp & WithLocation>>()
3639
createChooseFnMock.mockReturnValue(chooseInstalledAppMock)
@@ -48,7 +51,7 @@ describe('chooseInstalledAppFn', () => {
4851

4952
const listFunction = createChooseFnMock.mock.calls[0][1]
5053

51-
expect(await listFunction(client)).toBe(installedApps)
54+
expect(await listFunction(command)).toBe(installedApps)
5255

5356
expect(apiInstalledAppsListMock).toHaveBeenCalledExactlyOnceWith(undefined)
5457
expect(withLocationsMock).not.toHaveBeenCalled()
@@ -74,9 +77,9 @@ describe('chooseInstalledAppFn', () => {
7477
]
7578
withLocationsMock.mockResolvedValue(installedAppsWithLocations)
7679

77-
expect(await listFunction(client)).toBe(installedAppsWithLocations)
80+
expect(await listFunction(command)).toBe(installedAppsWithLocations)
7881

7982
expect(apiInstalledAppsListMock).toHaveBeenCalledExactlyOnceWith(listOptions)
80-
expect(withLocationsMock).toHaveBeenCalledExactlyOnceWith(client, installedApps)
83+
expect(withLocationsMock).toHaveBeenCalledExactlyOnceWith(command.client, installedApps)
8184
})
8285
})

0 commit comments

Comments
 (0)