Expandable.spec.tsx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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, waitFor } from "@testing-library/react";
  15. import "@testing-library/jest-dom";
  16. import userEvent from "@testing-library/user-event";
  17. import Expandable from "./Expandable";
  18. import { INITIAL_STATE, TaipyState } from "../../context/taipyReducers";
  19. import { TaipyContext } from "../../context/taipyContext";
  20. describe("Expandable Component", () => {
  21. it("renders", async () => {
  22. const { getByText } = render(<Expandable title="foo">bar</Expandable>);
  23. const elt = getByText("foo");
  24. expect(elt.tagName).toBe("DIV");
  25. });
  26. it("displays the right info for string", async () => {
  27. const { getByText } = render(
  28. <Expandable title="foo" defaultTitle="bar" className="taipy-expandable">
  29. bar
  30. </Expandable>
  31. );
  32. const elt = getByText("foo");
  33. expect(elt.parentElement?.parentElement).toHaveClass("taipy-expandable");
  34. });
  35. it("displays the default value", async () => {
  36. const { getByText } = render(
  37. <Expandable defaultTitle="bar" title={undefined as unknown as string}>
  38. foobar
  39. </Expandable>
  40. );
  41. getByText("bar");
  42. });
  43. it("is disabled", async () => {
  44. const { getByRole } = render(
  45. <Expandable title="foo" active={false}>
  46. bar
  47. </Expandable>
  48. );
  49. const elt = getByRole("button");
  50. expect(elt).toHaveClass("Mui-disabled");
  51. });
  52. it("is enabled by default", async () => {
  53. const { getByRole } = render(<Expandable title="foo">bar</Expandable>);
  54. const elt = getByRole("button");
  55. expect(elt).not.toHaveClass("Mui-disabled");
  56. });
  57. it("is enabled by active", async () => {
  58. const { getByRole } = render(
  59. <Expandable title="foo" active={true}>
  60. bar
  61. </Expandable>
  62. );
  63. const elt = getByRole("button");
  64. expect(elt).not.toHaveClass("Mui-disabled");
  65. });
  66. it("is collapsed", async () => {
  67. const { getByText } = render(
  68. <Expandable title="foo" expanded={false}>
  69. bar
  70. </Expandable>
  71. );
  72. const elt = getByText("bar");
  73. const div = elt.closest("div.MuiCollapse-root");
  74. expect(div).toBeInTheDocument();
  75. expect(div).toHaveClass("MuiCollapse-hidden");
  76. expect(div).toHaveStyle({ height: "0px" });
  77. });
  78. it("is not collapsed by default", async () => {
  79. const { getByText } = render(<Expandable title="foo">bar</Expandable>);
  80. const elt = getByText("bar");
  81. const div = elt.closest("div.MuiCollapse-root");
  82. expect(div).toBeInTheDocument();
  83. expect(div).not.toHaveClass("MuiCollapse-hidden");
  84. expect(div).not.toHaveStyle({ height: "0px" });
  85. });
  86. it("is not collapsed by expanded", async () => {
  87. const { getByText } = render(
  88. <Expandable title="foo" expanded={true}>
  89. bar
  90. </Expandable>
  91. );
  92. const elt = getByText("bar");
  93. const div = elt.closest("div.MuiCollapse-root");
  94. expect(div).toBeInTheDocument();
  95. expect(div).not.toHaveClass("MuiCollapse-hidden");
  96. expect(div).not.toHaveStyle({ height: "0px" });
  97. });
  98. it("dispatch a well formed message", async () => {
  99. const dispatch = jest.fn();
  100. const state: TaipyState = INITIAL_STATE;
  101. const { getByText } = render(
  102. <TaipyContext.Provider value={{ state, dispatch }}>
  103. <Expandable title="foo" expanded={true} updateVars="expanded=expanded_var">
  104. bar
  105. </Expandable>
  106. </TaipyContext.Provider>
  107. );
  108. const elt = getByText("foo");
  109. const div = elt.nextElementSibling;
  110. div && await userEvent.click(div);
  111. await waitFor(() => expect(dispatch).toHaveBeenCalled());
  112. expect(dispatch).toHaveBeenLastCalledWith({
  113. name: "expanded_var",
  114. payload: { value: false },
  115. propagate: true,
  116. type: "SEND_UPDATE_ACTION",
  117. });
  118. });
  119. });