@@ -4,23 +4,24 @@ import { instance, mock } from 'ts-mockito'
44import { PluginEvents } from 'uiSrc/plugins/pluginEvents'
55import { pluginApi } from 'uiSrc/services/PluginAPI'
66import { cleanup , mockedStore , render } from 'uiSrc/utils/test-utils'
7+ import { formatToText } from 'uiSrc/utils'
8+ import { sendPluginCommandAction , getPluginStateAction , setPluginStateAction } from 'uiSrc/slices/app/plugins'
79import QueryCardCliPlugin , { Props } from './QueryCardCliPlugin'
810
911const mockedProps = mock < Props > ( )
1012
11- let store : typeof mockedStore
12- beforeEach ( ( ) => {
13- cleanup ( )
14- store = cloneDeep ( mockedStore )
15- store . clearActions ( )
16- } )
17-
1813jest . mock ( 'uiSrc/services/PluginAPI' , ( ) => ( {
1914 pluginApi : {
20- onEvent : jest . fn ( )
15+ onEvent : jest . fn ( ) ,
16+ sendEvent : jest . fn ( ) ,
2117 }
2218} ) )
2319
20+ jest . mock ( 'uiSrc/utils' , ( ) => ( {
21+ ...jest . requireActual ( 'uiSrc/utils' ) ,
22+ formatToText : jest . fn ( )
23+ } ) )
24+
2425jest . mock ( 'uiSrc/slices/app/plugins' , ( ) => ( {
2526 ...jest . requireActual ( 'uiSrc/slices/app/plugins' ) ,
2627 appPluginsSelector : jest . fn ( ) . mockReturnValue ( {
@@ -35,6 +36,9 @@ jest.mock('uiSrc/slices/app/plugins', () => ({
3536 }
3637 ]
3738 } ) ,
39+ sendPluginCommandAction : jest . fn ( ) ,
40+ getPluginStateAction : jest . fn ( ) ,
41+ setPluginStateAction : jest . fn ( ) ,
3842} ) )
3943
4044jest . mock ( 'uiSrc/services' , ( ) => ( {
@@ -45,6 +49,13 @@ jest.mock('uiSrc/services', () => ({
4549 } ,
4650} ) )
4751
52+ let store : typeof mockedStore
53+ beforeEach ( ( ) => {
54+ cleanup ( )
55+ store = cloneDeep ( mockedStore )
56+ store . clearActions ( )
57+ } )
58+
4859describe ( 'QueryCardCliPlugin' , ( ) => {
4960 it ( 'should render' , ( ) => {
5061 expect ( render ( < QueryCardCliPlugin { ...instance ( mockedProps ) } /> ) ) . toBeTruthy ( )
@@ -64,5 +75,101 @@ describe('QueryCardCliPlugin', () => {
6475 expect ( onEventMock ) . toBeCalledWith ( expect . any ( String ) , PluginEvents . executeRedisCommand , expect . any ( Function ) )
6576 expect ( onEventMock ) . toBeCalledWith ( expect . any ( String ) , PluginEvents . getState , expect . any ( Function ) )
6677 expect ( onEventMock ) . toBeCalledWith ( expect . any ( String ) , PluginEvents . setState , expect . any ( Function ) )
78+ expect ( onEventMock ) . toBeCalledWith ( expect . any ( String ) , PluginEvents . formatRedisReply , expect . any ( Function ) )
79+ } )
80+
81+ it ( 'should subscribes and call sendPluginCommandAction' , ( ) => {
82+ const mockedSendPluginCommandAction = jest . fn ( ) . mockImplementation ( ( ) => jest . fn ( ) ) ;
83+ ( sendPluginCommandAction as jest . Mock ) . mockImplementation ( mockedSendPluginCommandAction )
84+
85+ const onEventMock = jest . fn ( ) . mockImplementation (
86+ ( _iframeId : string , event : string , callback : ( data : any ) => void ) => {
87+ if ( event === PluginEvents . executeRedisCommand ) {
88+ callback ( { command : 'info' } )
89+ }
90+ }
91+ ) ;
92+
93+ ( pluginApi . onEvent as jest . Mock ) . mockImplementation ( onEventMock )
94+
95+ render ( < QueryCardCliPlugin { ...instance ( mockedProps ) } id = "1" /> )
96+
97+ expect ( mockedSendPluginCommandAction ) . toBeCalledWith (
98+ {
99+ command : 'info' ,
100+ onSuccessAction : expect . any ( Function ) ,
101+ onFailAction : expect . any ( Function )
102+ }
103+ )
104+ } )
105+
106+ it ( 'should subscribes and call getPluginStateAction with proper data' , ( ) => {
107+ const mockedGetPluginStateAction = jest . fn ( ) . mockImplementation ( ( ) => jest . fn ( ) ) ;
108+ ( getPluginStateAction as jest . Mock ) . mockImplementation ( mockedGetPluginStateAction )
109+
110+ const onEventMock = jest . fn ( ) . mockImplementation (
111+ ( _iframeId : string , event : string , callback : ( data : any ) => void ) => {
112+ if ( event === PluginEvents . getState ) {
113+ callback ( { requestId : 5 } )
114+ }
115+ }
116+ ) ;
117+
118+ ( pluginApi . onEvent as jest . Mock ) . mockImplementation ( onEventMock )
119+
120+ render ( < QueryCardCliPlugin { ...instance ( mockedProps ) } id = "1" commandId = "100" /> )
121+
122+ expect ( mockedGetPluginStateAction ) . toBeCalledWith (
123+ {
124+ commandId : '100' ,
125+ onSuccessAction : expect . any ( Function ) ,
126+ onFailAction : expect . any ( Function ) ,
127+ visualizationId : '1'
128+ }
129+ )
130+ } )
131+
132+ it ( 'should subscribes and call setPluginStateAction with proper data' , ( ) => {
133+ const mockedSetPluginStateAction = jest . fn ( ) . mockImplementation ( ( ) => jest . fn ( ) ) ;
134+ ( setPluginStateAction as jest . Mock ) . mockImplementation ( mockedSetPluginStateAction )
135+
136+ const onEventMock = jest . fn ( ) . mockImplementation (
137+ ( _iframeId : string , event : string , callback : ( data : any ) => void ) => {
138+ if ( event === PluginEvents . setState ) {
139+ callback ( { requestId : 5 } )
140+ }
141+ }
142+ ) ;
143+
144+ ( pluginApi . onEvent as jest . Mock ) . mockImplementation ( onEventMock )
145+
146+ render ( < QueryCardCliPlugin { ...instance ( mockedProps ) } id = "1" commandId = "200" /> )
147+
148+ expect ( mockedSetPluginStateAction ) . toBeCalledWith (
149+ {
150+ commandId : '200' ,
151+ onSuccessAction : expect . any ( Function ) ,
152+ onFailAction : expect . any ( Function ) ,
153+ visualizationId : '1'
154+ }
155+ )
156+ } )
157+
158+ it ( 'should subscribes and call formatToText' , ( ) => {
159+ const formatToTextMock = jest . fn ( ) ;
160+ ( formatToText as jest . Mock ) . mockImplementation ( formatToTextMock )
161+ const onEventMock = jest . fn ( ) . mockImplementation (
162+ ( _iframeId : string , event : string , callback : ( dat : any ) => void ) => {
163+ if ( event === PluginEvents . formatRedisReply ) {
164+ callback ( { requestId : '1' , data : { response : [ ] , command : 'info' } } )
165+ }
166+ }
167+ ) ;
168+
169+ ( pluginApi . onEvent as jest . Mock ) . mockImplementation ( onEventMock )
170+
171+ render ( < QueryCardCliPlugin { ...instance ( mockedProps ) } id = "1" /> )
172+
173+ expect ( formatToTextMock ) . toBeCalledWith ( [ ] , 'info' )
67174 } )
68175} )
0 commit comments