1
- import { Alert } from "@mendix/widget-plugin-component-kit/Alert" ;
2
- import { mount , shallow } from "enzyme" ;
1
+ import { render , screen } from "@testing-library/react" ;
2
+ import userEvent from "@testing-library/user-event" ;
3
+ import "@testing-library/jest-dom" ;
3
4
import Circle from "../Circle/Circle" ;
4
5
import { createElement , FunctionComponent } from "react" ;
5
6
import { ProgressCircle } from "../ProgressCircle" ;
@@ -8,192 +9,129 @@ const mockedAnimate = jest.fn();
8
9
9
10
jest . mock ( "../Circle/Circle" , ( ) => {
10
11
const originalModule = jest . requireActual ( "../Circle/Circle" ) ;
11
-
12
12
return jest . fn ( ) . mockImplementation ( ( ) => ( {
13
13
...originalModule ,
14
14
path : {
15
15
className : {
16
16
baseVal : ""
17
17
}
18
18
} ,
19
- animate : mockedAnimate
19
+ animate : mockedAnimate ,
20
+ destroy : jest . fn ( )
20
21
} ) ) ;
21
22
} ) ;
22
23
23
- describe ( "ProgressCircle" , ( ) => {
24
- const onClickSpy = jest . fn ( ) ;
24
+ function renderProgressCircle ( props = { } ) : ReturnType < typeof render > {
25
+ const defaultProps = {
26
+ currentValue : 23 ,
27
+ minValue : 0 ,
28
+ maxValue : 100 ,
29
+ onClick : jest . fn ( ) ,
30
+ label : "23%" ,
31
+ class : ""
32
+ } ;
33
+ return render ( < ProgressCircle { ...defaultProps } { ...props } /> ) ;
34
+ }
25
35
36
+ describe ( "ProgressCircle" , ( ) => {
26
37
it ( "renders the structure correctly" , ( ) => {
27
- expect (
28
- shallow (
29
- < ProgressCircle
30
- currentValue = { 23 }
31
- minValue = { 0 }
32
- maxValue = { 100 }
33
- onClick = { onClickSpy }
34
- label = "23%"
35
- class = ""
36
- />
37
- )
38
- ) . toMatchSnapshot ( ) ;
38
+ renderProgressCircle ( ) ;
39
+ expect ( screen . getByText ( "23%" ) ) . toBeInTheDocument ( ) ;
39
40
} ) ;
40
41
41
42
it ( "renders the progressbar Circle" , ( ) => {
42
- mount (
43
- < ProgressCircle currentValue = { 23 } minValue = { 0 } maxValue = { 100 } onClick = { onClickSpy } label = "23%" class = "" />
44
- ) ;
43
+ renderProgressCircle ( ) ;
45
44
expect ( Circle ) . toHaveBeenCalled ( ) ;
46
45
expect ( mockedAnimate ) . toHaveBeenCalledWith ( 0.23 ) ;
47
46
} ) ;
48
47
49
- it ( "triggers an event when a clickable progress bar is clicked" , ( ) => {
50
- const progressCircle = mount (
51
- < ProgressCircle currentValue = { 23 } minValue = { 0 } maxValue = { 100 } onClick = { onClickSpy } label = "23%" class = "" />
52
- ) ;
53
- progressCircle . find ( ".progress-circle-label-container" ) . simulate ( "click" ) ;
48
+ it ( "triggers an event when a clickable progress bar is clicked" , async ( ) => {
49
+ const user = userEvent . setup ( ) ;
50
+ const onClickSpy = jest . fn ( ) ;
51
+ renderProgressCircle ( { onClick : onClickSpy } ) ;
52
+ await user . click ( screen . getByText ( "23%" ) ) ;
54
53
expect ( onClickSpy ) . toHaveBeenCalled ( ) ;
55
54
} ) ;
56
55
57
56
it ( "handles a different range" , ( ) => {
58
- const progressCircle = mount (
59
- < ProgressCircle currentValue = { 40 } minValue = { 20 } maxValue = { 100 } onClick = { undefined } label = "25%" class = "" />
60
- ) ;
61
- // Value 40 on range 20 - 100 is 25%.
57
+ renderProgressCircle ( { currentValue : 40 , minValue : 20 , maxValue : 100 , label : "25%" , onClick : undefined } ) ;
62
58
expect ( mockedAnimate ) . toHaveBeenCalledWith ( 0.25 ) ;
63
- expect ( progressCircle . text ( ) ) . toBe ( "25%" ) ;
59
+ expect ( screen . getByText ( "25%" ) ) . toBeInTheDocument ( ) ;
64
60
} ) ;
65
61
66
62
it ( "clamps a current value lower than the minimum value to 0% progress" , ( ) => {
67
- const progressCircle = mount (
68
- < ProgressCircle currentValue = { - 20 } minValue = { 20 } maxValue = { 100 } onClick = { undefined } label = "0%" class = "" />
69
- ) ;
63
+ renderProgressCircle ( { currentValue : - 20 , minValue : 20 , maxValue : 100 , label : "0%" , onClick : undefined } ) ;
70
64
expect ( mockedAnimate ) . toHaveBeenCalledWith ( 0 ) ;
71
- expect ( progressCircle . text ( ) ) . toContain ( "0%" ) ;
65
+ expect ( screen . getByText ( "0%" ) ) ?. toBeInTheDocument ( ) ;
72
66
} ) ;
73
67
74
68
it ( "clamps a current value higher than the maximum value to 100% progress" , ( ) => {
75
- const progressCircle = mount (
76
- < ProgressCircle currentValue = { 102 } minValue = { 20 } maxValue = { 100 } onClick = { undefined } label = "100%" class = "" />
77
- ) ;
69
+ renderProgressCircle ( { currentValue : 102 , minValue : 20 , maxValue : 100 , label : "100%" , onClick : undefined } ) ;
78
70
expect ( mockedAnimate ) . toHaveBeenCalledWith ( 1 ) ;
79
- expect ( progressCircle . text ( ) ) . toContain ( "100%" ) ;
71
+ expect ( screen . getByText ( "100%" ) ) ?. toBeInTheDocument ( ) ;
80
72
} ) ;
81
73
82
74
it ( "is not clickable when there is no onClick handler provided" , ( ) => {
83
- const progressCircle = mount (
84
- < ProgressCircle
85
- currentValue = { - 1 }
86
- minValue = { 0 }
87
- maxValue = { 100 }
88
- onClick = { undefined }
89
- label = { undefined }
90
- class = ""
91
- />
92
- ) ;
93
- expect (
94
- progressCircle . find ( ".progress-circle-label-container" ) . hasClass ( "widget-progress-circle-clickable" )
95
- ) . toBe ( false ) ;
75
+ renderProgressCircle ( { onClick : undefined , label : undefined , currentValue : - 1 } ) ;
76
+ const labelContainer = document . querySelector ( ".progress-circle-label-container" ) ;
77
+ expect ( labelContainer ) . not . toHaveClass ( "widget-progress-circle-clickable" ) ;
96
78
} ) ;
97
79
98
80
describe ( "shows a runtime error Alert" , ( ) => {
99
81
it ( "when the current value is lower than the minimum value" , ( ) => {
100
- const progressCircle = mount (
101
- < ProgressCircle
102
- currentValue = { - 1 }
103
- minValue = { 0 }
104
- maxValue = { 100 }
105
- onClick = { onClickSpy }
106
- label = { undefined }
107
- class = ""
108
- />
109
- ) ;
110
- const alert = progressCircle . find ( Alert ) ;
111
- expect ( alert ) . toHaveLength ( 1 ) ;
112
- expect ( alert . text ( ) ) . toBe (
82
+ renderProgressCircle ( { currentValue : - 1 , minValue : 0 , maxValue : 100 , label : undefined } ) ;
83
+ const alert = screen . getByRole ( "alert" ) ;
84
+ expect ( alert ) . toBeInTheDocument ( ) ;
85
+ expect ( alert ) . toHaveTextContent (
113
86
"Error in progress circle values: The progress value is lower than the minimum value."
114
87
) ;
115
88
} ) ;
116
89
117
90
it ( "when the current value is higher than the maximum value" , ( ) => {
118
- const progressCircle = mount (
119
- < ProgressCircle
120
- currentValue = { 200 }
121
- minValue = { 0 }
122
- maxValue = { 100 }
123
- onClick = { onClickSpy }
124
- label = { undefined }
125
- class = ""
126
- />
127
- ) ;
128
- const alert = progressCircle . find ( Alert ) ;
129
- expect ( alert ) . toHaveLength ( 1 ) ;
130
- expect ( alert . text ( ) ) . toBe (
91
+ renderProgressCircle ( { currentValue : 200 , minValue : 0 , maxValue : 100 , label : undefined } ) ;
92
+ const alert = screen . getByRole ( "alert" ) ;
93
+ expect ( alert ) . toBeInTheDocument ( ) ;
94
+ expect ( alert ) . toHaveTextContent (
131
95
"Error in progress circle values: The progress value is higher than the maximum value."
132
96
) ;
133
97
} ) ;
134
98
135
99
it ( "when the range of the progress bar is negative" , ( ) => {
136
- const progressCircle = mount (
137
- < ProgressCircle
138
- currentValue = { 50 }
139
- minValue = { 100 }
140
- maxValue = { 0 }
141
- onClick = { onClickSpy }
142
- label = { undefined }
143
- class = ""
144
- />
145
- ) ;
146
- const alert = progressCircle . find ( Alert ) ;
147
- expect ( alert ) . toHaveLength ( 1 ) ;
148
- expect ( alert . text ( ) ) . toBe (
100
+ renderProgressCircle ( { currentValue : 50 , minValue : 100 , maxValue : 0 , label : undefined } ) ;
101
+ const alert = screen . getByRole ( "alert" ) ;
102
+ expect ( alert ) . toBeInTheDocument ( ) ;
103
+ expect ( alert ) . toHaveTextContent (
149
104
"Error in progress circle values: The maximum value is lower than the minimum value."
150
105
) ;
151
106
} ) ;
152
107
} ) ;
153
108
154
109
describe ( "the label of the progressbar" , ( ) => {
155
110
it ( "should accept static text" , ( ) => {
156
- const progressCircle = mount (
157
- < ProgressCircle
158
- currentValue = { 50 }
159
- minValue = { 0 }
160
- maxValue = { 100 }
161
- onClick = { onClickSpy }
162
- label = "This is a static text"
163
- class = ""
164
- />
165
- ) ;
166
- expect ( progressCircle . text ( ) ) . toBe ( "This is a static text" ) ;
111
+ renderProgressCircle ( { currentValue : 50 , minValue : 0 , maxValue : 100 , label : "This is a static text" } ) ;
112
+ expect ( screen . getByText ( "This is a static text" ) ) . toBeInTheDocument ( ) ;
167
113
} ) ;
168
114
169
115
it ( "should accept a component" , ( ) => {
170
116
const RandomComponent : FunctionComponent < any > = ( ) => < div > This is a random component</ div > ;
171
- const progressCircle = mount (
117
+ render (
172
118
< ProgressCircle
173
119
currentValue = { 50 }
174
120
minValue = { 0 }
175
121
maxValue = { 100 }
176
- onClick = { onClickSpy }
122
+ onClick = { jest . fn ( ) }
177
123
label = { < RandomComponent /> }
178
124
class = ""
179
125
/>
180
126
) ;
181
- expect ( progressCircle . find ( RandomComponent ) ) . toHaveLength ( 1 ) ;
182
- expect ( progressCircle . text ( ) ) . toBe ( "This is a random component" ) ;
127
+ expect ( screen . getByText ( "This is a random component" ) ) . toBeInTheDocument ( ) ;
183
128
} ) ;
184
129
185
130
it ( "should accept nothing" , ( ) => {
186
- const progressCircle = mount (
187
- < ProgressCircle
188
- currentValue = { 50 }
189
- minValue = { 0 }
190
- maxValue = { 100 }
191
- onClick = { onClickSpy }
192
- label = { null }
193
- class = ""
194
- />
195
- ) ;
196
- expect ( progressCircle . text ( ) ) . toHaveLength ( 0 ) ;
131
+ renderProgressCircle ( { currentValue : 50 , minValue : 0 , maxValue : 100 , label : null } ) ;
132
+ // Should not find any label text
133
+ const labelContainer = document . querySelector ( ".progress-circle-label-container" ) ;
134
+ expect ( labelContainer ?. textContent ) . toBe ( "" ) ;
197
135
} ) ;
198
136
} ) ;
199
137
} ) ;
0 commit comments