33 calculateOutputFormat ,
44 chooseDevice ,
55 IOFormat ,
6- jsonFormatter ,
76 writeOutput ,
87} from '@smartthings/cli-lib'
98import { Device , DeviceActivity , DevicesEndpoint , HistoryEndpoint , PaginatedList , SmartThingsClient } from '@smartthings/core-sdk'
@@ -14,29 +13,14 @@ import { calculateRequestLimit, getHistory, writeDeviceEventsTable } from '../..
1413jest . mock ( '../../../lib/commands/history-util' )
1514
1615describe ( 'DeviceHistoryCommand' , ( ) => {
16+ const chooseDeviceMock = jest . mocked ( chooseDevice ) . mockResolvedValue ( 'deviceId' )
1717 const getDeviceSpy = jest . spyOn ( DevicesEndpoint . prototype , 'get' ) . mockImplementation ( )
1818 const historySpy = jest . spyOn ( HistoryEndpoint . prototype , 'devices' ) . mockImplementation ( )
19- const deviceSelectionMock = jest . mocked ( chooseDevice ) . mockResolvedValue ( 'deviceId' )
2019 const calculateOutputFormatMock = jest . mocked ( calculateOutputFormat ) . mockReturnValue ( IOFormat . COMMON )
2120 const writeDeviceEventsTableMock = jest . mocked ( writeDeviceEventsTable )
22- const calculateHistoryRequestLimitMock = jest . mocked ( calculateRequestLimit )
21+ const calculateRequestLimitMock = jest . mocked ( calculateRequestLimit )
2322 const getHistoryMock = jest . mocked ( getHistory )
2423
25- it ( 'prompts user to select device' , async ( ) => {
26- getDeviceSpy . mockResolvedValueOnce ( { locationId : 'locationId' } as Device )
27- historySpy . mockResolvedValueOnce ( {
28- items : [ ] ,
29- hasNext : ( ) : boolean => false ,
30- } as unknown as PaginatedList < DeviceActivity > )
31- await expect ( DeviceHistoryCommand . run ( [ 'deviceId' ] ) ) . resolves . not . toThrow ( )
32-
33- expect ( deviceSelectionMock ) . toHaveBeenCalledWith (
34- expect . any ( DeviceHistoryCommand ) ,
35- 'deviceId' ,
36- { allowIndex : true } ,
37- )
38- } )
39-
4024 it ( 'queries history and writes event table interactively' , async ( ) => {
4125 getDeviceSpy . mockResolvedValueOnce ( { locationId : 'locationId' } as Device )
4226 historySpy . mockResolvedValueOnce ( {
@@ -46,8 +30,13 @@ describe('DeviceHistoryCommand', () => {
4630
4731 await expect ( DeviceHistoryCommand . run ( [ 'deviceId' ] ) ) . resolves . not . toThrow ( )
4832
33+ expect ( calculateRequestLimitMock ) . toHaveBeenCalledTimes ( 1 )
34+ expect ( calculateRequestLimitMock ) . toHaveBeenCalledWith ( 20 )
35+ expect ( chooseDeviceMock ) . toHaveBeenCalledTimes ( 1 )
36+ expect ( chooseDeviceMock ) . toHaveBeenCalledWith ( expect . any ( DeviceHistoryCommand ) , 'deviceId' , { allowIndex : true } )
4937 expect ( getDeviceSpy ) . toHaveBeenCalledTimes ( 1 )
5038 expect ( getDeviceSpy ) . toHaveBeenCalledWith ( 'deviceId' )
39+ expect ( calculateOutputFormatMock ) . toHaveBeenCalledTimes ( 1 )
5140 expect ( historySpy ) . toHaveBeenCalledTimes ( 1 )
5241 expect ( historySpy ) . toHaveBeenCalledWith ( {
5342 deviceId : 'deviceId' ,
@@ -56,23 +45,28 @@ describe('DeviceHistoryCommand', () => {
5645 expect ( writeDeviceEventsTableMock ) . toHaveBeenCalledTimes ( 1 )
5746 } )
5847
59- it ( 'queries history and write event table directly' , async ( ) => {
60- const buildOutputFormatterMock = jest . mocked ( buildOutputFormatter )
48+ it ( 'writes non-table output when specified' , async ( ) => {
49+ const outputFormatterMock = jest . fn ( ) . mockReturnValueOnce ( 'formatted output' )
50+ const buildOutputFormatterMock = jest . mocked ( buildOutputFormatter < DeviceActivity [ ] > )
6151 const writeOutputMock = jest . mocked ( writeOutput )
6252
63- calculateHistoryRequestLimitMock . mockReturnValueOnce ( 20 )
53+ const items = [ { deviceId : 'device-1' } ] as DeviceActivity [ ]
54+
55+ calculateRequestLimitMock . mockReturnValueOnce ( 20 )
6456 getDeviceSpy . mockResolvedValueOnce ( { locationId : 'locationId' } as Device )
65- historySpy . mockResolvedValueOnce ( {
66- items : [ ] ,
67- hasNext : ( ) : boolean => false ,
68- } as unknown as PaginatedList < DeviceActivity > )
6957 calculateOutputFormatMock . mockReturnValueOnce ( IOFormat . JSON )
70- buildOutputFormatterMock . mockReturnValueOnce ( jsonFormatter ( 4 ) )
58+ buildOutputFormatterMock . mockReturnValueOnce ( outputFormatterMock )
59+ getHistoryMock . mockResolvedValueOnce ( items )
7160
7261 await expect ( DeviceHistoryCommand . run ( [ 'deviceId' ] ) ) . resolves . not . toThrow ( )
7362
63+ expect ( calculateRequestLimitMock ) . toHaveBeenCalledTimes ( 1 )
64+ expect ( calculateRequestLimitMock ) . toHaveBeenCalledWith ( 20 )
65+ expect ( chooseDeviceMock ) . toHaveBeenCalledTimes ( 1 )
66+ expect ( chooseDeviceMock ) . toHaveBeenCalledWith ( expect . any ( DeviceHistoryCommand ) , 'deviceId' , { allowIndex : true } )
7467 expect ( getDeviceSpy ) . toHaveBeenCalledTimes ( 1 )
7568 expect ( getDeviceSpy ) . toHaveBeenCalledWith ( 'deviceId' )
69+ expect ( calculateOutputFormatMock ) . toHaveBeenCalledTimes ( 1 )
7670 expect ( getHistoryMock ) . toHaveBeenCalledTimes ( 1 )
7771 expect ( getHistoryMock ) . toHaveBeenCalledWith (
7872 expect . any ( SmartThingsClient ) ,
@@ -84,7 +78,10 @@ describe('DeviceHistoryCommand', () => {
8478 } ) ,
8579 )
8680 expect ( buildOutputFormatterMock ) . toHaveBeenCalledTimes ( 1 )
81+ expect ( outputFormatterMock ) . toHaveBeenCalledTimes ( 1 )
82+ expect ( outputFormatterMock ) . toHaveBeenCalledWith ( items )
8783 expect ( writeOutputMock ) . toHaveBeenCalledTimes ( 1 )
84+ expect ( writeOutputMock ) . toHaveBeenCalledWith ( 'formatted output' , undefined )
8885
8986 expect ( historySpy ) . toHaveBeenCalledTimes ( 0 )
9087 expect ( writeDeviceEventsTableMock ) . toHaveBeenCalledTimes ( 0 )
0 commit comments