Skip to content

Commit da64393

Browse files
committed
refactor: add canned mock of exports from api-command
1 parent 42c0828 commit da64393

File tree

9 files changed

+68
-86
lines changed

9 files changed

+68
-86
lines changed

src/__tests__/commands/apps.test.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,18 @@ import { ArgumentsCamelCase, Argv, Options } from 'yargs'
44

55
import { AppClassification, AppResponse, AppType, AppsEndpoint, PagedApp, SmartThingsClient } from '@smartthings/core-sdk'
66

7-
import { APICommand, APICommandFlags, apiCommand, apiCommandBuilder, apiDocsURL } from '../../lib/command/api-command.js'
7+
import { APICommand, APICommandFlags } from '../../lib/command/api-command.js'
88
import { outputItemOrList, outputItemOrListBuilder } from '../../lib/command/listing-io.js'
99
import { CommandArgs } from '../../commands/apps.js'
1010
import { ListDataFunction } from '../../lib/command/io-defs.js'
1111
import { BuildOutputFormatterFlags } from '../../lib/command/output-builder.js'
1212
import { SmartThingsCommandFlags } from '../../lib/command/smartthings-command.js'
1313
import { shortARNorURL, verboseApps } from '../../lib/command/util/apps-util.js'
14+
import { apiCommandMocks } from '../test-lib/api-command-mock.js'
1415
import { buildArgvMock, buildArgvMockStub } from '../test-lib/builder-mock.js'
1516

1617

17-
18-
const apiCommandMock = jest.fn<typeof apiCommand>()
19-
const apiCommandBuilderMock = jest.fn<typeof apiCommandBuilder>()
20-
const apiDocsURLMock = jest.fn<typeof apiDocsURL>()
21-
jest.unstable_mockModule('../../lib/command/api-command.js', () => ({
22-
apiCommand: apiCommandMock,
23-
apiCommandBuilder: apiCommandBuilderMock,
24-
apiDocsURL: apiDocsURLMock,
25-
}))
18+
const { apiCommandMock, apiCommandBuilderMock } = apiCommandMocks('../..')
2619

2720
const outputItemOrListMock = jest.fn<typeof outputItemOrList<PagedApp | AppResponse>>()
2821
const outputItemOrListBuilderMock = jest.fn<typeof outputItemOrListBuilder>()

src/__tests__/commands/devices/capability-status.test.ts

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,7 @@ import type {
1010
} from '@smartthings/core-sdk'
1111

1212
import type { CommandArgs } from '../../../commands/devices/capability-status.js'
13-
import type {
14-
APICommand,
15-
apiCommand,
16-
apiCommandBuilder,
17-
apiDocsURL,
18-
} from '../../../lib/command/api-command.js'
13+
import type { APICommand } from '../../../lib/command/api-command.js'
1914
import type { stringTranslateToId } from '../../../lib/command/command-util.js'
2015
import type {
2116
CustomCommonOutputProducer,
@@ -40,16 +35,10 @@ import {
4035
tableToStringMock,
4136
} from '../../test-lib/table-mock.js'
4237
import type { fatalError } from '../../../lib/util.js'
38+
import { apiCommandMocks } from '../../test-lib/api-command-mock.js'
4339

4440

45-
const apiCommandMock = jest.fn<typeof apiCommand>()
46-
const apiCommandBuilderMock = jest.fn<typeof apiCommandBuilder>()
47-
const apiDocsURLMock = jest.fn<typeof apiDocsURL>()
48-
jest.unstable_mockModule('../../../lib/command/api-command.js', () => ({
49-
apiCommand: apiCommandMock,
50-
apiCommandBuilder: apiCommandBuilderMock,
51-
apiDocsURL: apiDocsURLMock,
52-
}))
41+
const { apiCommandMock, apiCommandBuilderMock } = apiCommandMocks('../../..')
5342

5443
const stringTranslateToIdMock = jest.fn<typeof stringTranslateToId>()
5544
jest.unstable_mockModule('../../../lib/command/command-util.js', () => ({

src/__tests__/commands/devices/preferences.test.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { ArgumentsCamelCase, Argv } from 'yargs'
44

55
import type { Device, DevicePreferenceResponse, DevicesEndpoint } from '@smartthings/core-sdk'
66

7-
import type { APICommand, apiCommand, apiCommandBuilder } from '../../../lib/command/api-command.js'
7+
import type { APICommand } from '../../../lib/command/api-command.js'
88
import type {
99
CustomCommonOutputProducer,
1010
formatAndWriteItem,
@@ -14,6 +14,7 @@ import type { CommandArgs } from '../../../commands/apps.js'
1414
import type { BuildOutputFormatterFlags } from '../.././../lib/command/output-builder.js'
1515
import type { SmartThingsCommandFlags } from '../../../lib/command/smartthings-command.js'
1616
import type { ChooseFunction } from '../../../lib/command/util/util-util.js'
17+
import { apiCommandMocks } from '../../test-lib/api-command-mock.js'
1718
import { buildArgvMock, buildArgvMockStub } from '../../test-lib/builder-mock.js'
1819
import {
1920
mockedTableOutput,
@@ -24,12 +25,7 @@ import {
2425
} from '../../test-lib/table-mock.js'
2526

2627

27-
const apiCommandMock = jest.fn<typeof apiCommand>()
28-
const apiCommandBuilderMock = jest.fn<typeof apiCommandBuilder>()
29-
jest.unstable_mockModule('../../../lib/command/api-command.js', () => ({
30-
apiCommand: apiCommandMock,
31-
apiCommandBuilder: apiCommandBuilderMock,
32-
}))
28+
const { apiCommandMock, apiCommandBuilderMock } = apiCommandMocks('../../..')
3329

3430
const chooseDeviceMock = jest.fn<ChooseFunction<Device>>()
3531
jest.unstable_mockModule('../../../lib/command/util/devices-util.js', () => ({

src/__tests__/commands/locations.test.ts

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

3-
import { ArgumentsCamelCase, Argv } from 'yargs'
3+
import type { ArgumentsCamelCase, Argv } from 'yargs'
44

5-
import { Location, LocationsEndpoint, SmartThingsClient } from '@smartthings/core-sdk'
5+
import type { Location, LocationsEndpoint, SmartThingsClient } from '@smartthings/core-sdk'
66

7-
import { APICommand, APICommandFlags, apiCommand, apiCommandBuilder, apiDocsURL } from '../../lib/command/api-command.js'
8-
import { outputItemOrList, outputItemOrListBuilder } from '../../lib/command/listing-io.js'
9-
import { CommandArgs } from '../../commands/locations.js'
7+
import type { APICommand, APICommandFlags } from '../../lib/command/api-command.js'
8+
import type { outputItemOrList, outputItemOrListBuilder } from '../../lib/command/listing-io.js'
9+
import type { CommandArgs } from '../../commands/locations.js'
10+
import { apiCommandMocks } from '../test-lib/api-command-mock.js'
1011
import { buildArgvMock, buildArgvMockStub } from '../test-lib/builder-mock.js'
1112

1213

13-
14-
const apiCommandMock = jest.fn<typeof apiCommand>()
15-
const apiCommandBuilderMock = jest.fn<typeof apiCommandBuilder>()
16-
const apiDocsURLMock = jest.fn<typeof apiDocsURL>()
17-
jest.unstable_mockModule('../../lib/command/api-command.js', () => ({
18-
apiCommand: apiCommandMock,
19-
apiCommandBuilder: apiCommandBuilderMock,
20-
apiDocsURL: apiDocsURLMock,
21-
}))
14+
const { apiCommandMock, apiCommandBuilderMock } = apiCommandMocks('../..')
2215

2316
const outputItemOrListMock = jest.fn<typeof outputItemOrList>()
2417
const outputItemOrListBuilderMock = jest.fn<typeof outputItemOrListBuilder>()
@@ -28,7 +21,6 @@ jest.unstable_mockModule('../../lib/command/listing-io.js', () => ({
2821
}))
2922

3023

31-
3224
const { default: cmd } = await import('../../commands/locations.js')
3325

3426

src/__tests__/commands/locations/create.test.ts

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,18 @@ import { ArgumentsCamelCase, Argv } from 'yargs'
55
import { Location, LocationCreate, LocationsEndpoint, SmartThingsClient } from '@smartthings/core-sdk'
66

77
import { tableFieldDefinitions } from '../../../lib/command/util/locations-util.js'
8-
import {
9-
APICommand,
10-
APICommandFlags,
11-
apiCommand,
12-
apiCommandBuilder,
13-
apiDocsURL,
14-
} from '../../../lib/command/api-command.js'
8+
import { APICommand, APICommandFlags } from '../../../lib/command/api-command.js'
159
import { inputAndOutputItem, inputAndOutputItemBuilder } from '../../../lib/command/input-and-output-item.js'
1610
import { CommandArgs } from '../../../commands/locations/create.js'
11+
import { apiCommandMocks } from '../../test-lib/api-command-mock.js'
1712
import { buildArgvMock, buildArgvMockStub } from '../../test-lib/builder-mock.js'
1813

1914

2015
jest.unstable_mockModule('../../../lib/command/util/locations-util.js', () => ({
2116
tableFieldDefinitions,
2217
}))
2318

24-
const apiCommandMock = jest.fn<typeof apiCommand>()
25-
const apiCommandBuilderMock = jest.fn<typeof apiCommandBuilder>()
26-
const apiDocsURLMock = jest.fn<typeof apiDocsURL>()
27-
jest.unstable_mockModule('../../../lib/command/api-command.js', () => ({
28-
apiCommand: apiCommandMock,
29-
apiCommandBuilder: apiCommandBuilderMock,
30-
apiDocsURL: apiDocsURLMock,
31-
}))
19+
const { apiCommandMock, apiCommandBuilderMock } = apiCommandMocks('../../..')
3220

3321
const inputAndOutputItemMock = jest.fn<typeof inputAndOutputItem>()
3422
const inputAndOutputItemBuilderMock = jest.fn<typeof inputAndOutputItemBuilder>()

src/__tests__/commands/locations/delete.test.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,13 @@ import { ArgumentsCamelCase, Argv } from 'yargs'
55
import { LocationsEndpoint, SmartThingsClient } from '@smartthings/core-sdk'
66

77
import { CommandArgs } from '../../../commands/locations/delete.js'
8-
import { APICommand, APICommandFlags, apiCommand, apiCommandBuilder, apiDocsURL } from '../../../lib/command/api-command.js'
8+
import { APICommand, APICommandFlags } from '../../../lib/command/api-command.js'
99
import { chooseLocation } from '../../../lib/command/util/locations-util.js'
10+
import { apiCommandMocks } from '../../test-lib/api-command-mock.js'
1011
import { buildArgvMock } from '../../test-lib/builder-mock.js'
1112

1213

13-
const apiCommandMock = jest.fn<typeof apiCommand>()
14-
const apiCommandBuilderMock = jest.fn<typeof apiCommandBuilder>()
15-
const apiDocsURLMock = jest.fn<typeof apiDocsURL>()
16-
jest.unstable_mockModule('../../../lib/command/api-command.js', () => ({
17-
apiCommand: apiCommandMock,
18-
apiCommandBuilder: apiCommandBuilderMock,
19-
apiDocsURL: apiDocsURLMock,
20-
}))
14+
const { apiCommandMock, apiCommandBuilderMock } = apiCommandMocks('../../..')
2115

2216
const chooseLocationMock = jest.fn<typeof chooseLocation>()
2317
jest.unstable_mockModule('../../../lib/command/util/locations-util.js', () => ({

src/__tests__/commands/locations/update.test.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ import { ArgumentsCamelCase, Argv } from 'yargs'
55
import { Location, LocationUpdate, LocationsEndpoint, SmartThingsClient } from '@smartthings/core-sdk'
66

77
import { chooseLocation, tableFieldDefinitions } from '../../../lib/command/util/locations-util.js'
8-
import { APICommand, APICommandFlags, apiCommand, apiCommandBuilder, apiDocsURL } from '../../../lib/command/api-command.js'
8+
import { APICommand, APICommandFlags } from '../../../lib/command/api-command.js'
99
import { inputAndOutputItem, inputAndOutputItemBuilder } from '../../../lib/command/input-and-output-item.js'
1010
import { CommandArgs } from '../../../commands/locations/update.js'
11+
import { apiCommandMocks } from '../../test-lib/api-command-mock.js'
1112
import { buildArgvMock, buildArgvMockStub } from '../../test-lib/builder-mock.js'
1213

1314

@@ -17,14 +18,7 @@ jest.unstable_mockModule('../../../lib/command/util/locations-util.js', () => ({
1718
tableFieldDefinitions,
1819
}))
1920

20-
const apiCommandMock = jest.fn<typeof apiCommand>()
21-
const apiCommandBuilderMock = jest.fn<typeof apiCommandBuilder>()
22-
const apiDocsURLMock = jest.fn<typeof apiDocsURL>()
23-
jest.unstable_mockModule('../../../lib/command/api-command.js', () => ({
24-
apiCommand: apiCommandMock,
25-
apiCommandBuilder: apiCommandBuilderMock,
26-
apiDocsURL: apiDocsURLMock,
27-
}))
21+
const { apiCommandMock, apiCommandBuilderMock } = apiCommandMocks('../../..')
2822

2923
const inputAndOutputItemMock = jest.fn<typeof inputAndOutputItem>()
3024
const inputAndOutputItemBuilderMock = jest.fn<typeof inputAndOutputItemBuilder>()

src/__tests__/lib/command/api-organization-command.test.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { jest } from '@jest/globals'
22

33
import { CLIConfig } from '../../../lib/cli-config.js'
4-
import { APICommand, apiCommand, apiCommandBuilder } from '../../../lib/command/api-command.js'
4+
import { APICommand } from '../../../lib/command/api-command.js'
55
import { APIOrganizationCommandFlags } from '../../../lib/command/api-organization-command.js'
66
import { SmartThingsCommandFlags } from '../../../lib/command/smartthings-command.js'
7+
import { apiCommandMocks } from '../../test-lib/api-command-mock.js'
78
import { buildArgvMock } from '../../test-lib/builder-mock.js'
89

910

@@ -15,13 +16,9 @@ const apiCommandResultMock = {
1516
},
1617
profileName: 'profile-from-parent',
1718
} as unknown as APICommand
18-
const apiCommandBuilderMock = jest.fn<typeof apiCommandBuilder>()
19-
const apiCommandMock = jest.fn<typeof apiCommand>()
19+
20+
const { apiCommandMock, apiCommandBuilderMock } = apiCommandMocks('../../..')
2021
apiCommandMock.mockResolvedValue(apiCommandResultMock)
21-
jest.unstable_mockModule('../../../lib/command/api-command.js', () => ({
22-
apiCommandBuilder: apiCommandBuilderMock,
23-
apiCommand: apiCommandMock,
24-
}))
2522

2623
const {
2724
apiOrganizationCommand,
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { jest } from '@jest/globals'
2+
3+
import type { Argv } from 'yargs'
4+
5+
import {
6+
userAgent,
7+
type APICommandFlags,
8+
type apiCommand,
9+
type apiDocsURL,
10+
type itemInputHelpText,
11+
} from '../../lib/command/api-command.js'
12+
import { buildArgvMockStub, type BuilderFunctionMock } from './builder-mock.js'
13+
14+
15+
export const apiCommandMocks = <T extends APICommandFlags = APICommandFlags>(prefix: string): {
16+
apiDocsURLMock: jest.Mock<typeof apiDocsURL>
17+
itemInputHelpTextMock: jest.Mock<typeof itemInputHelpText>
18+
apiCommandMock: jest.Mock<typeof apiCommand>
19+
apiCommandBuilderMock: BuilderFunctionMock<Argv<T>>
20+
} => {
21+
const apiDocsURLMock = jest.fn<typeof apiDocsURL>()
22+
const itemInputHelpTextMock = jest.fn<typeof itemInputHelpText>()
23+
const apiCommandBuilderMock = buildArgvMockStub<T>()
24+
const apiCommandMock = jest.fn<typeof apiCommand>()
25+
jest.unstable_mockModule(`${prefix}/lib/command/api-command.js`, () => ({
26+
userAgent,
27+
apiDocsURL: apiDocsURLMock,
28+
itemInputHelpText: itemInputHelpTextMock,
29+
apiCommandBuilder: apiCommandBuilderMock,
30+
apiCommand: apiCommandMock,
31+
}))
32+
33+
return {
34+
apiDocsURLMock,
35+
itemInputHelpTextMock,
36+
apiCommandBuilderMock,
37+
apiCommandMock,
38+
}
39+
}

0 commit comments

Comments
 (0)