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
13 changes: 3 additions & 10 deletions src/__tests__/commands/apps.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,18 @@ import { ArgumentsCamelCase, Argv, Options } from 'yargs'

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

import { APICommand, APICommandFlags, apiCommand, apiCommandBuilder, apiDocsURL } from '../../lib/command/api-command.js'
import { APICommand, APICommandFlags } from '../../lib/command/api-command.js'
import { outputItemOrList, outputItemOrListBuilder } from '../../lib/command/listing-io.js'
import { CommandArgs } from '../../commands/apps.js'
import { ListDataFunction } from '../../lib/command/io-defs.js'
import { BuildOutputFormatterFlags } from '../../lib/command/output-builder.js'
import { SmartThingsCommandFlags } from '../../lib/command/smartthings-command.js'
import { shortARNorURL, verboseApps } from '../../lib/command/util/apps-util.js'
import { apiCommandMocks } from '../test-lib/api-command-mock.js'
import { buildArgvMock, buildArgvMockStub } from '../test-lib/builder-mock.js'



const apiCommandMock = jest.fn<typeof apiCommand>()
const apiCommandBuilderMock = jest.fn<typeof apiCommandBuilder>()
const apiDocsURLMock = jest.fn<typeof apiDocsURL>()
jest.unstable_mockModule('../../lib/command/api-command.js', () => ({
apiCommand: apiCommandMock,
apiCommandBuilder: apiCommandBuilderMock,
apiDocsURL: apiDocsURLMock,
}))
const { apiCommandMock, apiCommandBuilderMock } = apiCommandMocks('../..')
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't love that we need to pass in the directory prefix here but jest.unstable_mockModules works relative to the test itself. There might be a way to determine this in the module but I haven't found anything yet.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see a better way either. I suppose you could at least set a default (probably of ../../..) so you don't always have to pass it?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had considered that but decided against it because I was afraid someone (like me 😄) might cut-and-paste one without the value specified and wonder why it didn't work.

I've also just realized the small sample of use-cases we have right now might not be representative.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking about this for a nanosecond longer and I'm realizing we probably do have more commands at one level deep overall so maybe we should make a default of ../../..? 🤷


const outputItemOrListMock = jest.fn<typeof outputItemOrList<PagedApp | AppResponse>>()
const outputItemOrListBuilderMock = jest.fn<typeof outputItemOrListBuilder>()
Expand Down
17 changes: 3 additions & 14 deletions src/__tests__/commands/devices/capability-status.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,7 @@ import type {
} from '@smartthings/core-sdk'

import type { CommandArgs } from '../../../commands/devices/capability-status.js'
import type {
APICommand,
apiCommand,
apiCommandBuilder,
apiDocsURL,
} from '../../../lib/command/api-command.js'
import type { APICommand } from '../../../lib/command/api-command.js'
import type { stringTranslateToId } from '../../../lib/command/command-util.js'
import type {
CustomCommonOutputProducer,
Expand All @@ -40,16 +35,10 @@ import {
tableToStringMock,
} from '../../test-lib/table-mock.js'
import type { fatalError } from '../../../lib/util.js'
import { apiCommandMocks } from '../../test-lib/api-command-mock.js'


const apiCommandMock = jest.fn<typeof apiCommand>()
const apiCommandBuilderMock = jest.fn<typeof apiCommandBuilder>()
const apiDocsURLMock = jest.fn<typeof apiDocsURL>()
jest.unstable_mockModule('../../../lib/command/api-command.js', () => ({
apiCommand: apiCommandMock,
apiCommandBuilder: apiCommandBuilderMock,
apiDocsURL: apiDocsURLMock,
}))
const { apiCommandMock, apiCommandBuilderMock } = apiCommandMocks('../../..')

const stringTranslateToIdMock = jest.fn<typeof stringTranslateToId>()
jest.unstable_mockModule('../../../lib/command/command-util.js', () => ({
Expand Down
10 changes: 3 additions & 7 deletions src/__tests__/commands/devices/preferences.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { ArgumentsCamelCase, Argv } from 'yargs'

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

import type { APICommand, apiCommand, apiCommandBuilder } from '../../../lib/command/api-command.js'
import type { APICommand } from '../../../lib/command/api-command.js'
import type {
CustomCommonOutputProducer,
formatAndWriteItem,
Expand All @@ -14,6 +14,7 @@ import type { CommandArgs } from '../../../commands/apps.js'
import type { BuildOutputFormatterFlags } from '../.././../lib/command/output-builder.js'
import type { SmartThingsCommandFlags } from '../../../lib/command/smartthings-command.js'
import type { ChooseFunction } from '../../../lib/command/util/util-util.js'
import { apiCommandMocks } from '../../test-lib/api-command-mock.js'
import { buildArgvMock, buildArgvMockStub } from '../../test-lib/builder-mock.js'
import {
mockedTableOutput,
Expand All @@ -24,12 +25,7 @@ import {
} from '../../test-lib/table-mock.js'


const apiCommandMock = jest.fn<typeof apiCommand>()
const apiCommandBuilderMock = jest.fn<typeof apiCommandBuilder>()
jest.unstable_mockModule('../../../lib/command/api-command.js', () => ({
apiCommand: apiCommandMock,
apiCommandBuilder: apiCommandBuilderMock,
}))
const { apiCommandMock, apiCommandBuilderMock } = apiCommandMocks('../../..')

const chooseDeviceMock = jest.fn<ChooseFunction<Device>>()
jest.unstable_mockModule('../../../lib/command/util/devices-util.js', () => ({
Expand Down
22 changes: 7 additions & 15 deletions src/__tests__/commands/locations.test.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,17 @@
import { jest } from '@jest/globals'

import { ArgumentsCamelCase, Argv } from 'yargs'
import type { ArgumentsCamelCase, Argv } from 'yargs'

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

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



const apiCommandMock = jest.fn<typeof apiCommand>()
const apiCommandBuilderMock = jest.fn<typeof apiCommandBuilder>()
const apiDocsURLMock = jest.fn<typeof apiDocsURL>()
jest.unstable_mockModule('../../lib/command/api-command.js', () => ({
apiCommand: apiCommandMock,
apiCommandBuilder: apiCommandBuilderMock,
apiDocsURL: apiDocsURLMock,
}))
const { apiCommandMock, apiCommandBuilderMock } = apiCommandMocks('../..')

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



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


Expand Down
18 changes: 3 additions & 15 deletions src/__tests__/commands/locations/create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,18 @@ import { ArgumentsCamelCase, Argv } from 'yargs'
import { Location, LocationCreate, LocationsEndpoint, SmartThingsClient } from '@smartthings/core-sdk'

import { tableFieldDefinitions } from '../../../lib/command/util/locations-util.js'
import {
APICommand,
APICommandFlags,
apiCommand,
apiCommandBuilder,
apiDocsURL,
} from '../../../lib/command/api-command.js'
import { APICommand, APICommandFlags } from '../../../lib/command/api-command.js'
import { inputAndOutputItem, inputAndOutputItemBuilder } from '../../../lib/command/input-and-output-item.js'
import { CommandArgs } from '../../../commands/locations/create.js'
import { apiCommandMocks } from '../../test-lib/api-command-mock.js'
import { buildArgvMock, buildArgvMockStub } from '../../test-lib/builder-mock.js'


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

const apiCommandMock = jest.fn<typeof apiCommand>()
const apiCommandBuilderMock = jest.fn<typeof apiCommandBuilder>()
const apiDocsURLMock = jest.fn<typeof apiDocsURL>()
jest.unstable_mockModule('../../../lib/command/api-command.js', () => ({
apiCommand: apiCommandMock,
apiCommandBuilder: apiCommandBuilderMock,
apiDocsURL: apiDocsURLMock,
}))
const { apiCommandMock, apiCommandBuilderMock } = apiCommandMocks('../../..')

const inputAndOutputItemMock = jest.fn<typeof inputAndOutputItem>()
const inputAndOutputItemBuilderMock = jest.fn<typeof inputAndOutputItemBuilder>()
Expand Down
12 changes: 3 additions & 9 deletions src/__tests__/commands/locations/delete.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,13 @@ import { ArgumentsCamelCase, Argv } from 'yargs'
import { LocationsEndpoint, SmartThingsClient } from '@smartthings/core-sdk'

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


const apiCommandMock = jest.fn<typeof apiCommand>()
const apiCommandBuilderMock = jest.fn<typeof apiCommandBuilder>()
const apiDocsURLMock = jest.fn<typeof apiDocsURL>()
jest.unstable_mockModule('../../../lib/command/api-command.js', () => ({
apiCommand: apiCommandMock,
apiCommandBuilder: apiCommandBuilderMock,
apiDocsURL: apiDocsURLMock,
}))
const { apiCommandMock, apiCommandBuilderMock } = apiCommandMocks('../../..')

const chooseLocationMock = jest.fn<typeof chooseLocation>()
jest.unstable_mockModule('../../../lib/command/util/locations-util.js', () => ({
Expand Down
12 changes: 3 additions & 9 deletions src/__tests__/commands/locations/update.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import { ArgumentsCamelCase, Argv } from 'yargs'
import { Location, LocationUpdate, LocationsEndpoint, SmartThingsClient } from '@smartthings/core-sdk'

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


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

const apiCommandMock = jest.fn<typeof apiCommand>()
const apiCommandBuilderMock = jest.fn<typeof apiCommandBuilder>()
const apiDocsURLMock = jest.fn<typeof apiDocsURL>()
jest.unstable_mockModule('../../../lib/command/api-command.js', () => ({
apiCommand: apiCommandMock,
apiCommandBuilder: apiCommandBuilderMock,
apiDocsURL: apiDocsURLMock,
}))
const { apiCommandMock, apiCommandBuilderMock } = apiCommandMocks('../../..')

const inputAndOutputItemMock = jest.fn<typeof inputAndOutputItem>()
const inputAndOutputItemBuilderMock = jest.fn<typeof inputAndOutputItemBuilder>()
Expand Down
11 changes: 4 additions & 7 deletions src/__tests__/lib/command/api-organization-command.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { jest } from '@jest/globals'

import { CLIConfig } from '../../../lib/cli-config.js'
import { APICommand, apiCommand, apiCommandBuilder } from '../../../lib/command/api-command.js'
import { APICommand } from '../../../lib/command/api-command.js'
import { APIOrganizationCommandFlags } from '../../../lib/command/api-organization-command.js'
import { SmartThingsCommandFlags } from '../../../lib/command/smartthings-command.js'
import { apiCommandMocks } from '../../test-lib/api-command-mock.js'
import { buildArgvMock } from '../../test-lib/builder-mock.js'


Expand All @@ -15,13 +16,9 @@ const apiCommandResultMock = {
},
profileName: 'profile-from-parent',
} as unknown as APICommand
const apiCommandBuilderMock = jest.fn<typeof apiCommandBuilder>()
const apiCommandMock = jest.fn<typeof apiCommand>()

const { apiCommandMock, apiCommandBuilderMock } = apiCommandMocks('../../..')
apiCommandMock.mockResolvedValue(apiCommandResultMock)
jest.unstable_mockModule('../../../lib/command/api-command.js', () => ({
apiCommandBuilder: apiCommandBuilderMock,
apiCommand: apiCommandMock,
}))

const {
apiOrganizationCommand,
Expand Down
39 changes: 39 additions & 0 deletions src/__tests__/test-lib/api-command-mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { jest } from '@jest/globals'

import type { Argv } from 'yargs'

import {
userAgent,
type APICommandFlags,
type apiCommand,
type apiDocsURL,
type itemInputHelpText,
} from '../../lib/command/api-command.js'
import { buildArgvMockStub, type BuilderFunctionMock } from './builder-mock.js'


export const apiCommandMocks = <T extends APICommandFlags = APICommandFlags>(prefix: string): {
apiDocsURLMock: jest.Mock<typeof apiDocsURL>
itemInputHelpTextMock: jest.Mock<typeof itemInputHelpText>
apiCommandMock: jest.Mock<typeof apiCommand>
apiCommandBuilderMock: BuilderFunctionMock<Argv<T>>
} => {
const apiDocsURLMock = jest.fn<typeof apiDocsURL>()
const itemInputHelpTextMock = jest.fn<typeof itemInputHelpText>()
const apiCommandBuilderMock = buildArgvMockStub<T>()
const apiCommandMock = jest.fn<typeof apiCommand>()
jest.unstable_mockModule(`${prefix}/lib/command/api-command.js`, () => ({
userAgent,
apiDocsURL: apiDocsURLMock,
itemInputHelpText: itemInputHelpTextMock,
apiCommandBuilder: apiCommandBuilderMock,
apiCommand: apiCommandMock,
}))

return {
apiDocsURLMock,
itemInputHelpTextMock,
apiCommandBuilderMock,
apiCommandMock,
}
}
Loading