Progress.spec.tsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /*
  2. * Copyright 2021-2024 Avaiga Private Limited
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  5. * the License. You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  10. * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
  11. * specific language governing permissions and limitations under the License.
  12. */
  13. import React from "react";
  14. import { render } from "@testing-library/react";
  15. import "@testing-library/jest-dom";
  16. import Progress from "./Progress";
  17. describe("Progress component", () => {
  18. it("renders circular progress without value (indeterminate)", () => {
  19. const { getByRole } = render(<Progress />);
  20. const elt = getByRole("progressbar");
  21. expect(elt).toHaveClass("MuiCircularProgress-root");
  22. });
  23. it("uses the class", async () => {
  24. const { getByRole } = render(<Progress className="test-class" />);
  25. const elt = getByRole("progressbar");
  26. // Actual progress bar in a child of the component
  27. expect(elt.parentElement).toHaveClass("test-class");
  28. });
  29. it("renders circular progress with value (determinate)", () => {
  30. const { getByRole, getByText } = render(<Progress showValue value={50} />);
  31. const elt = getByRole("progressbar");
  32. const valueText = getByText("50%");
  33. expect(elt).toHaveClass("MuiCircularProgress-root");
  34. expect(valueText).toBeInTheDocument();
  35. });
  36. it("renders linear progress without value (indeterminate)", () => {
  37. const { getByRole } = render(<Progress linear />);
  38. const elt = getByRole("progressbar");
  39. expect(elt).toHaveClass("MuiLinearProgress-root");
  40. });
  41. it("renders linear progress with value (determinate)", () => {
  42. const { getByRole, getByText } = render(<Progress linear showValue value={50} />);
  43. const elt = getByRole("progressbar");
  44. const valueText = getByText("50%");
  45. expect(elt).toHaveClass("MuiLinearProgress-root");
  46. expect(valueText).toBeInTheDocument();
  47. });
  48. it("does not render when render prop is false", async () => {
  49. const { container } = render(<Progress render={false} />);
  50. expect(container.firstChild).toBeNull();
  51. });
  52. it("should render the title when title is defined", () => {
  53. const { getByText } = render(<Progress title="Title" />);
  54. const title = getByText("Title");
  55. expect(title).toBeInTheDocument();
  56. });
  57. it("renders Typography with correct sx and variant", () => {
  58. const { getByText } = render(<Progress title="Title" />);
  59. const typographyElement = getByText("Title");
  60. expect(typographyElement).toBeInTheDocument();
  61. expect(typographyElement).toHaveStyle("margin: 8px");
  62. expect(typographyElement.tagName).toBe("SPAN");
  63. });
  64. it("renders determinate progress correctly", () => {
  65. const { getByRole } = render(<Progress value={50} />);
  66. const progressBar = getByRole("progressbar");
  67. expect(progressBar).toBeInTheDocument();
  68. expect(progressBar).toHaveAttribute("aria-valuenow", "50");
  69. });
  70. it("renders determinate progress with linear progress bar", () => {
  71. const { getByRole } = render(<Progress value={50} linear />);
  72. const progressBar = getByRole("progressbar");
  73. expect(progressBar).toBeInTheDocument();
  74. expect(progressBar).toHaveAttribute("aria-valuenow", "50");
  75. });
  76. it("renders title and linear progress bar correctly", () => {
  77. const { getByText, getByRole } = render(<Progress title="Title" value={50} linear showValue={true} />);
  78. const title = getByText("Title");
  79. const progressBar = getByRole("progressbar");
  80. expect(title).toBeInTheDocument();
  81. expect(progressBar).toBeInTheDocument();
  82. });
  83. it("renders title and linear progress bar without showing value", () => {
  84. const { getByText, queryByText } = render(<Progress title="Title" value={50} linear />);
  85. const title = getByText("Title");
  86. const value = queryByText("50%");
  87. expect(title).toBeInTheDocument();
  88. expect(value).toBeNull();
  89. });
  90. it("renders title and circular progress bar correctly", () => {
  91. const { getByText, getByRole } = render(<Progress title="Title" value={50} showValue={true} />);
  92. const title = getByText("Title");
  93. const progressBar = getByRole("progressbar");
  94. expect(title).toBeInTheDocument();
  95. expect(progressBar).toBeInTheDocument();
  96. });
  97. it("displays title above progress", () => {
  98. const { container } = render(<Progress titleAnchor="top" />);
  99. const box = container.querySelector(".MuiBox-root");
  100. expect(box).toHaveStyle("flex-direction: column");
  101. });
  102. it("displays title to the left of progress", () => {
  103. const { container } = render(<Progress titleAnchor="left" />);
  104. const box = container.querySelector(".MuiBox-root");
  105. expect(box).toHaveStyle("flex-direction: row");
  106. });
  107. it("displays title to the right of progress", () => {
  108. const { container } = render(<Progress titleAnchor="right" />);
  109. const box = container.querySelector(".MuiBox-root");
  110. expect(box).toHaveStyle("flex-direction: row-reverse");
  111. });
  112. it("displays title at the bottom of progress", () => {
  113. const { container } = render(<Progress titleAnchor="bottom" />);
  114. const box = container.querySelector(".MuiBox-root");
  115. expect(box).toHaveStyle("flex-direction: column-reverse");
  116. });
  117. it("displays the title at the bottom of the progress bar when the title anchor is undefined", () => {
  118. const { container } = render(<Progress />);
  119. const box = container.querySelector(".MuiBox-root");
  120. expect(box).toHaveStyle("flex-direction: column-reverse");
  121. });
  122. it("does not apply color to linear progress when color is undefined", () => {
  123. const { container } = render(<Progress linear value={50} />);
  124. const linearProgressBar = container.querySelector(".MuiLinearProgress-bar");
  125. expect(linearProgressBar).not.toHaveStyle("background: red");
  126. });
  127. it("does not apply color to circular progress when color is undefined", () => {
  128. const { container } = render(<Progress linear={false} value={50} />);
  129. const circularProgressCircle = container.querySelector(".MuiCircularProgress-circle");
  130. expect(circularProgressCircle).not.toHaveStyle("color: blue");
  131. });
  132. it("applies width to circular progress when width is defined as number", () => {
  133. const { container } = render(<Progress linear={false} value={50} width={100} />);
  134. const circularProgress = container.querySelector(".MuiCircularProgress-root");
  135. expect(circularProgress).toHaveStyle("width: 100px");
  136. expect(circularProgress).toHaveStyle("height: 100px");
  137. });
  138. it("applies width to circular progress when width is defined as number & title is defined", () => {
  139. const { container } = render(<Progress linear={false} value={50} width={100} title="Title" />);
  140. const circularProgress = container.querySelector(".MuiCircularProgress-root");
  141. expect(circularProgress).toHaveStyle("width: 100px");
  142. expect(circularProgress).toHaveStyle("height: 100px");
  143. });
  144. it("applies width to circular progress when width is defined as number & title, titleAnchor are defined", () => {
  145. const { container } = render(
  146. <Progress linear={false} value={50} width={100} title="Title" titleAnchor="top" />
  147. );
  148. const circularProgress = container.querySelector(".MuiCircularProgress-root");
  149. expect(circularProgress).toHaveStyle("width: 100px");
  150. expect(circularProgress).toHaveStyle("height: 100px");
  151. });
  152. it("applies width to circular progress when width is defined as number & title, titleAnchor, showValue are defined", () => {
  153. const { container } = render(
  154. <Progress linear={false} value={50} width={100} title="Title" titleAnchor="top" showValue={true} />
  155. );
  156. const circularProgress = container.querySelector(".MuiCircularProgress-root");
  157. expect(circularProgress).toHaveStyle("width: 100px");
  158. expect(circularProgress).toHaveStyle("height: 100px");
  159. });
  160. it("applies width to circular progress when width is defined as string", () => {
  161. const { container } = render(<Progress linear={false} value={50} width="10rem" />);
  162. const circularProgress = container.querySelector(".MuiCircularProgress-root");
  163. expect(circularProgress).toHaveStyle("width: 10rem");
  164. expect(circularProgress).toHaveStyle("height: 10rem");
  165. });
  166. it("applies width to circular progress when width is defined as string & title is defined", () => {
  167. const { container } = render(<Progress linear={false} value={50} width="10rem" title="Title" />);
  168. const circularProgress = container.querySelector(".MuiCircularProgress-root");
  169. expect(circularProgress).toHaveStyle("width: 10rem");
  170. expect(circularProgress).toHaveStyle("height: 10rem");
  171. });
  172. it("applies width to circular progress when width is defined as string & title, titleAnchor are defined", () => {
  173. const { container } = render(
  174. <Progress linear={false} value={50} width="10rem" title="Title" titleAnchor="top" />
  175. );
  176. const circularProgress = container.querySelector(".MuiCircularProgress-root");
  177. expect(circularProgress).toHaveStyle("width: 10rem");
  178. expect(circularProgress).toHaveStyle("height: 10rem");
  179. });
  180. it("applies width to circular progress when width is defined as string & title, titleAnchor, showValue are defined", () => {
  181. const { container } = render(
  182. <Progress linear={false} value={50} width="10rem" title="Title" titleAnchor="top" showValue={true} />
  183. );
  184. const circularProgress = container.querySelector(".MuiCircularProgress-root");
  185. expect(circularProgress).toHaveStyle("width: 10rem");
  186. expect(circularProgress).toHaveStyle("height: 10rem");
  187. });
  188. it("applies width to linear progress when width is defined as number", () => {
  189. const { container } = render(<Progress linear value={50} width={100} />);
  190. const linearProgress = container.querySelectorAll(".MuiBox-root")[0];
  191. expect(linearProgress).toHaveStyle("width: 100px");
  192. });
  193. it("applies width to linear progress when width is defined as number & title is defined", () => {
  194. const { container } = render(<Progress linear value={50} width={100} title="Title" />);
  195. const linearProgress = container.querySelectorAll(".MuiBox-root")[0];
  196. expect(linearProgress).toHaveStyle("width: 100px");
  197. });
  198. it("applies width to linear progress when width is defined as number & title, titleAnchor are defined", () => {
  199. const { container } = render(<Progress linear value={50} width={100} title="Title" titleAnchor="top" />);
  200. const linearProgress = container.querySelectorAll(".MuiBox-root")[0];
  201. expect(linearProgress).toHaveStyle("width: 100px");
  202. });
  203. it("applies width to linear progress when width is defined as number & title, titleAnchor, showValue are defined", () => {
  204. const { container } = render(
  205. <Progress linear showValue value={50} width={100} title="Title" titleAnchor="top" />
  206. );
  207. const linearProgress = container.querySelectorAll(".MuiBox-root")[0];
  208. expect(linearProgress).toHaveStyle("width: 100px");
  209. });
  210. it("applies width to linear progress when width is defined as string", () => {
  211. const { container } = render(<Progress linear value={50} width="10rem" />);
  212. const linearProgress = container.querySelectorAll(".MuiBox-root")[0];
  213. expect(linearProgress).toHaveStyle("width: 10rem");
  214. });
  215. it("applies width to linear progress when width is defined as string & title is defined", () => {
  216. const { container } = render(<Progress linear value={50} width="10rem" title="Title" />);
  217. const linearProgress = container.querySelectorAll(".MuiBox-root")[0];
  218. expect(linearProgress).toHaveStyle("width: 10rem");
  219. });
  220. it("applies width to linear progress when width is defined as string & title, titleAnchor are defined", () => {
  221. const { container } = render(<Progress linear value={50} width="10rem" title="Title" titleAnchor="top" />);
  222. const linearProgress = container.querySelectorAll(".MuiBox-root")[0];
  223. expect(linearProgress).toHaveStyle("width: 10rem");
  224. });
  225. it("applies width to linear progress when width is defined as string & title, titleAnchor, showValue are defined", () => {
  226. const { container } = render(
  227. <Progress linear value={50} width="10rem" title="Title" titleAnchor="top" showValue={true} />
  228. );
  229. const linearProgress = container.querySelectorAll(".MuiBox-root")[0];
  230. expect(linearProgress).toHaveStyle("width: 10rem");
  231. });
  232. });
  233. describe("Progress functions", () => {
  234. it("renders title and linear progress bar correctly", () => {
  235. const { getByText, getByRole } = render(<Progress title="Title" value={50} linear showValue={true} />);
  236. const title = getByText("Title");
  237. const progressBar = getByRole("progressbar");
  238. expect(title).toBeInTheDocument();
  239. expect(progressBar).toBeInTheDocument();
  240. });
  241. });