ReplicaMigrationOptions.spec.tsx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 WizardScripts from "@src/components/modules/WizardModule/WizardScripts";
  16. import { fireEvent, render } from "@testing-library/react";
  17. import { INSTANCE_MOCK } from "@tests/mocks/InstancesMock";
  18. import { MINION_POOL_MOCK } from "@tests/mocks/MinionPoolMock";
  19. import { REPLICA_ITEM_DETAILS_MOCK } from "@tests/mocks/TransferMock";
  20. import TestUtils from "@tests/TestUtils";
  21. import ReplicaMigrationOptions from "./";
  22. jest.mock("@src/plugins/default/ContentPlugin", () => jest.fn(() => null));
  23. jest.mock("@src/components/modules/WizardModule/WizardScripts", () => ({
  24. __esModule: true,
  25. default: (props: WizardScripts["props"]) => (
  26. <div data-testid="ScriptsComponent">
  27. <div data-testid="ScriptsUploaded">
  28. {props.uploadedScripts.map(s => s.scriptContent).join(", ")}
  29. </div>
  30. <div
  31. data-testid="ScriptsRemove"
  32. onClick={() => {
  33. props.onScriptDataRemove(props.uploadedScripts[0]);
  34. }}
  35. />
  36. <div data-testid="ScriptsRemoved">
  37. {props.removedScripts.map(s => s.scriptContent).join(", ")}
  38. </div>
  39. <div
  40. data-testid="ScriptsCancel"
  41. onClick={() => {
  42. props.onCancelScript("windows", null);
  43. props.scrollableRef &&
  44. props.scrollableRef(null as any as HTMLElement);
  45. }}
  46. />
  47. <div
  48. data-testid="ScriptsUpload"
  49. onClick={() => {
  50. props.onScriptUpload({
  51. scriptContent: `script-content-${Math.random()}`,
  52. fileName: `script-name.ps1`,
  53. global: "windows",
  54. });
  55. }}
  56. />
  57. </div>
  58. ),
  59. }));
  60. describe("ReplicaMigrationOptions", () => {
  61. let defaultProps: ReplicaMigrationOptions["props"];
  62. beforeEach(() => {
  63. defaultProps = {
  64. instances: [INSTANCE_MOCK],
  65. transferItem: REPLICA_ITEM_DETAILS_MOCK,
  66. minionPools: [
  67. MINION_POOL_MOCK,
  68. { ...MINION_POOL_MOCK, id: "pool2", name: "Pool2" },
  69. ],
  70. loadingInstances: false,
  71. onCancelClick: jest.fn(),
  72. onMigrateClick: jest.fn(),
  73. onResizeUpdate: jest.fn(),
  74. };
  75. });
  76. it("renders without crashing", () => {
  77. const { getByText } = render(<ReplicaMigrationOptions {...defaultProps} />);
  78. expect(getByText("Migrate")).toBeTruthy();
  79. });
  80. it("executes on Enter", () => {
  81. render(<ReplicaMigrationOptions {...defaultProps} />);
  82. fireEvent.keyDown(document.body, { key: "Enter" });
  83. expect(defaultProps.onMigrateClick).toHaveBeenCalled();
  84. });
  85. it("calls onResizeUpdate on selectedBarButton state change", () => {
  86. render(<ReplicaMigrationOptions {...defaultProps} />);
  87. fireEvent.click(TestUtils.selectAll("ToggleButtonBar__Item-")[1]);
  88. expect(defaultProps.onResizeUpdate).toHaveBeenCalled();
  89. });
  90. it("handles value change", () => {
  91. render(<ReplicaMigrationOptions {...defaultProps} />);
  92. expect(TestUtils.select("Switch__Wrapper")?.textContent).toBe("Yes");
  93. fireEvent.click(TestUtils.select("Switch__InputWrapper")!);
  94. expect(TestUtils.select("Switch__Wrapper")?.textContent).toBe("No");
  95. });
  96. it("handles script operations", () => {
  97. const { getByTestId } = render(
  98. <ReplicaMigrationOptions {...defaultProps} />
  99. );
  100. fireEvent.click(TestUtils.selectAll("ToggleButtonBar__Item-")[1]);
  101. fireEvent.click(getByTestId("ScriptsUpload"));
  102. expect(getByTestId("ScriptsUploaded").textContent).toContain(
  103. "script-content"
  104. );
  105. fireEvent.click(getByTestId("ScriptsCancel"));
  106. expect(getByTestId("ScriptsUploaded").textContent).toBe("");
  107. fireEvent.click(getByTestId("ScriptsUpload"));
  108. expect(getByTestId("ScriptsUploaded").textContent).toContain(
  109. "script-content"
  110. );
  111. expect(getByTestId("ScriptsRemoved").textContent).toBe("");
  112. fireEvent.click(getByTestId("ScriptsRemove"));
  113. expect(getByTestId("ScriptsRemoved").textContent).toContain(
  114. "script-content"
  115. );
  116. });
  117. it("doesn't render minion pool mappings", () => {
  118. const { rerender } = render(<ReplicaMigrationOptions {...defaultProps} />);
  119. expect(document.body.textContent).toContain("Minion Pool Mappings");
  120. rerender(<ReplicaMigrationOptions {...defaultProps} minionPools={[]} />);
  121. expect(document.body.textContent).not.toContain("Minion Pool Mappings");
  122. });
  123. it("changes minion pool mappings value", () => {
  124. render(<ReplicaMigrationOptions {...defaultProps} />);
  125. fireEvent.click(TestUtils.select("DropdownButton__Wrapper-")!);
  126. const dropdownItem = TestUtils.selectAll("Dropdown__ListItem-")[2];
  127. expect(dropdownItem.textContent).toBe("Pool2");
  128. fireEvent.click(dropdownItem);
  129. expect(TestUtils.select("DropdownButton__Label-")?.textContent).toBe(
  130. "Pool2"
  131. );
  132. });
  133. it("handles migrate click", () => {
  134. const { getByText } = render(<ReplicaMigrationOptions {...defaultProps} />);
  135. fireEvent.click(getByText("Migrate"));
  136. expect(defaultProps.onMigrateClick).toHaveBeenCalled();
  137. });
  138. });