AutoLoadingTable.spec.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 userEvent from "@testing-library/user-event";
  17. import AutoLoadingTable from "./AutoLoadingTable";
  18. import { TaipyContext } from "../../context/taipyContext";
  19. import { TaipyState, INITIAL_STATE } from "../../context/taipyReducers";
  20. import { TableValueType } from "./tableUtils";
  21. jest.mock(
  22. "react-virtualized-auto-sizer",
  23. () =>
  24. ({ children }: any) =>
  25. children({ height: 600, width: 600 })
  26. );
  27. const valueKey = "Infinite-Entity-asc";
  28. const tableValue = {
  29. [valueKey]: {
  30. data: [
  31. {
  32. Entity: "Austria",
  33. Day_str: "2020-04-01T00:00:00.000000Z",
  34. Code: "AUT",
  35. "Daily hospital occupancy": 856,
  36. },
  37. {
  38. Entity: "Austria",
  39. Day_str: "2020-04-02T00:00:00.000000Z",
  40. Code: "AUT",
  41. "Daily hospital occupancy": 823,
  42. },
  43. {
  44. Entity: "Austria",
  45. Day_str: "2020-04-03T00:00:00.000000Z",
  46. Code: "AUT",
  47. "Daily hospital occupancy": 829,
  48. },
  49. {
  50. Entity: "Austria",
  51. Day_str: "2020-04-04T00:00:00.000000Z",
  52. Code: "AUT",
  53. "Daily hospital occupancy": 826,
  54. },
  55. {
  56. Entity: "Austria",
  57. Day_str: "2020-04-05T00:00:00.000000Z",
  58. Code: "AUT",
  59. "Daily hospital occupancy": 712,
  60. },
  61. {
  62. Entity: "Austria",
  63. Day_str: "2020-04-06T00:00:00.000000Z",
  64. Code: "AUT",
  65. "Daily hospital occupancy": 824,
  66. },
  67. {
  68. Entity: "Austria",
  69. Day_str: "2020-04-07T00:00:00.000000Z",
  70. Code: "AUT",
  71. "Daily hospital occupancy": 857,
  72. },
  73. {
  74. Entity: "Austria",
  75. Day_str: "2020-04-08T00:00:00.000000Z",
  76. Code: "AUT",
  77. "Daily hospital occupancy": 829,
  78. },
  79. {
  80. Entity: "Austria",
  81. Day_str: "2020-04-09T00:00:00.000000Z",
  82. Code: "AUT",
  83. "Daily hospital occupancy": 820,
  84. },
  85. {
  86. Entity: "Austria",
  87. Day_str: "2020-04-10T00:00:00.000000Z",
  88. Code: "AUT",
  89. "Daily hospital occupancy": 771,
  90. },
  91. ],
  92. rowcount: 14477,
  93. start: 0,
  94. },
  95. };
  96. const tableColumns = JSON.stringify({ Entity: { dfid: "Entity" } });
  97. const tableWidthColumns = JSON.stringify({ Entity: { dfid: "Entity", width: "100px" }, Country: {dfid: "Country"} });
  98. describe("AutoLoadingTable Component", () => {
  99. it("renders", async () => {
  100. const { getByText } = render(<AutoLoadingTable data={undefined} defaultColumns={tableColumns} />);
  101. const elt = getByText("Entity");
  102. expect(elt.tagName).toBe("DIV");
  103. });
  104. it("displays the right info for class", async () => {
  105. const { getByText } = render(
  106. <AutoLoadingTable data={undefined} defaultColumns={tableColumns} className="taipy-table" />
  107. );
  108. const elt = getByText("Entity").closest("table");
  109. expect(elt?.parentElement?.parentElement?.parentElement).toHaveClass("taipy-table", "taipy-table-autoloading");
  110. });
  111. it("is disabled", async () => {
  112. const { getByText } = render(<AutoLoadingTable data={undefined} defaultColumns={tableColumns} active={false} />);
  113. const elt = getByText("Entity");
  114. expect(elt.parentElement).toHaveClass("Mui-disabled");
  115. });
  116. it("is enabled by default", async () => {
  117. const { getByText } = render(<AutoLoadingTable data={undefined} defaultColumns={tableColumns} />);
  118. const elt = getByText("Entity");
  119. expect(elt.parentElement).not.toHaveClass("Mui-disabled");
  120. });
  121. it("is enabled by active", async () => {
  122. const { getByText, getAllByTestId } = render(<AutoLoadingTable data={undefined} defaultColumns={tableColumns} active={true} />);
  123. const elt = getByText("Entity");
  124. expect(elt.parentElement).not.toHaveClass("Mui-disabled");
  125. expect(getAllByTestId("ArrowDownwardIcon").length).toBeGreaterThan(0);
  126. });
  127. it("hides sort icons when not active", async () => {
  128. const { queryByTestId } = render(<AutoLoadingTable data={undefined} defaultColumns={tableColumns} active={false} />);
  129. expect(queryByTestId("ArrowDownwardIcon")).toBeNull();
  130. });
  131. it("hides sort icons when not sortable", async () => {
  132. const { queryByTestId } = render(<AutoLoadingTable data={undefined} defaultColumns={tableColumns} sortable={false} />);
  133. expect(queryByTestId("ArrowDownwardIcon")).toBeNull();
  134. });
  135. it("set width if requested", async () => {
  136. const { getByText } = render(<AutoLoadingTable data={undefined} defaultColumns={tableWidthColumns} />);
  137. const header = getByText("Entity").closest("tr");
  138. expect(header?.firstChild).toHaveStyle({"min-width": "100px"});
  139. expect(header?.lastChild).toHaveStyle({"width": "100%"});
  140. });
  141. // keep getting undefined Error from jest, it seems to be linked to the setTimeout that makes the code run after the end of the test :-(
  142. // https://github.com/facebook/jest/issues/12262
  143. // Looks like the right way to handle this is to use jest fakeTimers and runAllTimers ...
  144. xit("dispatch a well formed message on sort", async () => {
  145. jest.useFakeTimers();
  146. const dispatch = jest.fn();
  147. const state: TaipyState = INITIAL_STATE;
  148. const { getByText } = render(
  149. <TaipyContext.Provider value={{ state, dispatch }}>
  150. <AutoLoadingTable data={undefined} defaultColumns={tableColumns} />
  151. </TaipyContext.Provider>
  152. );
  153. const elt = getByText("Entity");
  154. await userEvent.click(elt);
  155. jest.runAllTimers();
  156. expect(dispatch).toHaveBeenCalledWith({
  157. name: "",
  158. payload: {
  159. columns: ["Entity"],
  160. end: 99,
  161. id: undefined,
  162. infinite: true,
  163. orderby: "Entity",
  164. pagekey: "Infinite-Entity-Entity-asc",
  165. handlenan: false,
  166. sort: "asc",
  167. start: 0,
  168. aggregates: [],
  169. applies: undefined,
  170. cellClassNames: undefined,
  171. tooltips: undefined,
  172. filters: []
  173. },
  174. type: "REQUEST_DATA_UPDATE",
  175. });
  176. jest.useRealTimers();
  177. });
  178. it("dispatch a well formed message at first render", async () => {
  179. const dispatch = jest.fn();
  180. const state: TaipyState = INITIAL_STATE;
  181. render(
  182. <TaipyContext.Provider value={{ state, dispatch }}>
  183. <AutoLoadingTable id="table" data={undefined} defaultColumns={tableColumns} updateVars="varname=varname" />
  184. </TaipyContext.Provider>
  185. );
  186. expect(dispatch).toHaveBeenCalledWith({
  187. name: "",
  188. payload: { id: "table", names: ["varname"], refresh: false },
  189. type: "REQUEST_UPDATE",
  190. });
  191. });
  192. it("displays the received data", async () => {
  193. const dispatch = jest.fn();
  194. const state: TaipyState = { ...INITIAL_STATE, data: { table: undefined } };
  195. const { getAllByText, rerender } = render(
  196. <TaipyContext.Provider value={{ state, dispatch }}>
  197. <AutoLoadingTable
  198. id="table"
  199. data={state.data.table as undefined}
  200. defaultColumns={tableColumns}
  201. defaultKey={valueKey}
  202. pageSize={2}
  203. updateVars="varname=varname"
  204. />
  205. </TaipyContext.Provider>
  206. );
  207. const newState = { ...state, data: { ...state.data, table: tableValue } };
  208. rerender(
  209. <TaipyContext.Provider value={{ state: newState, dispatch }}>
  210. <AutoLoadingTable
  211. id="table"
  212. data={newState.data.table as TableValueType}
  213. defaultKey={valueKey}
  214. defaultColumns={tableColumns}
  215. pageSize={2}
  216. updateVars="varname=varname"
  217. />
  218. </TaipyContext.Provider>
  219. );
  220. const elements = getAllByText("Austria");
  221. expect(elements.length).toBeGreaterThan(1);
  222. expect(elements[0].tagName).toBe("SPAN");
  223. });
  224. it("selects the rows", async () => {
  225. const dispatch = jest.fn();
  226. const state: TaipyState = INITIAL_STATE;
  227. const selected = [2, 4, 6];
  228. const { getAllByText, rerender } = render(
  229. <TaipyContext.Provider value={{ state, dispatch }}>
  230. <AutoLoadingTable data={undefined} defaultColumns={tableColumns} pageSize={2} />
  231. </TaipyContext.Provider>
  232. );
  233. rerender(
  234. <TaipyContext.Provider value={{ state, dispatch }}>
  235. <AutoLoadingTable
  236. selected={selected}
  237. data={tableValue as TableValueType}
  238. defaultKey={valueKey}
  239. defaultColumns={tableColumns}
  240. pageSize={2}
  241. />
  242. </TaipyContext.Provider>
  243. );
  244. const elements = getAllByText("Austria");
  245. elements.forEach((elt: HTMLElement, idx: number) =>
  246. selected.indexOf(idx) == -1
  247. ? expect(elt.parentElement?.parentElement?.parentElement?.parentElement).not.toHaveClass("Mui-selected")
  248. : expect(elt.parentElement?.parentElement?.parentElement?.parentElement).toHaveClass("Mui-selected")
  249. );
  250. expect(document.querySelectorAll(".Mui-selected")).toHaveLength(selected.length);
  251. });
  252. });