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" ;
34import Circle from "../Circle/Circle" ;
45import { createElement , FunctionComponent } from "react" ;
56import { ProgressCircle } from "../ProgressCircle" ;
@@ -8,192 +9,129 @@ const mockedAnimate = jest.fn();
89
910jest . mock ( "../Circle/Circle" , ( ) => {
1011 const originalModule = jest . requireActual ( "../Circle/Circle" ) ;
11-
1212 return jest . fn ( ) . mockImplementation ( ( ) => ( {
1313 ...originalModule ,
1414 path : {
1515 className : {
1616 baseVal : ""
1717 }
1818 } ,
19- animate : mockedAnimate
19+ animate : mockedAnimate ,
20+ destroy : jest . fn ( )
2021 } ) ) ;
2122} ) ;
2223
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+ }
2535
36+ describe ( "ProgressCircle" , ( ) => {
2637 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 ( ) ;
3940 } ) ;
4041
4142 it ( "renders the progressbar Circle" , ( ) => {
42- mount (
43- < ProgressCircle currentValue = { 23 } minValue = { 0 } maxValue = { 100 } onClick = { onClickSpy } label = "23%" class = "" />
44- ) ;
43+ renderProgressCircle ( ) ;
4544 expect ( Circle ) . toHaveBeenCalled ( ) ;
4645 expect ( mockedAnimate ) . toHaveBeenCalledWith ( 0.23 ) ;
4746 } ) ;
4847
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%" ) ) ;
5453 expect ( onClickSpy ) . toHaveBeenCalled ( ) ;
5554 } ) ;
5655
5756 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 } ) ;
6258 expect ( mockedAnimate ) . toHaveBeenCalledWith ( 0.25 ) ;
63- expect ( progressCircle . text ( ) ) . toBe ( "25%" ) ;
59+ expect ( screen . getByText ( "25%" ) ) . toBeInTheDocument ( ) ;
6460 } ) ;
6561
6662 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 } ) ;
7064 expect ( mockedAnimate ) . toHaveBeenCalledWith ( 0 ) ;
71- expect ( progressCircle . text ( ) ) . toContain ( "0%" ) ;
65+ expect ( screen . getByText ( "0%" ) ) ?. toBeInTheDocument ( ) ;
7266 } ) ;
7367
7468 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 } ) ;
7870 expect ( mockedAnimate ) . toHaveBeenCalledWith ( 1 ) ;
79- expect ( progressCircle . text ( ) ) . toContain ( "100%" ) ;
71+ expect ( screen . getByText ( "100%" ) ) ?. toBeInTheDocument ( ) ;
8072 } ) ;
8173
8274 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" ) ;
9678 } ) ;
9779
9880 describe ( "shows a runtime error Alert" , ( ) => {
9981 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 (
11386 "Error in progress circle values: The progress value is lower than the minimum value."
11487 ) ;
11588 } ) ;
11689
11790 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 (
13195 "Error in progress circle values: The progress value is higher than the maximum value."
13296 ) ;
13397 } ) ;
13498
13599 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 (
149104 "Error in progress circle values: The maximum value is lower than the minimum value."
150105 ) ;
151106 } ) ;
152107 } ) ;
153108
154109 describe ( "the label of the progressbar" , ( ) => {
155110 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 ( ) ;
167113 } ) ;
168114
169115 it ( "should accept a component" , ( ) => {
170116 const RandomComponent : FunctionComponent < any > = ( ) => < div > This is a random component</ div > ;
171- const progressCircle = mount (
117+ render (
172118 < ProgressCircle
173119 currentValue = { 50 }
174120 minValue = { 0 }
175121 maxValue = { 100 }
176- onClick = { onClickSpy }
122+ onClick = { jest . fn ( ) }
177123 label = { < RandomComponent /> }
178124 class = ""
179125 />
180126 ) ;
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 ( ) ;
183128 } ) ;
184129
185130 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 ( "" ) ;
197135 } ) ;
198136 } ) ;
199137} ) ;
0 commit comments