1
1
import {
2
+ AppEvents ,
2
3
DefaultTimeRange ,
3
4
DefaultTimeZone ,
4
5
LoadingState ,
5
6
PanelProps ,
6
7
} from '@grafana/data' ;
7
- import { mount } from 'enzyme' ;
8
+ import { getBackendSrv , getDataSourceSrv , SystemJS } from '@grafana/runtime' ;
9
+ import { Button , HorizontalGroup , VerticalGroup } from '@grafana/ui' ;
10
+ import { shallow } from 'enzyme' ;
8
11
import React from 'react' ;
9
- import { Options } from 'types' ;
12
+ import { ButtonOptions , Options } from 'types' ;
10
13
import { ButtonPanel } from './buttonPanel' ;
14
+ jest . mock ( '@grafana/runtime' ) ;
11
15
12
16
describe ( 'button panel' , ( ) => {
13
- test ( 'panel' , ( ) => {
14
- let props : PanelProps < Options > = {
17
+ const status = 200 ;
18
+ const statusError = 500 ;
19
+ const statusText = 'OKI' ;
20
+ let defaultProps : PanelProps < Options > ;
21
+
22
+ beforeEach ( ( ) => {
23
+ defaultProps = {
15
24
id : 1 ,
16
25
data : {
17
26
timeRange : DefaultTimeRange ,
@@ -34,7 +43,89 @@ describe('button panel', () => {
34
43
orientation : 'horizontal' ,
35
44
} as Options ,
36
45
} ;
46
+ } ) ;
47
+
48
+ test ( 'orientation' , ( ) => {
49
+ let wrapper = shallow ( < ButtonPanel { ...defaultProps } /> ) ;
50
+ expect ( wrapper . find ( HorizontalGroup ) ) . toHaveLength ( 1 ) ;
51
+ expect ( wrapper . find ( VerticalGroup ) ) . toHaveLength ( 0 ) ;
52
+ wrapper . setProps ( { options : { buttons : [ ] , orientation : 'vertical' } } ) ;
53
+ expect ( wrapper . find ( HorizontalGroup ) ) . toHaveLength ( 0 ) ;
54
+ expect ( wrapper . find ( VerticalGroup ) ) . toHaveLength ( 1 ) ;
55
+ } ) ;
56
+
57
+ test ( 'buttons' , ( ) => {
58
+ const wrapper = shallow ( < ButtonPanel { ...defaultProps } /> ) ;
59
+ expect ( wrapper . find ( Button ) ) . toHaveLength ( 0 ) ;
60
+
61
+ const buttons : ButtonOptions [ ] = [
62
+ { text : 'a' , variant : 'destructive' , datasource : 'a' } ,
63
+ { text : 'b' , variant : 'primary' , datasource : 'b' } ,
64
+ ] ;
65
+ wrapper . setProps ( { options : { buttons : buttons } } ) ;
66
+
67
+ const mockGet = jest . fn ( ) . mockReturnValue ( { id : 1 } ) ;
68
+ getDataSourceSrv . mockImplementation ( ( ) => ( { get : mockGet } ) ) ;
69
+ const mockDataSourceRequest = jest . fn ( ) . mockReturnValue ( {
70
+ status : status ,
71
+ statusText : statusText ,
72
+ } ) ;
73
+ getBackendSrv . mockImplementation ( ( ) => ( {
74
+ datasourceRequest : mockDataSourceRequest ,
75
+ } ) ) ;
76
+ const mockEmit = jest . fn ( ) ;
77
+ SystemJS . load . mockImplementation ( async ( ) => ( {
78
+ emit : mockEmit ,
79
+ } ) ) ;
80
+
81
+ const buttonWidgets = wrapper . find ( Button ) ;
82
+ expect ( buttonWidgets ) . toHaveLength ( buttons . length ) ;
83
+ buttonWidgets . forEach ( ( b : any , i : number ) => {
84
+ expect ( b . key ( ) ) . toBe ( i . toString ( ) ) ;
85
+ expect ( b . prop ( 'variant' ) ) . toBe ( buttons [ i ] . variant ) ;
86
+ expect ( b . text ( ) ) . toBe ( buttons [ i ] . text ) ;
87
+ b . simulate ( 'click' ) ;
88
+ setImmediate ( ( ) => {
89
+ expect ( mockGet ) . toHaveBeenCalledWith ( buttons [ i ] . datasource ) ;
90
+ expect ( mockDataSourceRequest ) . toHaveBeenCalled ( ) ;
91
+ expect ( mockEmit ) . toHaveBeenCalledWith ( AppEvents . alertSuccess , [
92
+ buttons [ i ] . text + ': ' + status + ' (' + statusText + ')' ,
93
+ ] ) ;
94
+ } ) ;
95
+ } ) ;
96
+ } ) ;
97
+
98
+ test ( 'button error' , ( ) => {
99
+ const wrapper = shallow ( < ButtonPanel { ...defaultProps } /> ) ;
100
+ const buttons : ButtonOptions [ ] = [
101
+ { variant : 'destructive' , datasource : 'a' } ,
102
+ ] ;
103
+ wrapper . setProps ( { options : { buttons : buttons } } ) ;
104
+
105
+ const mockGet = jest . fn ( ) . mockReturnValue ( { id : 1 } ) ;
106
+ getDataSourceSrv . mockImplementation ( ( ) => ( { get : mockGet } ) ) ;
107
+ const msg = 'msg' ;
108
+ const mockDataSourceRequest = jest . fn ( ) . mockRejectedValue ( {
109
+ status : statusError ,
110
+ statusText : statusText ,
111
+ data : { message : msg } ,
112
+ } ) ;
113
+ getBackendSrv . mockImplementation ( ( ) => ( {
114
+ datasourceRequest : mockDataSourceRequest ,
115
+ } ) ) ;
116
+ const mockEmit = jest . fn ( ) ;
117
+ SystemJS . load . mockImplementation ( async ( ) => ( {
118
+ emit : mockEmit ,
119
+ } ) ) ;
37
120
38
- mount ( < ButtonPanel { ...props } /> ) ;
121
+ const widget = wrapper . find ( Button ) ;
122
+ expect ( widget . text ( ) ) . toBe ( 'Button' ) ;
123
+ widget . simulate ( 'click' ) ;
124
+ setImmediate ( ( ) => {
125
+ expect ( mockEmit ) . toHaveBeenCalledWith ( AppEvents . alertError , [
126
+ widget . text ( ) + ': ' + statusError + ' (' + statusText + ')' ,
127
+ msg ,
128
+ ] ) ;
129
+ } ) ;
39
130
} ) ;
40
131
} ) ;
0 commit comments