UIBlocker.spec.tsx 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /*
  2. * Copyright 2021-2024 Avaiga Private Limited
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  5. * the License. You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  10. * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
  11. * specific language governing permissions and limitations under the License.
  12. */
  13. import React from "react";
  14. import { render } from "@testing-library/react";
  15. import "@testing-library/jest-dom";
  16. import userEvent from "@testing-library/user-event";
  17. import UIBlocker from "./UIBlocker";
  18. import { TaipyContext } from "../../context/taipyContext";
  19. import { TaipyState, INITIAL_STATE, BlockMessage } from "../../context/taipyReducers";
  20. const blockWithCancel: BlockMessage = {message: "message", close: false, noCancel: false, action:"action"}
  21. describe("UIBlocker Component", () => {
  22. it("renders", async () => {
  23. const { getByText } = render(<UIBlocker block={blockWithCancel}/>);
  24. const elt = getByText("Cancel");
  25. expect(elt.tagName).toBe("BUTTON");
  26. });
  27. it("displays the right className", async () => {
  28. const { getByText } = render(<UIBlocker block={blockWithCancel} />);
  29. const elt = getByText("Cancel");
  30. expect(elt.parentElement).toHaveClass("taipy-UIBlocker");
  31. });
  32. it("doesn't display by default", async () => {
  33. const { queryAllByAltText } = render(
  34. <UIBlocker />
  35. );
  36. expect(queryAllByAltText("Cancel")).toHaveLength(0);
  37. });
  38. it("dispatch a well formed message", async () => {
  39. const dispatch = jest.fn();
  40. const state: TaipyState = INITIAL_STATE;
  41. const { getByText } = render(<TaipyContext.Provider value={{ state, dispatch }}>
  42. <UIBlocker block={blockWithCancel} />
  43. </TaipyContext.Provider>);
  44. const elt = getByText("Cancel");
  45. await userEvent.click(elt);
  46. expect(dispatch).toHaveBeenCalledWith({"name": "UIBlocker", "payload": {"action": "action", "args": []}, "type": "SEND_ACTION_ACTION"});
  47. });
  48. });