Skip to content

Conversation

@bflorian
Copy link
Contributor

@bflorian bflorian commented Aug 2, 2022

Refactored devices command to move utility functions into a separate module and added flags to include device health and device status information to device list and get commands.

Checklist

  • I have read the CONTRIBUTING document
  • Any required documentation has been added
  • My code follows the code style of this project (npm run lint produces no warnings/errors)
  • I have added tests to cover my changes

@changeset-bot
Copy link

changeset-bot bot commented Aug 2, 2022

🦋 Changeset detected

Latest commit: 61067b1

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@smartthings/cli Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@bflorian bflorian force-pushed the device-health-status branch from 8913398 to a48f48d Compare August 2, 2022 19:19
const device = await this.client.devices.get(id, deviceGetOptions)
// Note -- we have to do this explicitly because the API does not honor the includeHealth parameter
// for individual devices
if (this.flags.health) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This could be refactored to be a parallel call since we aren't using the device to get the health state.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@bflorian bflorian changed the title feat: Refactored devices command and added health and status flags. feat: refactored devices command and added health and status flags. Aug 3, 2022
@bflorian bflorian force-pushed the device-health-status branch from a48f48d to 61067b1 Compare August 3, 2022 21:05
@bflorian bflorian requested a review from john-u August 3, 2022 21:13
Comment on lines +46 to +58
const tablePushMock: jest.Mock<number, [(string | undefined)[]]> = jest.fn()
const tableToStringMock = jest.fn()
const tableMock = {
push: tablePushMock,
toString: tableToStringMock,
} as unknown as Table
const newOutputTableMock = jest.fn().mockReturnValue(tableMock)

const tableGeneratorMock: TableGenerator = {
newOutputTable: newOutputTableMock,
buildTableFromItem: jest.fn(),
buildTableFromList: jest.fn(),
} as TableGenerator
Copy link
Contributor

Choose a reason for hiding this comment

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

I see these mocks repeated throughout several tests. We should extract them out to a common scope for reuse.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will refactor these in next PR, which is in progress

Comment on lines +115 to +116
const tablePushMock: jest.Mock<number, [(string | undefined)[]]> = jest.fn()
const tableToStringMock = jest.fn()
Copy link
Contributor

Choose a reason for hiding this comment

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

These jest.fn() as well as the TableGenerator mock functions below can be specified with jest.mocked(TableAdapter.prototype.push)
jest.mocked(DefaultTableGenerator.prototype.newOutputTable)
etc. which should give us more type safety.

Comment on lines +221 to +222
const deviceId = 'device-id'
const getSpy = jest.spyOn(DevicesEndpoint.prototype, 'get').mockImplementation()
Copy link
Contributor

Choose a reason for hiding this comment

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

These should be declared at the describe level to reuse throughout the tests.

"@smartthings/cli": patch
---

feat: Refactored devices command and added health and status flags.
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is minor these are used for release notes and it might be more clear to just say something like added health and status flags to devices command (no need for feat: here either).

I wonder if we shouldn't come up with some sort of a little style guide for these.

})

it('uses devices.get to get device', async () => {
await expect(DevicesCommand.run(['--capability', 'cmd-line-capability'])).resolves.not.toThrow()
Copy link
Collaborator

Choose a reason for hiding this comment

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

Good catch.


export const buildTableOutput = (tableGenerator: TableGenerator, device: Device & { profileId?: string; healthState?: DeviceHealth }): string => {
const table = tableGenerator.newOutputTable()
table.push(['Label', device.label])
Copy link
Collaborator

Choose a reason for hiding this comment

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

Moving this is such a minor thing but it definitely belongs where you put it. 👍

return output
}

export const buildEmbeddedStatusTableOutput = (tableGenerator: TableGenerator, data: Device): string => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I might play with trying to merge these two functions after you merge this if that's okay. (I'm thinking maybe a single generic function and then two glue functions that call it, assuming I can figure out how to make the types work.)

@bflorian bflorian merged commit d62c051 into SmartThingsCommunity:master Aug 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants