forked from SmartThingsCommunity/smartthings-cli
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdelete.test.ts
More file actions
89 lines (69 loc) · 3.06 KB
/
delete.test.ts
File metadata and controls
89 lines (69 loc) · 3.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import { jest } from '@jest/globals'
import type { ArgumentsCamelCase, Argv } from 'yargs'
import type { DriversEndpoint } from '@smartthings/core-sdk'
import type { CommandArgs } from '../../../../commands/edge/drivers/delete.js'
import type { buildEpilog } from '../../../../lib/help.js'
import type {
APIOrganizationCommand,
apiOrganizationCommand,
apiOrganizationCommandBuilder,
APIOrganizationCommandFlags,
} from '../../../../lib/command/api-organization-command.js'
import type { chooseDriver } from '../../../../lib/command/util/drivers-choose.js'
import { buildArgvMock } from '../../../test-lib/builder-mock.js'
const buildEpilogMock = jest.fn<typeof buildEpilog>()
jest.unstable_mockModule('../../../../lib/help.js', () => ({
buildEpilog: buildEpilogMock,
}))
const apiOrganizationCommandMock = jest.fn<typeof apiOrganizationCommand>()
const apiOrganizationCommandBuilderMock = jest.fn<typeof apiOrganizationCommandBuilder>()
jest.unstable_mockModule('../../../../lib/command/api-organization-command.js', () => ({
apiOrganizationCommand: apiOrganizationCommandMock,
apiOrganizationCommandBuilder: apiOrganizationCommandBuilderMock,
}))
const chooseDriverMock = jest.fn<typeof chooseDriver>().mockResolvedValue('chosen-driver-id')
jest.unstable_mockModule('../../../../lib/command/util/drivers-choose.js', () => ({
chooseDriver: chooseDriverMock,
}))
const consoleLogSpy = jest.spyOn(console, 'log').mockImplementation(() => { /* do nothing */ })
const { default: cmd } = await import('../../../../commands/edge/drivers/delete.js')
test('builder', () => {
const {
yargsMock,
positionalMock,
optionMock,
exampleMock,
epilogMock,
argvMock,
} = buildArgvMock<APIOrganizationCommandFlags, CommandArgs>()
apiOrganizationCommandBuilderMock.mockReturnValue(argvMock)
const builder = cmd.builder as (yargs: Argv<object>) => Argv<CommandArgs>
expect(builder(yargsMock)).toBe(argvMock)
expect(apiOrganizationCommandBuilderMock).toHaveBeenCalledExactlyOnceWith(yargsMock)
expect(positionalMock).toHaveBeenCalledTimes(1)
expect(optionMock).toHaveBeenCalledTimes(0)
expect(exampleMock).toHaveBeenCalledTimes(1)
expect(buildEpilogMock).toHaveBeenCalledTimes(1)
expect(epilogMock).toHaveBeenCalledTimes(1)
})
test('handler', async () => {
const apiDriversDeleteMock = jest.fn<typeof DriversEndpoint.prototype.delete>()
const command = {
client: {
drivers: {
delete: apiDriversDeleteMock,
},
},
} as unknown as APIOrganizationCommand<ArgumentsCamelCase<CommandArgs>>
apiOrganizationCommandMock.mockResolvedValue(command)
const inputArgv = {
profile: 'default',
id: 'cmd-line-id',
} as ArgumentsCamelCase<CommandArgs>
await expect(cmd.handler(inputArgv)).resolves.not.toThrow()
expect(apiOrganizationCommandMock).toHaveBeenCalledExactlyOnceWith(inputArgv)
expect(chooseDriverMock)
.toHaveBeenCalledExactlyOnceWith(command, 'cmd-line-id', { promptMessage: 'Select a driver to delete.' })
expect(apiDriversDeleteMock).toHaveBeenCalledExactlyOnceWith('chosen-driver-id')
expect(consoleLogSpy).toHaveBeenCalledExactlyOnceWith('Driver chosen-driver-id deleted.')
})