1
0

Dialog.spec.tsx 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. * Copyright 2023 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 userEvent from "@testing-library/user-event";
  17. import axios from "axios";
  18. import Dialog from "./Dialog";
  19. import { TaipyContext } from "../../context/taipyContext";
  20. import { TaipyState, INITIAL_STATE } from "../../context/taipyReducers";
  21. import { HelmetProvider } from "react-helmet-async";
  22. jest.mock("axios");
  23. const mockedAxios = axios as jest.Mocked<typeof axios>;
  24. mockedAxios.get.mockRejectedValue("Network error: Something went wrong");
  25. mockedAxios.get.mockResolvedValue({ data: { jsx_no: '<div key="mock" data-testid="mocked"></div>' } });
  26. jest.mock("react-router-dom", () => ({
  27. ...jest.requireActual("react-router-dom"),
  28. useLocation: () => ({
  29. pathname: "pathname",
  30. }),
  31. }));
  32. beforeEach(() => {
  33. mockedAxios.get.mockClear();
  34. });
  35. describe("Dialog Component", () => {
  36. it("renders when value is true", async () => {
  37. const { getByText } = render(
  38. <HelmetProvider>
  39. <Dialog title="Dialog-Test-Title" page="page" open={true} />
  40. </HelmetProvider>
  41. );
  42. const elt = getByText("Dialog-Test-Title");
  43. expect(elt.tagName).toBe("H2");
  44. expect(mockedAxios.get).toHaveBeenCalled();
  45. });
  46. it("renders nothing when value is false", async () => {
  47. const { queryAllByText } = render(
  48. <HelmetProvider>
  49. <Dialog title="Dialog-Test-Title" page="page" open={false} />
  50. </HelmetProvider>
  51. );
  52. expect(queryAllByText("Dialog-Test-Title")).toHaveLength(0);
  53. const divs = document.getElementsByTagName("div");
  54. expect(divs).toHaveLength(1);
  55. expect(divs[0].childElementCount).toBe(0);
  56. });
  57. it("displays the right info for class", async () => {
  58. const wrapper = render(
  59. <HelmetProvider>
  60. <Dialog title="Dialog-Test-Title" page="page" open={true} className="taipy-dialog" />
  61. </HelmetProvider>
  62. );
  63. const elt = document.querySelector(".MuiDialog-root");
  64. expect(elt).toHaveClass("taipy-dialog");
  65. });
  66. it("displays the default value", async () => {
  67. const { getByText } = render(
  68. <HelmetProvider>
  69. <Dialog
  70. title="Dialog-Test-Title"
  71. page="page"
  72. defaultOpen="true"
  73. open={undefined as unknown as boolean}
  74. />
  75. </HelmetProvider>
  76. );
  77. getByText("Dialog-Test-Title");
  78. });
  79. it("doesn't show a button by default", async () => {
  80. const { getAllByRole } = render(
  81. <HelmetProvider>
  82. <Dialog
  83. title="Dialog-Test-Title"
  84. page="page"
  85. defaultOpen="true"
  86. open={undefined as unknown as boolean}
  87. />
  88. </HelmetProvider>
  89. );
  90. expect(getAllByRole("button")).toHaveLength(1);
  91. });
  92. it("shows one button when a label set", async () => {
  93. const { getAllByRole } = render(
  94. <HelmetProvider>
  95. <Dialog
  96. title="Dialog-Test-Title"
  97. page="page"
  98. defaultOpen="true"
  99. open={undefined as unknown as boolean}
  100. labels={JSON.stringify(["toto"])}
  101. />
  102. </HelmetProvider>
  103. );
  104. expect(getAllByRole("button")).toHaveLength(2);
  105. });
  106. it("shows 3 buttons when 3 labels set", async () => {
  107. const { getAllByRole } = render(
  108. <HelmetProvider>
  109. <Dialog
  110. title="Dialog-Test-Title"
  111. page="page"
  112. defaultOpen="true"
  113. open={undefined as unknown as boolean}
  114. labels={JSON.stringify(["toto", "titi", "toto"])}
  115. />
  116. </HelmetProvider>
  117. );
  118. expect(getAllByRole("button")).toHaveLength(4);
  119. });
  120. it("is disabled", async () => {
  121. const { getByText } = render(
  122. <HelmetProvider>
  123. <Dialog
  124. title="Dialog-Test-Title"
  125. page="page"
  126. open={true}
  127. active={false}
  128. labels={JSON.stringify(["testValidate", "testCancel"])}
  129. />
  130. </HelmetProvider>
  131. );
  132. expect(getByText("testValidate")).toBeDisabled();
  133. expect(getByText("testCancel")).toBeDisabled();
  134. });
  135. it("is enabled by default", async () => {
  136. const { getByText } = render(
  137. <HelmetProvider>
  138. <Dialog
  139. title="Dialog-Test-Title"
  140. page="page"
  141. open={true}
  142. labels={JSON.stringify(["testValidate", "testCancel"])}
  143. />
  144. </HelmetProvider>
  145. );
  146. expect(getByText("testValidate")).not.toBeDisabled();
  147. expect(getByText("testCancel")).not.toBeDisabled();
  148. });
  149. it("is enabled by active", async () => {
  150. const { getByText } = render(
  151. <HelmetProvider>
  152. <Dialog
  153. title="Dialog-Test-Title"
  154. page="page"
  155. open={true}
  156. active={true}
  157. labels={JSON.stringify(["testValidate", "testCancel"])}
  158. />
  159. </HelmetProvider>
  160. );
  161. expect(getByText("testValidate")).not.toBeDisabled();
  162. expect(getByText("testCancel")).not.toBeDisabled();
  163. });
  164. it("dispatch a well formed message on close icon press", async () => {
  165. const dispatch = jest.fn();
  166. const state: TaipyState = INITIAL_STATE;
  167. const { getByTitle } = render(
  168. <TaipyContext.Provider value={{ state, dispatch }}>
  169. <HelmetProvider>
  170. <Dialog
  171. id="testId"
  172. title="Dialog-Test-Title"
  173. page="page"
  174. open={true}
  175. active={true}
  176. onAction="testValidateAction"
  177. />
  178. </HelmetProvider>
  179. </TaipyContext.Provider>
  180. );
  181. await userEvent.click(getByTitle("Close"));
  182. expect(dispatch).toHaveBeenLastCalledWith({
  183. name: "testId",
  184. payload: { action: "testValidateAction", args: [-1] },
  185. type: "SEND_ACTION_ACTION",
  186. });
  187. });
  188. it("dispatch a well formed message on first button press", async () => {
  189. const dispatch = jest.fn();
  190. const state: TaipyState = INITIAL_STATE;
  191. const { getByText } = render(
  192. <TaipyContext.Provider value={{ state, dispatch }}>
  193. <HelmetProvider>
  194. <Dialog
  195. id="testId"
  196. title="Dialog-Test-Title"
  197. page="page"
  198. open={true}
  199. active={true}
  200. labels={JSON.stringify(["testValidate", "testCancel"])}
  201. onAction="testValidateAction"
  202. />
  203. </HelmetProvider>
  204. </TaipyContext.Provider>
  205. );
  206. await userEvent.click(getByText("testValidate"));
  207. expect(dispatch).toHaveBeenLastCalledWith({
  208. name: "testId",
  209. payload: { action: "testValidateAction", args: [0] },
  210. type: "SEND_ACTION_ACTION",
  211. });
  212. });
  213. it("dispatch a well formed message on third button press", async () => {
  214. const dispatch = jest.fn();
  215. const state: TaipyState = INITIAL_STATE;
  216. const { getByText } = render(
  217. <TaipyContext.Provider value={{ state, dispatch }}>
  218. <HelmetProvider>
  219. <Dialog
  220. id="testId"
  221. title="Dialog-Test-Title"
  222. page="page"
  223. open={true}
  224. active={true}
  225. labels={JSON.stringify(["testValidate", "testCancel", "Another One"])}
  226. onAction="testValidateAction"
  227. />
  228. </HelmetProvider>
  229. </TaipyContext.Provider>
  230. );
  231. await userEvent.click(getByText("Another One"));
  232. expect(dispatch).toHaveBeenLastCalledWith({
  233. name: "testId",
  234. payload: { action: "testValidateAction", args: [2] },
  235. type: "SEND_ACTION_ACTION",
  236. });
  237. });
  238. });