MetalHubModal.spec.tsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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, { act } from "react";
  15. import metalHubStore from "@src/stores/MetalHubStore";
  16. import { fireEvent, render, waitFor } from "@testing-library/react";
  17. import { METALHUB_SERVER_MOCK } from "@tests/mocks/MetalHubServerMock";
  18. import MetalHubModal from "./MetalHubModal";
  19. describe("MetalHubModal", () => {
  20. let defaultProps: MetalHubModal["props"];
  21. let metalHubStoreSpies: {
  22. clearValidationError: jest.SpyInstance;
  23. patchServer: jest.SpyInstance;
  24. validateServer: jest.SpyInstance;
  25. addServer: jest.SpyInstance;
  26. };
  27. beforeEach(() => {
  28. metalHubStoreSpies = {
  29. clearValidationError: jest.spyOn(metalHubStore, "clearValidationError"),
  30. patchServer: jest.spyOn(metalHubStore, "patchServer").mockResolvedValue(),
  31. validateServer: jest
  32. .spyOn(metalHubStore, "validateServer")
  33. .mockResolvedValue(true),
  34. addServer: jest.spyOn(metalHubStore, "addServer").mockResolvedValue({
  35. ...METALHUB_SERVER_MOCK,
  36. }),
  37. };
  38. defaultProps = {
  39. onRequestClose: jest.fn(),
  40. onUpdateDone: jest.fn(),
  41. };
  42. });
  43. it("renders without crashing and clears validation error on unmount", () => {
  44. const { getByText, unmount } = render(<MetalHubModal {...defaultProps} />);
  45. expect(getByText("Add Coriolis Bare Metal Server")).toBeTruthy();
  46. unmount();
  47. expect(metalHubStoreSpies.clearValidationError).toHaveBeenCalledTimes(1);
  48. });
  49. it("shows the server for editing", () => {
  50. const { getByText } = render(
  51. <MetalHubModal {...defaultProps} server={{ ...METALHUB_SERVER_MOCK }} />
  52. );
  53. expect(getByText("Update Coriolis Bare Metal Server")).toBeTruthy();
  54. const testInput = (label: string, value: string) => {
  55. const input =
  56. getByText(label).parentElement!.parentElement!.querySelector("input");
  57. expect(input).toBeTruthy();
  58. expect(input!.value).toBe(value);
  59. };
  60. testInput(
  61. "Host",
  62. METALHUB_SERVER_MOCK.api_endpoint!.split(":")[1].replace("//", "")
  63. );
  64. testInput(
  65. "Port",
  66. METALHUB_SERVER_MOCK.api_endpoint!.split(":")[2].replace(/\/.*/, "")
  67. );
  68. });
  69. it("renders validation error", async () => {
  70. await act(async () => {
  71. metalHubStore.validationError = ["Validation error", "Validation error 2"];
  72. });
  73. const { getByText } = render(
  74. <MetalHubModal {...defaultProps} server={{ ...METALHUB_SERVER_MOCK }} />
  75. );
  76. expect(getByText("Validation error")).toBeTruthy();
  77. expect(getByText("Validation error 2")).toBeTruthy();
  78. await act(async () => {
  79. metalHubStore.validationError = [];
  80. });
  81. });
  82. it("triggers submit on enter key", async () => {
  83. render(
  84. <MetalHubModal {...defaultProps} server={{ ...METALHUB_SERVER_MOCK }} />
  85. );
  86. const input = document.querySelector("input")!;
  87. await act(async () => {
  88. fireEvent.keyDown(input, { key: "Enter", code: "Enter" });
  89. });
  90. await waitFor(() => {
  91. expect(metalHubStoreSpies.patchServer).toHaveBeenCalledTimes(1);
  92. });
  93. expect(metalHubStoreSpies.validateServer).toHaveBeenCalledTimes(1);
  94. });
  95. it("highlights invalid fields", async () => {
  96. const { getByText, getAllByText } = render(
  97. <MetalHubModal {...defaultProps} />
  98. );
  99. await act(async () => {
  100. fireEvent.click(getByText("Validate and save"));
  101. });
  102. expect(getAllByText("Required field").length).toBeGreaterThan(0);
  103. });
  104. it("adds new server", async () => {
  105. const { getByText } = render(<MetalHubModal {...defaultProps} />);
  106. const getInput = (label: string): HTMLInputElement =>
  107. getByText(label).parentElement!.parentElement!.querySelector("input")!;
  108. fireEvent.change(getInput("Host"), {
  109. target: { value: "api.example.com" },
  110. });
  111. fireEvent.change(getInput("Port"), { target: { value: "5566" } });
  112. await act(async () => {
  113. fireEvent.click(getByText("Validate and save"));
  114. });
  115. await act(async () => {
  116. expect(metalHubStoreSpies.addServer).toHaveBeenCalledWith(
  117. "https://api.example.com:5566/api/v1"
  118. );
  119. });
  120. expect(metalHubStoreSpies.validateServer).toHaveBeenCalledWith(
  121. METALHUB_SERVER_MOCK.id
  122. );
  123. });
  124. });