|
1 |
| -import { Alert } from "@mendix/widget-plugin-component-kit/Alert"; |
2 |
| -import { HTMLAttributes, mount, ReactWrapper, shallow } from "enzyme"; |
3 |
| -import { createElement, CSSProperties, FunctionComponent } from "react"; |
| 1 | +import "@testing-library/jest-dom"; |
| 2 | +import { render, RenderResult } from "@testing-library/react"; |
| 3 | +import userEvent, { UserEvent } from "@testing-library/user-event"; |
| 4 | +import { createElement, ReactElement } from "react"; |
4 | 5 | import { ProgressBar, ProgressBarProps } from "../ProgressBar";
|
5 | 6 |
|
6 |
| -const RandomComponent: FunctionComponent<any> = () => <div>This is a random component</div>; |
7 |
| -const progressBarSmallClassName = "progress-bar-small"; |
8 |
| - |
9 | 7 | describe("Progress bar", () => {
|
10 |
| - function getProgressBarLabelElement(progressBar: ReactWrapper<ProgressBarProps>): ReactWrapper<HTMLAttributes> { |
11 |
| - return progressBar.find(".progress-bar"); |
12 |
| - } |
13 |
| - function getProgressBarStyle(progressBar: ReactWrapper<ProgressBarProps>): CSSProperties | undefined { |
14 |
| - return getProgressBarLabelElement(progressBar).prop("style"); |
15 |
| - } |
16 |
| - const progress = 23; |
17 |
| - const maximumValue = 100; |
18 | 8 | const onClickSpy = jest.fn();
|
| 9 | + const progress = 23; |
| 10 | + let user: UserEvent; |
19 | 11 |
|
20 |
| - it("has progress bar structure", () => { |
21 |
| - const progressBar = shallow( |
| 12 | + function renderProgressBar(props: Partial<ProgressBarProps> = {}): RenderResult { |
| 13 | + return render( |
22 | 14 | <ProgressBar
|
23 | 15 | currentValue={progress}
|
24 | 16 | minValue={0}
|
25 |
| - maxValue={maximumValue} |
| 17 | + maxValue={100} |
26 | 18 | onClick={onClickSpy}
|
27 | 19 | label={`${progress}%`}
|
28 | 20 | class=""
|
| 21 | + {...props} |
29 | 22 | />
|
30 | 23 | );
|
31 |
| - expect(progressBar).toMatchSnapshot(); |
| 24 | + } |
| 25 | + |
| 26 | + beforeEach(() => { |
| 27 | + user = userEvent.setup(); |
32 | 28 | });
|
33 | 29 |
|
34 |
| - it("shows a positive progress", () => { |
35 |
| - const progressBar = mount( |
36 |
| - <ProgressBar |
37 |
| - currentValue={progress} |
38 |
| - minValue={0} |
39 |
| - maxValue={maximumValue} |
40 |
| - onClick={onClickSpy} |
41 |
| - label={`${progress}%`} |
42 |
| - class="" |
43 |
| - /> |
44 |
| - ); |
| 30 | + it("has progress bar structure", () => { |
| 31 | + const progressBar = renderProgressBar(); |
| 32 | + expect(progressBar.asFragment()).toMatchSnapshot(); |
| 33 | + }); |
45 | 34 |
|
46 |
| - expect(getProgressBarStyle(progressBar)).toEqual({ width: "23%" }); |
| 35 | + it("shows a positive progress", () => { |
| 36 | + const { container } = renderProgressBar(); |
| 37 | + expect(container.querySelector(".progress-bar")).toHaveStyle({ width: "23%" }); |
47 | 38 | });
|
48 | 39 |
|
49 |
| - it("triggers an event when a clickable progress bar is clicked", () => { |
50 |
| - const progressBar = mount( |
51 |
| - <ProgressBar |
52 |
| - currentValue={progress} |
53 |
| - minValue={0} |
54 |
| - maxValue={maximumValue} |
55 |
| - onClick={onClickSpy} |
56 |
| - label={`${progress}%`} |
57 |
| - class="" |
58 |
| - /> |
59 |
| - ); |
60 |
| - const progressElement = progressBar.find(".progress"); |
61 |
| - expect(progressElement.hasClass("widget-progress-bar-clickable")).toBe(true); |
62 |
| - progressElement.simulate("click"); |
| 40 | + it("triggers an event when a clickable progress bar is clicked", async () => { |
| 41 | + const { container } = renderProgressBar(); |
| 42 | + const progressElement = container.querySelector(".progress"); |
| 43 | + expect(progressElement?.className.includes("widget-progress-bar-clickable")).toBe(true); |
| 44 | + await user.click(progressElement!); |
63 | 45 | expect(onClickSpy).toHaveBeenCalledTimes(1);
|
64 | 46 | });
|
65 | 47 |
|
66 | 48 | it("handles a different range", () => {
|
67 |
| - const progressBar = mount( |
68 |
| - <ProgressBar |
69 |
| - currentValue={progress} |
70 |
| - minValue={20} |
71 |
| - maxValue={30} |
72 |
| - onClick={onClickSpy} |
73 |
| - label={`${progress}%`} |
74 |
| - class="" |
75 |
| - /> |
76 |
| - ); |
77 |
| - |
| 49 | + const { container } = renderProgressBar({ minValue: 20, maxValue: 30 }); |
78 | 50 | // 23 on a range from 20 to 30 is 30% progress.
|
79 |
| - expect(getProgressBarStyle(progressBar)).toEqual({ width: "30%" }); |
| 51 | + expect(container.querySelector(".progress-bar")).toHaveStyle({ width: "30%" }); |
80 | 52 | });
|
81 | 53 |
|
82 | 54 | it("clamps a current value lower than the minimum value to 0% progress", () => {
|
83 |
| - const progressBar = mount( |
84 |
| - <ProgressBar |
85 |
| - currentValue={-20} |
86 |
| - minValue={0} |
87 |
| - maxValue={100} |
88 |
| - onClick={onClickSpy} |
89 |
| - label={`${progress}%`} |
90 |
| - class="" |
91 |
| - /> |
92 |
| - ); |
93 |
| - |
94 |
| - expect(getProgressBarStyle(progressBar)).toEqual({ width: "0%" }); |
| 55 | + const { container } = renderProgressBar({ currentValue: -20 }); |
| 56 | + expect(container.querySelector(".progress-bar")).toHaveStyle({ width: "0%" }); |
95 | 57 | });
|
96 | 58 |
|
97 | 59 | it("clamps a current value higher than the maximum value to 100% progress", () => {
|
98 |
| - const progressBar = mount( |
99 |
| - <ProgressBar |
100 |
| - currentValue={110} |
101 |
| - minValue={0} |
102 |
| - maxValue={100} |
103 |
| - onClick={onClickSpy} |
104 |
| - label={`${progress}%`} |
105 |
| - class="" |
106 |
| - /> |
107 |
| - ); |
108 |
| - |
109 |
| - expect(getProgressBarStyle(progressBar)).toEqual({ width: "100%" }); |
| 60 | + const { container } = renderProgressBar({ currentValue: 110 }); |
| 61 | + expect(container.querySelector(".progress-bar")).toHaveStyle({ width: "100%" }); |
110 | 62 | });
|
111 | 63 |
|
112 | 64 | it("is not clickable when there is no onClick handler provided", () => {
|
113 |
| - const progressBar = mount( |
114 |
| - <ProgressBar |
115 |
| - currentValue={50} |
116 |
| - minValue={0} |
117 |
| - maxValue={100} |
118 |
| - onClick={undefined} |
119 |
| - label={`${progress}%`} |
120 |
| - class="" |
121 |
| - /> |
122 |
| - ); |
123 |
| - expect(progressBar.find(".progress").hasClass("widget-progress-bar-clickable")).toBe(false); |
| 65 | + const { container } = renderProgressBar({ currentValue: 50, onClick: undefined }); |
| 66 | + expect(container.querySelector(".progress")?.className.includes("widget-progress-bar-clickable")).toBe(false); |
124 | 67 | });
|
125 | 68 |
|
126 | 69 | describe("shows a runtime error Alert", () => {
|
127 | 70 | it("when the current value is lower than the minimum value", () => {
|
128 |
| - const progressBar = mount( |
129 |
| - <ProgressBar |
130 |
| - currentValue={-20} |
131 |
| - minValue={0} |
132 |
| - maxValue={100} |
133 |
| - onClick={onClickSpy} |
134 |
| - label={`${progress}%`} |
135 |
| - class="" |
136 |
| - /> |
137 |
| - ); |
138 |
| - const alert = progressBar.find(Alert); |
139 |
| - expect(alert).toHaveLength(1); |
140 |
| - expect(alert.text()).toBe( |
| 71 | + const { container } = renderProgressBar({ currentValue: -20 }); |
| 72 | + const alert = container.querySelector(".alert-danger"); |
| 73 | + expect(alert).toBeInTheDocument(); |
| 74 | + expect(alert?.textContent).toBe( |
141 | 75 | "Error in progress bar values: The progress value is lower than the minimum value."
|
142 | 76 | );
|
143 | 77 | });
|
144 | 78 |
|
145 | 79 | it("when the current value is higher than the maximum value", () => {
|
146 |
| - const progressBar = mount( |
147 |
| - <ProgressBar |
148 |
| - currentValue={110} |
149 |
| - minValue={0} |
150 |
| - maxValue={100} |
151 |
| - onClick={onClickSpy} |
152 |
| - label={`${progress}%`} |
153 |
| - class="" |
154 |
| - /> |
155 |
| - ); |
156 |
| - const alert = progressBar.find(Alert); |
157 |
| - expect(alert).toHaveLength(1); |
158 |
| - expect(alert.text()).toBe( |
| 80 | + const { container } = renderProgressBar({ currentValue: 110 }); |
| 81 | + const alert = container.querySelector(".alert-danger"); |
| 82 | + expect(alert).toBeInTheDocument(); |
| 83 | + expect(alert?.textContent).toBe( |
159 | 84 | "Error in progress bar values: The progress value is higher than the maximum value."
|
160 | 85 | );
|
161 | 86 | });
|
162 | 87 |
|
163 | 88 | it("when the range of the progress bar is negative", () => {
|
164 |
| - const progressBar = mount( |
165 |
| - <ProgressBar |
166 |
| - currentValue={50} |
167 |
| - minValue={40} |
168 |
| - maxValue={30} |
169 |
| - onClick={onClickSpy} |
170 |
| - label={`${progress}%`} |
171 |
| - class="" |
172 |
| - /> |
173 |
| - ); |
174 |
| - const alert = progressBar.find(Alert); |
175 |
| - expect(alert).toHaveLength(1); |
176 |
| - expect(alert.text()).toBe( |
| 89 | + const { container } = renderProgressBar({ minValue: 40, maxValue: 30, currentValue: 50 }); |
| 90 | + const alert = container.querySelector(".alert-danger"); |
| 91 | + expect(alert).toBeInTheDocument(); |
| 92 | + expect(alert?.textContent).toBe( |
177 | 93 | "Error in progress bar values: The maximum value is lower than the minimum value."
|
178 | 94 | );
|
179 | 95 | });
|
180 | 96 | });
|
181 | 97 |
|
182 | 98 | describe("the label of the progressbar", () => {
|
| 99 | + const RandomComponent = (): ReactElement => <div>This is a random component</div>; |
| 100 | + const progressBarSmallClassName = "progress-bar-small"; |
| 101 | + |
183 | 102 | it("should accept static text", () => {
|
184 |
| - const progressBar = mount( |
185 |
| - <ProgressBar |
186 |
| - currentValue={30} |
187 |
| - minValue={0} |
188 |
| - maxValue={100} |
189 |
| - onClick={onClickSpy} |
190 |
| - label="This is your progress" |
191 |
| - class="" |
192 |
| - /> |
193 |
| - ); |
194 |
| - expect(progressBar.text()).toBe("This is your progress"); |
| 103 | + const { container } = renderProgressBar({ label: "This is your progress" }); |
| 104 | + expect(container.textContent).toBe("This is your progress"); |
195 | 105 | });
|
196 | 106 |
|
197 | 107 | it("should accept a component", () => {
|
198 |
| - const progressBar = mount( |
199 |
| - <ProgressBar |
200 |
| - currentValue={30} |
201 |
| - minValue={0} |
202 |
| - maxValue={100} |
203 |
| - onClick={onClickSpy} |
204 |
| - label={<RandomComponent />} |
205 |
| - class="" |
206 |
| - /> |
207 |
| - ); |
208 |
| - expect(progressBar.find(RandomComponent)).toHaveLength(1); |
209 |
| - expect(progressBar.text()).toContain("This is a random component"); |
| 108 | + const { container, getByText } = renderProgressBar({ label: <RandomComponent /> }); |
| 109 | + expect(getByText("This is a random component")).toBeInTheDocument(); |
| 110 | + expect(container.textContent).toContain("This is a random component"); |
210 | 111 | });
|
211 | 112 |
|
212 | 113 | it("should accept nothing", () => {
|
213 |
| - const progressBar = mount( |
214 |
| - <ProgressBar currentValue={30} minValue={0} maxValue={100} onClick={onClickSpy} label={null} class="" /> |
215 |
| - ); |
216 |
| - expect(progressBar.text()).toHaveLength(0); |
| 114 | + const { container } = renderProgressBar({ label: null }); |
| 115 | + // Should not render any label text |
| 116 | + // Remove whitespace for strict check |
| 117 | + expect(container.textContent?.trim()).toHaveLength(0); |
217 | 118 | });
|
218 | 119 |
|
219 | 120 | it("has a tooltip with the label text when the size is small and label type is textual", () => {
|
220 |
| - const progressBar = mount( |
221 |
| - <ProgressBar |
222 |
| - currentValue={30} |
223 |
| - minValue={0} |
224 |
| - maxValue={100} |
225 |
| - onClick={onClickSpy} |
226 |
| - label="This is a label text" |
227 |
| - class={`${progressBarSmallClassName}`} |
228 |
| - /> |
229 |
| - ); |
230 |
| - expect(getProgressBarLabelElement(progressBar).prop("title")).toBe("This is a label text"); |
| 121 | + const { container } = renderProgressBar({ |
| 122 | + label: "This is a label text", |
| 123 | + class: progressBarSmallClassName |
| 124 | + }); |
| 125 | + const label = container.querySelector(".progress-bar"); |
| 126 | + expect(label).toHaveAttribute("title", "This is a label text"); |
231 | 127 | });
|
232 | 128 |
|
233 | 129 | it("does not have a tooltip nor a label when the size is small and label type is custom", () => {
|
234 |
| - const progressBar = mount( |
235 |
| - <ProgressBar |
236 |
| - currentValue={30} |
237 |
| - minValue={0} |
238 |
| - maxValue={100} |
239 |
| - onClick={onClickSpy} |
240 |
| - label={<RandomComponent />} |
241 |
| - class={`${progressBarSmallClassName}`} |
242 |
| - /> |
243 |
| - ); |
244 |
| - expect(getProgressBarLabelElement(progressBar).prop("title")).toBe(undefined); |
| 130 | + const { container } = renderProgressBar({ |
| 131 | + label: <RandomComponent />, |
| 132 | + class: progressBarSmallClassName |
| 133 | + }); |
| 134 | + const label = container.querySelector(".progress-bar"); |
| 135 | + expect(label).not.toHaveAttribute("title"); |
245 | 136 | });
|
246 | 137 | });
|
247 | 138 | });
|
0 commit comments