123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770 |
- /*
- * Copyright 2021-2024 Avaiga Private Limited
- *
- * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
- * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations under the License.
- */
- import React, { act } from "react";
- import { fireEvent, render, waitFor } from "@testing-library/react";
- import "@testing-library/jest-dom";
- import userEvent from "@testing-library/user-event";
- import PaginatedTable from "./PaginatedTable";
- import { TaipyContext } from "../../context/taipyContext";
- import { TaipyState, INITIAL_STATE } from "../../context/taipyReducers";
- import { TableValueType } from "./tableUtils";
- const valueKey = "0-99-Entity,Daily hospital occupancy--asc";
- const tableValue = {
- [valueKey]: {
- data: [
- {
- Day_str: "2020-04-01T00:00:00.000000Z",
- "Daily hospital occupancy": 856,
- Entity: "Austria",
- Code: "AUT",
- },
- {
- Day_str: "2020-04-02T00:00:00.000000Z",
- "Daily hospital occupancy": 823,
- Entity: "Austria",
- Code: "AUT",
- },
- {
- Day_str: "2020-04-03T00:00:00.000000Z",
- "Daily hospital occupancy": 829,
- Entity: "Austria",
- Code: "AUT",
- },
- {
- Day_str: "2020-04-04T00:00:00.000000Z",
- "Daily hospital occupancy": 826,
- Entity: "Austria",
- Code: "AUT",
- },
- {
- Day_str: "2020-04-05T00:00:00.000000Z",
- "Daily hospital occupancy": 712,
- Entity: "Austria",
- Code: "AUT",
- },
- {
- Day_str: "2020-04-06T00:00:00.000000Z",
- "Daily hospital occupancy": 824,
- Entity: "Austria",
- Code: "AUT",
- },
- {
- Day_str: "2020-04-07T00:00:00.000000Z",
- "Daily hospital occupancy": 857,
- Entity: "Austria",
- Code: "AUT",
- },
- {
- Day_str: "2020-04-08T00:00:00.000000Z",
- "Daily hospital occupancy": 829,
- Entity: "Austria",
- Code: "AUT",
- },
- {
- Day_str: "2020-04-09T00:00:00.000000Z",
- "Daily hospital occupancy": 820,
- Entity: "Austria",
- Code: "AUT",
- },
- {
- Day_str: "2020-04-10T00:00:00.000000Z",
- "Daily hospital occupancy": 771,
- Entity: "Austria",
- Code: "AUT",
- },
- ],
- rowcount: 14477,
- start: 0,
- },
- };
- const tableColumns = JSON.stringify({
- Entity: { dfid: "Entity" },
- "Daily hospital occupancy": { dfid: "Daily hospital occupancy", type: "int64" },
- });
- const changedValue = {
- [valueKey]: {
- data: [
- {
- Day_str: "2020-04-01T00:00:00.000000Z",
- "Daily hospital occupancy": 856,
- Entity: "Australia",
- Code: "AUS",
- },
- ],
- rowcount: 1,
- start: 0,
- },
- };
- const editableValue = {
- "0--1-bool,int,float,Code--asc": {
- data: [
- {
- bool: true,
- int: 856,
- float: 1.5,
- Code: "AUT",
- },
- {
- bool: false,
- int: 823,
- float: 2.5,
- Code: "ZZZ",
- },
- ],
- rowcount: 2,
- start: 0,
- },
- };
- const editableColumns = JSON.stringify({
- bool: { dfid: "bool", type: "bool", index: 0 },
- int: { dfid: "int", type: "int", index: 1 },
- float: { dfid: "float", type: "float", index: 2 },
- Code: { dfid: "Code", type: "str", index: 3 },
- });
- const buttonValue = {
- "0--1-bool,int,float,Code--asc": {
- data: [
- {
- bool: true,
- int: 856,
- float: 1.5,
- Code: "[Button Label](button action)",
- },
- {
- bool: false,
- int: 823,
- float: 2.5,
- Code: "ZZZ",
- },
- ],
- rowcount: 2,
- start: 0,
- },
- };
- const buttonColumns = JSON.stringify({
- bool: { dfid: "bool", type: "bool", index: 0 },
- int: { dfid: "int", type: "int", index: 1 },
- float: { dfid: "float", type: "float", index: 2 },
- Code: { dfid: "Code", type: "str", index: 3 },
- });
- const styledColumns = JSON.stringify({
- Entity: { dfid: "Entity" },
- "Daily hospital occupancy": {
- dfid: "Daily hospital occupancy",
- type: "int64",
- style: "some style function",
- tooltip: "some tooltip",
- },
- });
- describe("PaginatedTable Component", () => {
- it("renders", async () => {
- const { getByText } = render(<PaginatedTable data={undefined} defaultColumns={tableColumns} />);
- const elt = getByText("Entity");
- expect(elt.tagName).toBe("DIV");
- });
- it("displays the right info for class", async () => {
- const { getByText } = render(
- <PaginatedTable data={undefined} defaultColumns={tableColumns} className="taipy-table" />
- );
- const elt = getByText("Entity").closest("table");
- expect(elt?.parentElement?.parentElement?.parentElement).toHaveClass("taipy-table", "taipy-table-paginated");
- });
- it("is disabled", async () => {
- const { getByText } = render(<PaginatedTable data={undefined} defaultColumns={tableColumns} active={false} />);
- const elt = getByText("Entity");
- expect(elt.parentElement).toHaveClass("Mui-disabled");
- });
- it("is enabled by default", async () => {
- const { getByText } = render(<PaginatedTable data={undefined} defaultColumns={tableColumns} />);
- const elt = getByText("Entity");
- expect(elt.parentElement).not.toHaveClass("Mui-disabled");
- });
- it("is enabled by active", async () => {
- const { getByText, getAllByTestId } = render(
- <PaginatedTable data={undefined} defaultColumns={tableColumns} active={true} />
- );
- const elt = getByText("Entity");
- expect(elt.parentElement).not.toHaveClass("Mui-disabled");
- expect(getAllByTestId("ArrowDownwardIcon").length).toBeGreaterThan(0);
- });
- it("Hides sort icons when not active", async () => {
- const { queryByTestId } = render(
- <PaginatedTable data={undefined} defaultColumns={tableColumns} active={false} />
- );
- expect(queryByTestId("ArrowDownwardIcon")).toBeNull();
- });
- it("dispatch 2 well formed messages at first render", async () => {
- const dispatch = jest.fn();
- const state: TaipyState = INITIAL_STATE;
- render(
- <TaipyContext.Provider value={{ state, dispatch }}>
- <PaginatedTable
- id="table"
- data={undefined}
- defaultColumns={tableColumns}
- updateVars="varname=varname"
- />
- </TaipyContext.Provider>
- );
- expect(dispatch).toHaveBeenCalledWith({
- name: "",
- payload: { id: "table", names: ["varname"], refresh: false },
- type: "REQUEST_UPDATE",
- });
- expect(dispatch).toHaveBeenCalledWith({
- name: "",
- payload: {
- columns: ["Entity", "Daily hospital occupancy"],
- end: 99,
- id: "table",
- orderby: "",
- pagekey: valueKey,
- handlenan: false,
- sort: "asc",
- start: 0,
- aggregates: [],
- applies: undefined,
- styles: undefined,
- tooltips: undefined,
- filters: [],
- },
- type: "REQUEST_DATA_UPDATE",
- });
- });
- it("dispatch a well formed message on sort", async () => {
- const dispatch = jest.fn();
- const state: TaipyState = INITIAL_STATE;
- const { getByText } = render(
- <TaipyContext.Provider value={{ state, dispatch }}>
- <PaginatedTable data={undefined} defaultColumns={tableColumns} />
- </TaipyContext.Provider>
- );
- const elt = getByText("Entity");
- await userEvent.click(elt);
- expect(dispatch).toHaveBeenCalledWith({
- name: "",
- payload: {
- columns: ["Entity", "Daily hospital occupancy"],
- end: 99,
- id: undefined,
- orderby: "Entity",
- pagekey: "0-99-Entity,Daily hospital occupancy-Entity-asc",
- handlenan: false,
- sort: "asc",
- start: 0,
- aggregates: [],
- applies: undefined,
- styles: undefined,
- tooltips: undefined,
- filters: [],
- },
- type: "REQUEST_DATA_UPDATE",
- });
- });
- it("dispatch a well formed message on page change", async () => {
- const dispatch = jest.fn();
- const state: TaipyState = { ...INITIAL_STATE, data: { table: undefined } };
- const { getByLabelText, rerender } = render(
- <TaipyContext.Provider value={{ state, dispatch }}>
- <PaginatedTable
- id="table"
- data={state.data.table as undefined}
- defaultColumns={tableColumns}
- updateVars="varname=varname"
- />
- </TaipyContext.Provider>
- );
- const newState = { ...state, data: { ...state.data, table: tableValue } };
- rerender(
- <TaipyContext.Provider value={{ state: newState, dispatch }}>
- <PaginatedTable
- id="table"
- data={newState.data.table as TableValueType}
- defaultColumns={tableColumns}
- updateVars="varname=varname"
- />
- </TaipyContext.Provider>
- );
- const elt = getByLabelText("Go to next page");
- await userEvent.click(elt);
- expect(dispatch).toHaveBeenCalledWith({
- name: "",
- payload: {
- columns: ["Entity", "Daily hospital occupancy"],
- end: 199,
- id: "table",
- orderby: "",
- pagekey: "100-199-Entity,Daily hospital occupancy--asc",
- handlenan: false,
- sort: "asc",
- start: 100,
- aggregates: [],
- applies: undefined,
- styles: undefined,
- tooltips: undefined,
- filters: [],
- },
- type: "REQUEST_DATA_UPDATE",
- });
- });
- it("displays the received data", async () => {
- const dispatch = jest.fn();
- const state: TaipyState = INITIAL_STATE;
- const { getAllByText, rerender } = render(
- <TaipyContext.Provider value={{ state, dispatch }}>
- <PaginatedTable data={undefined} defaultColumns={tableColumns} />
- </TaipyContext.Provider>
- );
- rerender(
- <TaipyContext.Provider value={{ state, dispatch }}>
- <PaginatedTable data={tableValue as TableValueType} defaultColumns={tableColumns} />
- </TaipyContext.Provider>
- );
- const elts = getAllByText("Austria");
- expect(elts.length).toBeGreaterThan(1);
- expect(elts[0].tagName).toBe("SPAN");
- });
- it("displays the refreshed data", async () => {
- const dispatch = jest.fn();
- const state: TaipyState = INITIAL_STATE;
- const { getAllByText, rerender, queryByText } = render(
- <TaipyContext.Provider value={{ state, dispatch }}>
- <PaginatedTable data={undefined} defaultColumns={tableColumns} />
- </TaipyContext.Provider>
- );
- rerender(
- <TaipyContext.Provider value={{ state, dispatch }}>
- <PaginatedTable data={tableValue as TableValueType} defaultColumns={tableColumns} />
- </TaipyContext.Provider>
- );
- expect(getAllByText("Austria").length).toBeGreaterThan(1);
- rerender(
- <TaipyContext.Provider value={{ state, dispatch }}>
- <PaginatedTable data={changedValue as TableValueType} defaultColumns={tableColumns} />
- </TaipyContext.Provider>
- );
- expect(queryByText("Austria")).toBeNull();
- expect(getAllByText("Australia").length).toBe(1);
- });
- it("selects the rows", async () => {
- const dispatch = jest.fn();
- const state: TaipyState = INITIAL_STATE;
- const selected = [2, 4, 6];
- const { findAllByText, rerender } = render(
- <TaipyContext.Provider value={{ state, dispatch }}>
- <PaginatedTable data={undefined} defaultColumns={tableColumns} />
- </TaipyContext.Provider>
- );
- rerender(
- <TaipyContext.Provider value={{ state: { ...state }, dispatch }}>
- <PaginatedTable selected={selected} data={tableValue as TableValueType} defaultColumns={tableColumns} />
- </TaipyContext.Provider>
- );
- const elts = await waitFor(() => findAllByText("Austria"));
- elts.forEach((elt: HTMLElement, idx: number) =>
- selected.indexOf(idx) == -1
- ? expect(elt.parentElement?.parentElement?.parentElement?.parentElement).not.toHaveClass("Mui-selected")
- : expect(elt.parentElement?.parentElement?.parentElement?.parentElement).toHaveClass("Mui-selected")
- );
- expect(document.querySelectorAll(".Mui-selected")).toHaveLength(selected.length);
- });
- describe("Edit Mode", () => {
- it("displays the data with edit buttons", async () => {
- const dispatch = jest.fn();
- const state: TaipyState = INITIAL_STATE;
- const { getAllByTestId, queryAllByTestId, rerender } = render(
- <TaipyContext.Provider value={{ state, dispatch }}>
- <PaginatedTable
- data={undefined}
- defaultColumns={editableColumns}
- editable={true}
- onEdit="onEdit"
- showAll={true}
- />
- </TaipyContext.Provider>
- );
- rerender(
- <TaipyContext.Provider value={{ state: { ...state }, dispatch }}>
- <PaginatedTable
- data={editableValue as TableValueType}
- defaultColumns={editableColumns}
- editable={true}
- onEdit="onEdit"
- showAll={true}
- />
- </TaipyContext.Provider>
- );
- expect(document.querySelectorAll(".MuiSwitch-root")).not.toHaveLength(0);
- expect(getAllByTestId("EditIcon")).not.toHaveLength(0);
- expect(queryAllByTestId("CheckIcon")).toHaveLength(0);
- expect(queryAllByTestId("ClearIcon")).toHaveLength(0);
- });
- it("can edit", async () => {
- const dispatch = jest.fn();
- const state: TaipyState = INITIAL_STATE;
- const { getByTestId, queryAllByTestId, getAllByTestId, rerender } = render(
- <TaipyContext.Provider value={{ state, dispatch }}>
- <PaginatedTable
- data={undefined}
- defaultColumns={editableColumns}
- editable={true}
- onEdit="onEdit"
- showAll={true}
- />
- </TaipyContext.Provider>
- );
- rerender(
- <TaipyContext.Provider value={{ state: { ...state }, dispatch }}>
- <PaginatedTable
- data={editableValue as TableValueType}
- defaultColumns={editableColumns}
- editable={true}
- onEdit="onEdit"
- showAll={true}
- />
- </TaipyContext.Provider>
- );
- const edits = getAllByTestId("EditIcon");
- await userEvent.click(edits[0]);
- const checkButton = getByTestId("CheckIcon");
- getByTestId("ClearIcon");
- await userEvent.click(checkButton);
- expect(queryAllByTestId("CheckIcon")).toHaveLength(0);
- expect(queryAllByTestId("ClearIcon")).toHaveLength(0);
- await userEvent.click(edits[1]);
- const clearButton = getByTestId("ClearIcon");
- const input = document.querySelector("input");
- expect(input).not.toBeNull();
- if (input) {
- if (input.type == "checkbox") {
- await userEvent.click(input);
- } else {
- await userEvent.type(input, "1");
- }
- }
- await userEvent.click(clearButton);
- expect(queryAllByTestId("CheckIcon")).toHaveLength(0);
- expect(queryAllByTestId("ClearIcon")).toHaveLength(0);
- dispatch.mockClear();
- await userEvent.click(edits[2]);
- await userEvent.click(getByTestId("CheckIcon"));
- expect(dispatch).toHaveBeenCalledWith({
- name: "",
- payload: {
- action: "onEdit",
- args: [],
- col: "float",
- index: 0,
- user_value: 1.5,
- value: 1.5,
- },
- type: "SEND_ACTION_ACTION",
- });
- await userEvent.click(edits[3]);
- const input2 = document.querySelector("input");
- expect(input2).not.toBeNull();
- if (input2) {
- if (input2.type == "checkbox") {
- await userEvent.click(input2);
- await userEvent.click(getByTestId("ClearIcon"));
- } else {
- await userEvent.type(input2, "{Esc}");
- }
- }
- dispatch.mockClear();
- await userEvent.click(edits[5]);
- const input3 = document.querySelector("input");
- expect(input3).not.toBeNull();
- if (input3) {
- if (input3.type == "checkbox") {
- await userEvent.click(input3);
- await userEvent.click(getByTestId("CheckIcon"));
- } else {
- await userEvent.type(input3, "{Enter}");
- }
- }
- expect(dispatch).toHaveBeenCalledWith({
- name: "",
- payload: {
- action: "onEdit",
- args: [],
- col: "int",
- index: 1,
- user_value: 823,
- value: 823,
- },
- type: "SEND_ACTION_ACTION",
- });
- });
- });
- it("can add", async () => {
- const dispatch = jest.fn();
- const state: TaipyState = INITIAL_STATE;
- const { getByTestId } = render(
- <TaipyContext.Provider value={{ state, dispatch }}>
- <PaginatedTable
- data={undefined}
- defaultColumns={editableColumns}
- showAll={true}
- editable={true}
- onAdd="onAdd"
- />
- </TaipyContext.Provider>
- );
- dispatch.mockClear();
- const addButton = getByTestId("AddIcon");
- await userEvent.click(addButton);
- expect(dispatch).toHaveBeenCalledWith({
- name: "",
- payload: {
- action: "onAdd",
- args: [],
- index: 0,
- },
- type: "SEND_ACTION_ACTION",
- });
- });
- it("can delete", async () => {
- const dispatch = jest.fn();
- const state: TaipyState = INITIAL_STATE;
- const { getAllByTestId, getByTestId, queryAllByTestId, rerender } = render(
- <TaipyContext.Provider value={{ state, dispatch }}>
- <PaginatedTable
- data={undefined}
- defaultColumns={editableColumns}
- showAll={true}
- editable={true}
- onDelete="onDelete"
- />
- </TaipyContext.Provider>
- );
- rerender(
- <TaipyContext.Provider value={{ state: { ...state }, dispatch }}>
- <PaginatedTable
- data={editableValue as TableValueType}
- defaultColumns={editableColumns}
- editable={true}
- showAll={true}
- onDelete="onDelete"
- />
- </TaipyContext.Provider>
- );
- let deleteButtons = getAllByTestId("DeleteIcon");
- expect(deleteButtons).not.toHaveLength(0);
- await userEvent.click(deleteButtons[0]);
- const checkButton = getByTestId("CheckIcon");
- getByTestId("ClearIcon");
- await userEvent.click(checkButton);
- expect(queryAllByTestId("CheckIcon")).toHaveLength(0);
- expect(queryAllByTestId("ClearIcon")).toHaveLength(0);
- await userEvent.click(deleteButtons[1]);
- const clearButton = getByTestId("ClearIcon");
- const input = document.querySelector("input");
- expect(input).not.toBeNull();
- input && (await userEvent.type(input, "1"));
- await userEvent.click(clearButton);
- expect(queryAllByTestId("CheckIcon")).toHaveLength(0);
- expect(queryAllByTestId("ClearIcon")).toHaveLength(0);
- await userEvent.click(deleteButtons[2]);
- const input3 = document.querySelector("input");
- expect(input3).not.toBeNull();
- input3 && (await userEvent.type(input3, "{esc}"));
- deleteButtons = getAllByTestId("DeleteIcon");
- dispatch.mockClear();
- await userEvent.click(deleteButtons[0]);
- const input2 = document.querySelector("input");
- expect(input2).not.toBeNull();
- input2 && (await userEvent.type(input2, "{enter}"));
- expect(dispatch).toHaveBeenCalledWith({
- name: "",
- payload: {
- action: "onDelete",
- args: [],
- index: 0,
- },
- type: "SEND_ACTION_ACTION",
- });
- });
- it("can select", async () => {
- const dispatch = jest.fn();
- const state: TaipyState = INITIAL_STATE;
- const { getByText, rerender } = render(
- <TaipyContext.Provider value={{ state, dispatch }}>
- <PaginatedTable data={undefined} defaultColumns={editableColumns} showAll={true} onAction="onSelect" />
- </TaipyContext.Provider>
- );
- rerender(
- <TaipyContext.Provider value={{ state: { ...state }, dispatch }}>
- <PaginatedTable
- data={editableValue as TableValueType}
- defaultColumns={editableColumns}
- showAll={true}
- onAction="onSelect"
- />
- </TaipyContext.Provider>
- );
- dispatch.mockClear();
- const elt = getByText("823");
- await userEvent.click(elt);
- expect(dispatch).toHaveBeenCalledWith({
- name: "",
- payload: {
- action: "onSelect",
- args: [],
- col: "int",
- index: 1,
- reason: "click",
- value: undefined,
- },
- type: "SEND_ACTION_ACTION",
- });
- });
- it("can click on button", async () => {
- const dispatch = jest.fn();
- const state: TaipyState = INITIAL_STATE;
- const { getByText, rerender } = render(
- <TaipyContext.Provider value={{ state, dispatch }}>
- <PaginatedTable data={undefined} defaultColumns={editableColumns} showAll={true} onAction="onSelect" />
- </TaipyContext.Provider>
- );
- rerender(
- <TaipyContext.Provider value={{ state: { ...state }, dispatch }}>
- <PaginatedTable
- data={buttonValue as TableValueType}
- defaultColumns={buttonColumns}
- showAll={true}
- onAction="onSelect"
- />
- </TaipyContext.Provider>
- );
- dispatch.mockClear();
- const elt = getByText("Button Label");
- expect(elt.tagName).toBe("BUTTON");
- await userEvent.click(elt);
- expect(dispatch).toHaveBeenCalledWith({
- name: "",
- payload: {
- action: "onSelect",
- args: [],
- col: "Code",
- index: 0,
- reason: "button",
- value: "button action",
- },
- type: "SEND_ACTION_ACTION",
- });
- });
- it("should render correctly when style is applied to columns", async () => {
- const dispatch = jest.fn();
- const state: TaipyState = INITIAL_STATE;
- await waitFor(() => {
- render(
- <TaipyContext.Provider value={{ state, dispatch }}>
- <PaginatedTable
- data={tableValue}
- defaultColumns={styledColumns}
- lineStyle={"class_name=rows-bordered"}
- />
- </TaipyContext.Provider>
- );
- });
- const elt = document.querySelector('table[aria-labelledby="tableTitle"]');
- expect(elt).toBeInTheDocument();
- });
- it("should sort the table in ascending order", async () => {
- await waitFor(() => {
- render(<PaginatedTable data={tableValue} defaultColumns={tableColumns} />);
- });
- const elt = document.querySelector('svg[data-testid="ArrowDownwardIcon"]');
- act(() => {
- fireEvent.click(elt as Element);
- });
- expect(document.querySelector('th[aria-sort="ascending"]')).toBeInTheDocument();
- });
- it("should handle rows per page change", async () => {
- const { getByRole, queryByRole } = render(<PaginatedTable data={tableValue} defaultColumns={tableColumns} />);
- const rowsPerPageDropdown = getByRole("combobox");
- fireEvent.mouseDown(rowsPerPageDropdown);
- const option = queryByRole("option", { selected: false, name: "50" });
- fireEvent.click(option as Element);
- const table = document.querySelector(
- 'table[aria-labelledby="tableTitle"].MuiTable-root.MuiTable-stickyHeader.css-cz602z-MuiTable-root'
- );
- expect(table).toBeInTheDocument();
- });
- it("should allow all rows", async () => {
- const { getByRole, queryByRole } = render(
- <PaginatedTable data={tableValue} defaultColumns={tableColumns} allowAllRows={true} />
- );
- const rowsPerPageDropdown = getByRole("combobox");
- fireEvent.mouseDown(rowsPerPageDropdown);
- const option = queryByRole("option", { selected: false, name: "All" });
- expect(option).toBeInTheDocument();
- });
- it("should display row per page correctly", async () => {
- const { getByRole, queryByRole } = render(
- <PaginatedTable
- data={tableValue}
- defaultColumns={tableColumns}
- pageSizeOptions={JSON.stringify([10, 20, 30])}
- />
- );
- const rowsPerPageDropdown = getByRole("combobox");
- fireEvent.mouseDown(rowsPerPageDropdown);
- const option = queryByRole("option", { selected: false, name: "10" });
- expect(option).toBeInTheDocument();
- });
- it("logs error when pageSizeOptions prop is invalid", () => {
- // Create a spy on console.log
- const logSpy = jest.spyOn(console, "log");
- // Render the component with invalid pageSizeOptions prop
- render(<PaginatedTable data={tableValue} defaultColumns={tableColumns} pageSizeOptions={"not a valid json"} />);
- // Check if console.log was called with the expected arguments
- expect(logSpy).toHaveBeenCalledWith(
- "PaginatedTable pageSizeOptions is wrong ",
- "not a valid json",
- expect.any(Error)
- );
- // Clean up the spy
- logSpy.mockRestore();
- });
- });
|