PaginatedTable.spec.tsx 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  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, waitFor } from "@testing-library/react";
  15. import "@testing-library/jest-dom";
  16. import userEvent from "@testing-library/user-event";
  17. import PaginatedTable from "./PaginatedTable";
  18. import { TaipyContext } from "../../context/taipyContext";
  19. import { TaipyState, INITIAL_STATE } from "../../context/taipyReducers";
  20. import { TableValueType } from "./tableUtils";
  21. const valueKey = "0-99-Entity,Daily hospital occupancy--asc";
  22. const tableValue = {
  23. [valueKey]: {
  24. data: [
  25. {
  26. Day_str: "2020-04-01T00:00:00.000000Z",
  27. "Daily hospital occupancy": 856,
  28. Entity: "Austria",
  29. Code: "AUT",
  30. },
  31. {
  32. Day_str: "2020-04-02T00:00:00.000000Z",
  33. "Daily hospital occupancy": 823,
  34. Entity: "Austria",
  35. Code: "AUT",
  36. },
  37. {
  38. Day_str: "2020-04-03T00:00:00.000000Z",
  39. "Daily hospital occupancy": 829,
  40. Entity: "Austria",
  41. Code: "AUT",
  42. },
  43. {
  44. Day_str: "2020-04-04T00:00:00.000000Z",
  45. "Daily hospital occupancy": 826,
  46. Entity: "Austria",
  47. Code: "AUT",
  48. },
  49. {
  50. Day_str: "2020-04-05T00:00:00.000000Z",
  51. "Daily hospital occupancy": 712,
  52. Entity: "Austria",
  53. Code: "AUT",
  54. },
  55. {
  56. Day_str: "2020-04-06T00:00:00.000000Z",
  57. "Daily hospital occupancy": 824,
  58. Entity: "Austria",
  59. Code: "AUT",
  60. },
  61. {
  62. Day_str: "2020-04-07T00:00:00.000000Z",
  63. "Daily hospital occupancy": 857,
  64. Entity: "Austria",
  65. Code: "AUT",
  66. },
  67. {
  68. Day_str: "2020-04-08T00:00:00.000000Z",
  69. "Daily hospital occupancy": 829,
  70. Entity: "Austria",
  71. Code: "AUT",
  72. },
  73. {
  74. Day_str: "2020-04-09T00:00:00.000000Z",
  75. "Daily hospital occupancy": 820,
  76. Entity: "Austria",
  77. Code: "AUT",
  78. },
  79. {
  80. Day_str: "2020-04-10T00:00:00.000000Z",
  81. "Daily hospital occupancy": 771,
  82. Entity: "Austria",
  83. Code: "AUT",
  84. },
  85. ],
  86. rowcount: 14477,
  87. start: 0,
  88. },
  89. };
  90. const tableColumns = JSON.stringify({
  91. Entity: { dfid: "Entity" },
  92. "Daily hospital occupancy": { dfid: "Daily hospital occupancy", type: "int64" },
  93. });
  94. const editableValue = {
  95. "0--1-bool,int,float,Code--asc": {
  96. data: [
  97. {
  98. bool: true,
  99. int: 856,
  100. float: 1.5,
  101. Code: "AUT",
  102. },
  103. {
  104. bool: false,
  105. int: 823,
  106. float: 2.5,
  107. Code: "ZZZ",
  108. },
  109. ],
  110. rowcount: 2,
  111. start: 0,
  112. },
  113. };
  114. const editableColumns = JSON.stringify({
  115. bool: { dfid: "bool", type: "bool", index: 0 },
  116. int: { dfid: "int", type: "int", index: 1 },
  117. float: { dfid: "float", type: "float", index: 2 },
  118. Code: { dfid: "Code", type: "str", index: 3 },
  119. });
  120. describe("PaginatedTable Component", () => {
  121. it("renders", async () => {
  122. const { getByText } = render(<PaginatedTable data={undefined} defaultColumns={tableColumns} />);
  123. const elt = getByText("Entity");
  124. expect(elt.tagName).toBe("DIV");
  125. });
  126. it("displays the right info for class", async () => {
  127. const { getByText } = render(
  128. <PaginatedTable data={undefined} defaultColumns={tableColumns} className="taipy-table" />
  129. );
  130. const elt = getByText("Entity").closest("table");
  131. expect(elt?.parentElement?.parentElement?.parentElement).toHaveClass("taipy-table", "taipy-table-paginated");
  132. });
  133. it("is disabled", async () => {
  134. const { getByText } = render(<PaginatedTable data={undefined} defaultColumns={tableColumns} active={false} />);
  135. const elt = getByText("Entity");
  136. expect(elt.parentElement).toHaveClass("Mui-disabled");
  137. });
  138. it("is enabled by default", async () => {
  139. const { getByText } = render(<PaginatedTable data={undefined} defaultColumns={tableColumns} />);
  140. const elt = getByText("Entity");
  141. expect(elt.parentElement).not.toHaveClass("Mui-disabled");
  142. });
  143. it("is enabled by active", async () => {
  144. const { getByText, getAllByTestId } = render(<PaginatedTable data={undefined} defaultColumns={tableColumns} active={true} />);
  145. const elt = getByText("Entity");
  146. expect(elt.parentElement).not.toHaveClass("Mui-disabled");
  147. expect(getAllByTestId("ArrowDownwardIcon").length).toBeGreaterThan(0);
  148. });
  149. it("Hides sort icons when not active", async () => {
  150. const { queryByTestId } = render(<PaginatedTable data={undefined} defaultColumns={tableColumns} active={false} />);
  151. expect(queryByTestId("ArrowDownwardIcon")).toBeNull();
  152. });
  153. it("dispatch 2 well formed messages at first render", async () => {
  154. const dispatch = jest.fn();
  155. const state: TaipyState = INITIAL_STATE;
  156. render(
  157. <TaipyContext.Provider value={{ state, dispatch }}>
  158. <PaginatedTable id="table" data={undefined} defaultColumns={tableColumns} updateVars="varname=varname" />
  159. </TaipyContext.Provider>
  160. );
  161. expect(dispatch).toHaveBeenCalledWith({
  162. name: "",
  163. payload: { id: "table", names: ["varname"], refresh: false },
  164. type: "REQUEST_UPDATE",
  165. });
  166. expect(dispatch).toHaveBeenCalledWith({
  167. name: "",
  168. payload: {
  169. columns: ["Entity", "Daily hospital occupancy"],
  170. end: 99,
  171. id: "table",
  172. orderby: "",
  173. pagekey: valueKey,
  174. handlenan: false,
  175. sort: "asc",
  176. start: 0,
  177. aggregates: [],
  178. applies: undefined,
  179. styles: undefined,
  180. tooltips: undefined,
  181. filters: [],
  182. },
  183. type: "REQUEST_DATA_UPDATE",
  184. });
  185. });
  186. it("dispatch a well formed message on sort", async () => {
  187. const dispatch = jest.fn();
  188. const state: TaipyState = INITIAL_STATE;
  189. const { getByText } = render(
  190. <TaipyContext.Provider value={{ state, dispatch }}>
  191. <PaginatedTable data={undefined} defaultColumns={tableColumns} />
  192. </TaipyContext.Provider>
  193. );
  194. const elt = getByText("Entity");
  195. await userEvent.click(elt);
  196. expect(dispatch).toHaveBeenCalledWith({
  197. name: "",
  198. payload: {
  199. columns: ["Entity", "Daily hospital occupancy"],
  200. end: 99,
  201. id: undefined,
  202. orderby: "Entity",
  203. pagekey: "0-99-Entity,Daily hospital occupancy-Entity-asc",
  204. handlenan: false,
  205. sort: "asc",
  206. start: 0,
  207. aggregates: [],
  208. applies: undefined,
  209. styles: undefined,
  210. tooltips: undefined,
  211. filters: [],
  212. },
  213. type: "REQUEST_DATA_UPDATE",
  214. });
  215. });
  216. it("dispatch a well formed message on page change", async () => {
  217. const dispatch = jest.fn();
  218. const state: TaipyState = { ...INITIAL_STATE, data: { table: undefined } };
  219. const { getByLabelText, rerender } = render(
  220. <TaipyContext.Provider value={{ state, dispatch }}>
  221. <PaginatedTable
  222. id="table"
  223. data={state.data.table as undefined}
  224. defaultColumns={tableColumns}
  225. updateVars="varname=varname"
  226. />
  227. </TaipyContext.Provider>
  228. );
  229. const newState = { ...state, data: { ...state.data, table: tableValue } };
  230. rerender(
  231. <TaipyContext.Provider value={{ state: newState, dispatch }}>
  232. <PaginatedTable
  233. id="table"
  234. data={newState.data.table as TableValueType}
  235. defaultColumns={tableColumns}
  236. updateVars="varname=varname"
  237. />
  238. </TaipyContext.Provider>
  239. );
  240. const elt = getByLabelText("Go to next page");
  241. await userEvent.click(elt);
  242. expect(dispatch).toHaveBeenCalledWith({
  243. name: "",
  244. payload: {
  245. columns: ["Entity", "Daily hospital occupancy"],
  246. end: 199,
  247. id: "table",
  248. orderby: "",
  249. pagekey: "100-199-Entity,Daily hospital occupancy--asc",
  250. handlenan: false,
  251. sort: "asc",
  252. start: 100,
  253. aggregates: [],
  254. applies: undefined,
  255. styles: undefined,
  256. tooltips: undefined,
  257. filters: [],
  258. },
  259. type: "REQUEST_DATA_UPDATE",
  260. });
  261. });
  262. it("displays the received data", async () => {
  263. const dispatch = jest.fn();
  264. const state: TaipyState = INITIAL_STATE;
  265. const { getAllByText, rerender } = render(
  266. <TaipyContext.Provider value={{ state, dispatch }}>
  267. <PaginatedTable data={undefined} defaultColumns={tableColumns} />
  268. </TaipyContext.Provider>
  269. );
  270. rerender(
  271. <TaipyContext.Provider value={{ state, dispatch }}>
  272. <PaginatedTable data={tableValue as TableValueType} defaultColumns={tableColumns} />
  273. </TaipyContext.Provider>
  274. );
  275. const elts = getAllByText("Austria");
  276. expect(elts.length).toBeGreaterThan(1);
  277. expect(elts[0].tagName).toBe("SPAN");
  278. });
  279. it("selects the rows", async () => {
  280. const dispatch = jest.fn();
  281. const state: TaipyState = INITIAL_STATE;
  282. const selected = [2, 4, 6];
  283. const { findAllByText, rerender } = render(
  284. <TaipyContext.Provider value={{ state, dispatch }}>
  285. <PaginatedTable data={undefined} defaultColumns={tableColumns} />
  286. </TaipyContext.Provider>
  287. );
  288. rerender(
  289. <TaipyContext.Provider value={{ state: { ...state }, dispatch }}>
  290. <PaginatedTable selected={selected} data={tableValue as TableValueType} defaultColumns={tableColumns} />
  291. </TaipyContext.Provider>
  292. );
  293. const elts = await waitFor(() => findAllByText("Austria"));
  294. elts.forEach((elt: HTMLElement, idx: number) =>
  295. selected.indexOf(idx) == -1
  296. ? expect(elt.parentElement?.parentElement?.parentElement).not.toHaveClass("Mui-selected")
  297. : expect(elt.parentElement?.parentElement?.parentElement).toHaveClass("Mui-selected")
  298. );
  299. expect(document.querySelectorAll(".Mui-selected")).toHaveLength(selected.length);
  300. });
  301. describe("Edit Mode", () => {
  302. it("displays the data with edit buttons", async () => {
  303. const dispatch = jest.fn();
  304. const state: TaipyState = INITIAL_STATE;
  305. const { getAllByTestId, queryAllByTestId, rerender } = render(
  306. <TaipyContext.Provider value={{ state, dispatch }}>
  307. <PaginatedTable data={undefined} defaultColumns={editableColumns} onEdit="onEdit" showAll={true} />
  308. </TaipyContext.Provider>
  309. );
  310. rerender(
  311. <TaipyContext.Provider value={{ state: { ...state }, dispatch }}>
  312. <PaginatedTable
  313. data={editableValue as TableValueType}
  314. defaultColumns={editableColumns}
  315. onEdit="onEdit"
  316. showAll={true}
  317. />
  318. </TaipyContext.Provider>
  319. );
  320. expect(document.querySelectorAll(".MuiSwitch-root")).not.toHaveLength(0);
  321. expect(getAllByTestId("EditIcon")).not.toHaveLength(0);
  322. expect(queryAllByTestId("CheckIcon")).toHaveLength(0);
  323. expect(queryAllByTestId("ClearIcon")).toHaveLength(0);
  324. });
  325. it("can edit", async () => {
  326. const dispatch = jest.fn();
  327. const state: TaipyState = INITIAL_STATE;
  328. const { getByTestId, queryAllByTestId, getAllByTestId, rerender } = render(
  329. <TaipyContext.Provider value={{ state, dispatch }}>
  330. <PaginatedTable data={undefined} defaultColumns={editableColumns} onEdit="onEdit" showAll={true} />
  331. </TaipyContext.Provider>
  332. );
  333. rerender(
  334. <TaipyContext.Provider value={{ state: { ...state }, dispatch }}>
  335. <PaginatedTable
  336. data={editableValue as TableValueType}
  337. defaultColumns={editableColumns}
  338. onEdit="onEdit"
  339. showAll={true}
  340. />
  341. </TaipyContext.Provider>
  342. );
  343. const edits = getAllByTestId("EditIcon");
  344. await userEvent.click(edits[0]);
  345. const checkButton = getByTestId("CheckIcon");
  346. getByTestId("ClearIcon");
  347. await userEvent.click(checkButton);
  348. expect(queryAllByTestId("CheckIcon")).toHaveLength(0);
  349. expect(queryAllByTestId("ClearIcon")).toHaveLength(0);
  350. await userEvent.click(edits[1]);
  351. const clearButton = getByTestId("ClearIcon");
  352. const input = document.querySelector("input");
  353. expect(input).not.toBeNull();
  354. if (input) {
  355. if (input.type == "checkbox") {
  356. await userEvent.click(input);
  357. } else {
  358. await userEvent.type(input, "1");
  359. }
  360. }
  361. await userEvent.click(clearButton);
  362. expect(queryAllByTestId("CheckIcon")).toHaveLength(0);
  363. expect(queryAllByTestId("ClearIcon")).toHaveLength(0);
  364. dispatch.mockClear();
  365. await userEvent.click(edits[2]);
  366. await userEvent.click(getByTestId("CheckIcon"));
  367. expect(dispatch).toHaveBeenCalledWith({
  368. name: "",
  369. payload: {
  370. action: "onEdit",
  371. args: [],
  372. col: "float",
  373. index: 0,
  374. user_value: 1.5,
  375. value: 1.5,
  376. },
  377. type: "SEND_ACTION_ACTION",
  378. });
  379. await userEvent.click(edits[3]);
  380. const input2 = document.querySelector("input");
  381. expect(input2).not.toBeNull();
  382. if (input2) {
  383. if (input2.type == "checkbox") {
  384. await userEvent.click(input2);
  385. await userEvent.click(getByTestId("ClearIcon"));
  386. } else {
  387. await userEvent.type(input2, "{Esc}");
  388. }
  389. }
  390. dispatch.mockClear();
  391. await userEvent.click(edits[5]);
  392. const input3 = document.querySelector("input");
  393. expect(input3).not.toBeNull();
  394. if (input3) {
  395. if (input3.type == "checkbox") {
  396. await userEvent.click(input3);
  397. await userEvent.click(getByTestId("CheckIcon"));
  398. } else {
  399. await userEvent.type(input3, "{Enter}");
  400. }
  401. }
  402. expect(dispatch).toHaveBeenCalledWith({
  403. name: "",
  404. payload: {
  405. action: "onEdit",
  406. args: [],
  407. col: "int",
  408. index: 1,
  409. user_value: 823,
  410. value: 823,
  411. },
  412. type: "SEND_ACTION_ACTION",
  413. });
  414. });
  415. });
  416. it("can add", async () => {
  417. const dispatch = jest.fn();
  418. const state: TaipyState = INITIAL_STATE;
  419. const { getByTestId } = render(
  420. <TaipyContext.Provider value={{ state, dispatch }}>
  421. <PaginatedTable data={undefined} defaultColumns={editableColumns} showAll={true} onAdd="onAdd" />
  422. </TaipyContext.Provider>
  423. );
  424. dispatch.mockClear();
  425. const addButton = getByTestId("AddIcon");
  426. await userEvent.click(addButton);
  427. expect(dispatch).toHaveBeenCalledWith({
  428. name: "",
  429. payload: {
  430. action: "onAdd",
  431. args: [],
  432. index: 0,
  433. },
  434. type: "SEND_ACTION_ACTION",
  435. });
  436. });
  437. it("can delete", async () => {
  438. const dispatch = jest.fn();
  439. const state: TaipyState = INITIAL_STATE;
  440. const { getAllByTestId, getByTestId, queryAllByTestId, rerender } = render(
  441. <TaipyContext.Provider value={{ state, dispatch }}>
  442. <PaginatedTable data={undefined} defaultColumns={editableColumns} showAll={true} onDelete="onDelete" />
  443. </TaipyContext.Provider>
  444. );
  445. rerender(
  446. <TaipyContext.Provider value={{ state: { ...state }, dispatch }}>
  447. <PaginatedTable
  448. data={editableValue as TableValueType}
  449. defaultColumns={editableColumns}
  450. showAll={true}
  451. onDelete="onDelete"
  452. />
  453. </TaipyContext.Provider>
  454. );
  455. let deleteButtons = getAllByTestId("DeleteIcon");
  456. expect(deleteButtons).not.toHaveLength(0);
  457. await userEvent.click(deleteButtons[0]);
  458. const checkButton = getByTestId("CheckIcon");
  459. getByTestId("ClearIcon");
  460. await userEvent.click(checkButton);
  461. expect(queryAllByTestId("CheckIcon")).toHaveLength(0);
  462. expect(queryAllByTestId("ClearIcon")).toHaveLength(0);
  463. await userEvent.click(deleteButtons[1]);
  464. const clearButton = getByTestId("ClearIcon");
  465. const input = document.querySelector("input");
  466. expect(input).not.toBeNull();
  467. input && (await userEvent.type(input, "1"));
  468. await userEvent.click(clearButton);
  469. expect(queryAllByTestId("CheckIcon")).toHaveLength(0);
  470. expect(queryAllByTestId("ClearIcon")).toHaveLength(0);
  471. await userEvent.click(deleteButtons[2]);
  472. const input3 = document.querySelector("input");
  473. expect(input3).not.toBeNull();
  474. input3 && (await userEvent.type(input3, "{esc}"));
  475. deleteButtons = getAllByTestId("DeleteIcon");
  476. dispatch.mockClear();
  477. await userEvent.click(deleteButtons[0]);
  478. const input2 = document.querySelector("input");
  479. expect(input2).not.toBeNull();
  480. input2 && (await userEvent.type(input2, "{enter}"));
  481. expect(dispatch).toHaveBeenCalledWith({
  482. name: "",
  483. payload: {
  484. action: "onDelete",
  485. args: [],
  486. index: 0,
  487. },
  488. type: "SEND_ACTION_ACTION",
  489. });
  490. });
  491. it("can select", async () => {
  492. const dispatch = jest.fn();
  493. const state: TaipyState = INITIAL_STATE;
  494. const { getByText, rerender } = render(
  495. <TaipyContext.Provider value={{ state, dispatch }}>
  496. <PaginatedTable data={undefined} defaultColumns={editableColumns} showAll={true} onAction="onSelect" />
  497. </TaipyContext.Provider>
  498. );
  499. rerender(
  500. <TaipyContext.Provider value={{ state: { ...state }, dispatch }}>
  501. <PaginatedTable
  502. data={editableValue as TableValueType}
  503. defaultColumns={editableColumns}
  504. showAll={true}
  505. onAction="onSelect"
  506. />
  507. </TaipyContext.Provider>
  508. );
  509. dispatch.mockClear();
  510. const elt = getByText("823");
  511. await userEvent.click(elt);
  512. expect(dispatch).toHaveBeenCalledWith({
  513. name: "",
  514. payload: {
  515. action: "onSelect",
  516. args: [],
  517. col: "int",
  518. index: 1,
  519. },
  520. type: "SEND_ACTION_ACTION",
  521. });
  522. });
  523. });