@@ -4,23 +4,24 @@ import { instance, mock } from 'ts-mockito'
4
4
import { PluginEvents } from 'uiSrc/plugins/pluginEvents'
5
5
import { pluginApi } from 'uiSrc/services/PluginAPI'
6
6
import { cleanup , mockedStore , render } from 'uiSrc/utils/test-utils'
7
+ import { formatToText } from 'uiSrc/utils'
8
+ import { sendPluginCommandAction , getPluginStateAction , setPluginStateAction } from 'uiSrc/slices/app/plugins'
7
9
import QueryCardCliPlugin , { Props } from './QueryCardCliPlugin'
8
10
9
11
const mockedProps = mock < Props > ( )
10
12
11
- let store : typeof mockedStore
12
- beforeEach ( ( ) => {
13
- cleanup ( )
14
- store = cloneDeep ( mockedStore )
15
- store . clearActions ( )
16
- } )
17
-
18
13
jest . mock ( 'uiSrc/services/PluginAPI' , ( ) => ( {
19
14
pluginApi : {
20
- onEvent : jest . fn ( )
15
+ onEvent : jest . fn ( ) ,
16
+ sendEvent : jest . fn ( ) ,
21
17
}
22
18
} ) )
23
19
20
+ jest . mock ( 'uiSrc/utils' , ( ) => ( {
21
+ ...jest . requireActual ( 'uiSrc/utils' ) ,
22
+ formatToText : jest . fn ( )
23
+ } ) )
24
+
24
25
jest . mock ( 'uiSrc/slices/app/plugins' , ( ) => ( {
25
26
...jest . requireActual ( 'uiSrc/slices/app/plugins' ) ,
26
27
appPluginsSelector : jest . fn ( ) . mockReturnValue ( {
@@ -35,6 +36,9 @@ jest.mock('uiSrc/slices/app/plugins', () => ({
35
36
}
36
37
]
37
38
} ) ,
39
+ sendPluginCommandAction : jest . fn ( ) ,
40
+ getPluginStateAction : jest . fn ( ) ,
41
+ setPluginStateAction : jest . fn ( ) ,
38
42
} ) )
39
43
40
44
jest . mock ( 'uiSrc/services' , ( ) => ( {
@@ -45,6 +49,13 @@ jest.mock('uiSrc/services', () => ({
45
49
} ,
46
50
} ) )
47
51
52
+ let store : typeof mockedStore
53
+ beforeEach ( ( ) => {
54
+ cleanup ( )
55
+ store = cloneDeep ( mockedStore )
56
+ store . clearActions ( )
57
+ } )
58
+
48
59
describe ( 'QueryCardCliPlugin' , ( ) => {
49
60
it ( 'should render' , ( ) => {
50
61
expect ( render ( < QueryCardCliPlugin { ...instance ( mockedProps ) } /> ) ) . toBeTruthy ( )
@@ -64,5 +75,101 @@ describe('QueryCardCliPlugin', () => {
64
75
expect ( onEventMock ) . toBeCalledWith ( expect . any ( String ) , PluginEvents . executeRedisCommand , expect . any ( Function ) )
65
76
expect ( onEventMock ) . toBeCalledWith ( expect . any ( String ) , PluginEvents . getState , expect . any ( Function ) )
66
77
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' )
67
174
} )
68
175
} )
0 commit comments