EndpointValidation.spec.tsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. Copyright (C) 2023 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 DomUtils from "@src/utils/DomUtils";
  16. import { render } from "@testing-library/react";
  17. import EndpointValidation from "./EndpointValidation";
  18. jest.mock("@src/components/ui/StatusComponents/StatusImage", () => ({
  19. __esModule: true,
  20. default: (props: any) => (
  21. <div data-testid="StatusImage">
  22. Status: {props.status || "-"}, Loading: {String(props.loading || false)}
  23. </div>
  24. ),
  25. }));
  26. jest.mock("@src/utils/DomUtils", () => ({
  27. copyTextToClipboard: jest.fn(),
  28. }));
  29. describe("EndpointValidation", () => {
  30. let defaultProps: EndpointValidation["props"];
  31. beforeEach(() => {
  32. defaultProps = {
  33. loading: false,
  34. validation: {
  35. valid: true,
  36. message: "",
  37. },
  38. onCancelClick: jest.fn(),
  39. onRetryClick: jest.fn(),
  40. };
  41. });
  42. it("renders without crashing", () => {
  43. const { getByText, getByTestId } = render(
  44. <EndpointValidation {...defaultProps} />
  45. );
  46. expect(getByText("Endpoint is Valid")).toBeTruthy();
  47. expect(getByTestId("StatusImage").textContent).toBe(
  48. "Status: COMPLETED, Loading: false"
  49. );
  50. });
  51. it("renders loading", () => {
  52. const { getByTestId, getByText } = render(
  53. <EndpointValidation {...defaultProps} loading />
  54. );
  55. expect(getByTestId("StatusImage").textContent).toBe(
  56. "Status: -, Loading: true"
  57. );
  58. expect(getByText("Validating Endpoint")).toBeTruthy();
  59. });
  60. it("renders failed validation", () => {
  61. const { getByTestId, getByText } = render(
  62. <EndpointValidation
  63. {...defaultProps}
  64. validation={{
  65. valid: false,
  66. message: "connection error",
  67. }}
  68. />
  69. );
  70. expect(getByTestId("StatusImage").textContent).toBe(
  71. "Status: ERROR, Loading: false"
  72. );
  73. expect(getByText("connection error")).toBeTruthy();
  74. });
  75. it("renders generic error message", () => {
  76. const { getByTestId, getByText } = render(
  77. <EndpointValidation
  78. {...defaultProps}
  79. validation={{
  80. valid: false,
  81. message: "",
  82. }}
  83. />
  84. );
  85. expect(getByTestId("StatusImage").textContent).toBe(
  86. "Status: ERROR, Loading: false"
  87. );
  88. expect(getByText("An unexpected error occurred.")).toBeTruthy();
  89. });
  90. it("copies the error message to clipboard", () => {
  91. const { getByText } = render(
  92. <EndpointValidation
  93. {...defaultProps}
  94. validation={{
  95. valid: false,
  96. message: "connection error",
  97. }}
  98. />
  99. );
  100. getByText("connection error").click();
  101. expect(DomUtils.copyTextToClipboard).toHaveBeenCalledWith(
  102. "connection error"
  103. );
  104. });
  105. });