DashboardActivity.spec.tsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. Copyright (C) 2021 Cloudbase Solutions SRL
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. import React from "react";
  15. import { render } from "@testing-library/react";
  16. import TestUtils from "@tests/TestUtils";
  17. import { NotificationItemData } from "@src/@types/NotificationItem";
  18. import progressImage from "@src/components/ui/StatusComponents/StatusIcon/images/progress";
  19. import { ThemePalette } from "@src/components/Theme";
  20. import DashboardActivity from ".";
  21. const encodedProgressImage = encodeURIComponent(
  22. progressImage(ThemePalette.grayscale[3], ThemePalette.primary),
  23. );
  24. jest.mock("react-router", () => ({ Link: "a" }));
  25. const ITEMS: NotificationItemData[] = [
  26. {
  27. id: "1",
  28. type: "replica",
  29. status: "ERROR",
  30. name: "Replica 1",
  31. description: "Replica 1 description",
  32. },
  33. {
  34. id: "2",
  35. type: "migration",
  36. status: "RUNNING",
  37. name: "Migration 1",
  38. description: "Migration 1 description",
  39. },
  40. {
  41. id: "3",
  42. type: "migration",
  43. status: "COMPLETED",
  44. name: "Migration 2",
  45. description: "Migration 2 description",
  46. },
  47. ];
  48. describe("DashboardActivity", () => {
  49. it("renders no recent activity", () => {
  50. render(<DashboardActivity notificationItems={[]} />);
  51. expect(
  52. TestUtils.select("DashboardActivity__Message")!.textContent,
  53. ).toContain("There is no recent activity");
  54. });
  55. it("fires new click", () => {
  56. const onNewClick = jest.fn();
  57. render(
  58. <DashboardActivity notificationItems={[]} onNewClick={onNewClick} />,
  59. );
  60. TestUtils.select("Button__StyledButton")!.click();
  61. expect(onNewClick).toHaveBeenCalled();
  62. });
  63. it("renders loading", () => {
  64. const { rerender } = render(
  65. <DashboardActivity notificationItems={[]} loading />,
  66. );
  67. expect(TestUtils.select("DashboardActivity__LoadingWrapper")).toBeTruthy();
  68. rerender(<DashboardActivity notificationItems={[]} />);
  69. expect(TestUtils.select("DashboardActivity__LoadingWrapper")).toBeFalsy();
  70. });
  71. it("renders all items", () => {
  72. render(<DashboardActivity notificationItems={ITEMS} />);
  73. const listItemsEl = TestUtils.selectAll("DashboardActivity__ListItem");
  74. expect(listItemsEl.length).toBe(ITEMS.length);
  75. });
  76. it.each`
  77. idx | href | expectedStatusIcon
  78. ${0} | ${"/deployments/1"} | ${"error-hollow.svg"}
  79. ${1} | ${"/deployments/2"} | ${encodedProgressImage}
  80. ${2} | ${"/deployments/3"} | ${"success-hollow.svg"}
  81. `("renders item with href $href", ({ idx, href, expectedStatusIcon }) => {
  82. render(<DashboardActivity notificationItems={ITEMS} />);
  83. const itemElement = TestUtils.selectAll("DashboardActivity__ListItem")[idx];
  84. expect(itemElement.getAttribute("to")).toBe(href);
  85. const background = window.getComputedStyle(
  86. TestUtils.select("StatusIcon__Wrapper", itemElement)!,
  87. ).background;
  88. expect(background).toContain(expectedStatusIcon);
  89. expect(
  90. TestUtils.select("NotificationDropdown__ItemTransferBadge", itemElement)!
  91. .textContent,
  92. ).toContain(ITEMS[idx].type === "transfer" ? "TR" : "DE");
  93. expect(
  94. TestUtils.select("NotificationDropdown__ItemTitle", itemElement)!
  95. .textContent,
  96. ).toContain(ITEMS[idx].name);
  97. expect(
  98. TestUtils.select("NotificationDropdown__ItemDescription", itemElement)!
  99. .textContent,
  100. ).toContain(ITEMS[idx].description);
  101. });
  102. });