StatusList.spec.tsx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 { StatusType } from "./Status";
  18. import StatusList, { getStatusStrValue, getStatusIntValue } from "./StatusList";
  19. const statuses = [
  20. { status: "info", message: "info" },
  21. ["error", "error"],
  22. { status: "warning", message: "warning" },
  23. { status: "success", message: "success" },
  24. ] as Array<[string, string] | StatusType>;
  25. describe("StatusList Component", () => {
  26. it("renders", async () => {
  27. const { getByText } = render(<StatusList value={statuses} />);
  28. const elt = getByText("4 statuses");
  29. const av = getByText("E");
  30. expect(elt.tagName).toBe("SPAN");
  31. expect(av.tagName).toBe("DIV");
  32. });
  33. it("uses the class", async () => {
  34. const { getByText } = render(<StatusList value={statuses} className="taipy-status" />);
  35. const elt = getByText("4 statuses");
  36. expect(elt.parentElement).toHaveClass("taipy-status");
  37. });
  38. it("can be opened when more than 1 status", async () => {
  39. const { getByTestId } = render(<StatusList value={statuses} />);
  40. const elt = getByTestId("ArrowDownwardIcon");
  41. expect(elt).toBeInTheDocument();
  42. });
  43. it("cannot be opened when 1 status", async () => {
  44. const { queryAllByRole } = render(<StatusList value={statuses[0]} />);
  45. expect(queryAllByRole("button")).toHaveLength(0);
  46. });
  47. it("displays a default status", async () => {
  48. const { getByText } = render(<StatusList value={[]} />);
  49. getByText("No Status");
  50. getByText("I");
  51. });
  52. it("opens on click", async () => {
  53. const { getByTestId, getByText } = render(<StatusList value={statuses} />);
  54. const elt = getByTestId("ArrowDownwardIcon");
  55. await userEvent.click(elt);
  56. const selt = getByText("info");
  57. expect(selt.parentElement?.parentElement?.childElementCount).toBe(4);
  58. });
  59. it("hide closed statuses", async () => {
  60. const { getByTestId, queryAllByTestId } = render(<StatusList value={statuses} />);
  61. const elt = getByTestId("ArrowDownwardIcon");
  62. await userEvent.click(elt);
  63. const icons = queryAllByTestId("CancelIcon");
  64. expect(icons).toHaveLength(4);
  65. await userEvent.click(icons[0]);
  66. expect(queryAllByTestId("CancelIcon")).toHaveLength(3);
  67. });
  68. it("should return 0 for unknown status", () => {
  69. expect(getStatusIntValue("unknown")).toBe(0);
  70. expect(getStatusIntValue("")).toBe(0);
  71. expect(getStatusIntValue("a")).toBe(0);
  72. expect(getStatusIntValue("z")).toBe(0);
  73. });
  74. it('should return "info" for status 1', () => {
  75. expect(getStatusStrValue(1)).toBe("info");
  76. });
  77. it('should return "success" for status 2', () => {
  78. expect(getStatusStrValue(2)).toBe("success");
  79. });
  80. it('should return "warning" for status 3', () => {
  81. expect(getStatusStrValue(3)).toBe("warning");
  82. });
  83. it('should return "error" for status 4', () => {
  84. expect(getStatusStrValue(4)).toBe("error");
  85. });
  86. it('should return "unknown" for any other status', () => {
  87. expect(getStatusStrValue(0)).toBe("unknown");
  88. expect(getStatusStrValue(5)).toBe("unknown");
  89. expect(getStatusStrValue(-1)).toBe("unknown");
  90. });
  91. it("should handle invalid JSON in defaultValue", () => {
  92. const invalidDefaultValue = "{invalidJson}";
  93. const consoleSpy = jest.spyOn(console, "info").mockImplementation(() => {});
  94. const { getByText } = render(<StatusList value={undefined!} defaultValue={invalidDefaultValue} />);
  95. expect(consoleSpy).toHaveBeenCalledWith(expect.stringContaining("Cannot parse status value"));
  96. const elt = getByText("No Status");
  97. expect(elt).toBeInTheDocument();
  98. consoleSpy.mockRestore();
  99. });
  100. });