ReplicaExecutionOptions.spec.tsx 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 { fireEvent, render } from "@testing-library/react";
  16. import TestUtils from "@tests/TestUtils";
  17. import ReplicaExecutionOptions from ".";
  18. jest.mock("@src/plugins/default/ContentPlugin", () => jest.fn(() => null));
  19. describe("ReplicaExecutionOptions", () => {
  20. let defaultProps: ReplicaExecutionOptions["props"];
  21. beforeEach(() => {
  22. defaultProps = {
  23. options: {
  24. shutdown_instances: true,
  25. },
  26. disableExecutionOptions: false,
  27. onChange: jest.fn(),
  28. executionLabel: "Execute",
  29. onCancelClick: jest.fn(),
  30. onExecuteClick: jest.fn(),
  31. };
  32. });
  33. it("renders without crashing", () => {
  34. const { getByText } = render(<ReplicaExecutionOptions {...defaultProps} />);
  35. expect(getByText(defaultProps.executionLabel)).toBeTruthy();
  36. });
  37. it("executes on Enter", () => {
  38. render(<ReplicaExecutionOptions {...defaultProps} />);
  39. fireEvent.keyDown(document.body, { key: "Enter" });
  40. expect(defaultProps.onExecuteClick).toHaveBeenCalled();
  41. });
  42. it("returns original field value if options is null", () => {
  43. render(
  44. <ReplicaExecutionOptions
  45. {...defaultProps}
  46. options={{ shutdown_instances: null }}
  47. />
  48. );
  49. expect(TestUtils.select("Switch__Wrapper")?.textContent).toBe("No");
  50. });
  51. it("handles value change", () => {
  52. render(<ReplicaExecutionOptions {...defaultProps} />);
  53. fireEvent.click(TestUtils.select("Switch__InputWrapper")!);
  54. expect(defaultProps.onChange).toHaveBeenCalledWith(
  55. "shutdown_instances",
  56. false
  57. );
  58. });
  59. it("handles execute click", () => {
  60. const { getByText } = render(<ReplicaExecutionOptions {...defaultProps} />);
  61. fireEvent.click(getByText(defaultProps.executionLabel));
  62. expect(defaultProps.onExecuteClick).toHaveBeenCalled();
  63. });
  64. });