MetalHubModal.spec.tsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 = [
  72. "Validation error",
  73. "Validation error 2",
  74. ];
  75. });
  76. const { getByText } = render(
  77. <MetalHubModal {...defaultProps} server={{ ...METALHUB_SERVER_MOCK }} />,
  78. );
  79. expect(getByText("Validation error")).toBeTruthy();
  80. expect(getByText("Validation error 2")).toBeTruthy();
  81. await act(async () => {
  82. metalHubStore.validationError = [];
  83. });
  84. });
  85. it("triggers submit on enter key", async () => {
  86. render(
  87. <MetalHubModal {...defaultProps} server={{ ...METALHUB_SERVER_MOCK }} />,
  88. );
  89. const input = document.querySelector("input")!;
  90. await act(async () => {
  91. fireEvent.keyDown(input, { key: "Enter", code: "Enter" });
  92. });
  93. await waitFor(() => {
  94. expect(metalHubStoreSpies.patchServer).toHaveBeenCalledTimes(1);
  95. });
  96. expect(metalHubStoreSpies.validateServer).toHaveBeenCalledTimes(1);
  97. });
  98. it("highlights invalid fields", async () => {
  99. const { getByText, getAllByText } = render(
  100. <MetalHubModal {...defaultProps} />,
  101. );
  102. await act(async () => {
  103. fireEvent.click(getByText("Validate and save"));
  104. });
  105. expect(getAllByText("Required field").length).toBeGreaterThan(0);
  106. });
  107. it("adds new server", async () => {
  108. const { getByText } = render(<MetalHubModal {...defaultProps} />);
  109. const getInput = (label: string): HTMLInputElement =>
  110. getByText(label).parentElement!.parentElement!.querySelector("input")!;
  111. fireEvent.change(getInput("Host"), {
  112. target: { value: "api.example.com" },
  113. });
  114. fireEvent.change(getInput("Port"), { target: { value: "5566" } });
  115. await act(async () => {
  116. fireEvent.click(getByText("Validate and save"));
  117. });
  118. await act(async () => {
  119. expect(metalHubStoreSpies.addServer).toHaveBeenCalledWith(
  120. "https://api.example.com:5566/api/v1",
  121. );
  122. });
  123. expect(metalHubStoreSpies.validateServer).toHaveBeenCalledWith(
  124. METALHUB_SERVER_MOCK.id,
  125. );
  126. });
  127. });