FileSelector.spec.tsx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Copyright 2023 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 { fireEvent, render, waitFor } from "@testing-library/react";
  15. import "@testing-library/jest-dom";
  16. import userEvent from "@testing-library/user-event";
  17. import FileSelector from "./FileSelector";
  18. import { TaipyContext } from "../../context/taipyContext";
  19. import { TaipyState, INITIAL_STATE } from "../../context/taipyReducers";
  20. describe("FileSelector Component", () => {
  21. it("renders", async () => {
  22. const { getByText } = render(<FileSelector label="toto" />);
  23. const elt = getByText("toto");
  24. expect(elt.tagName).toBe("SPAN");
  25. });
  26. it("displays the right info for string", async () => {
  27. const { getByText } = render(<FileSelector label="toto" defaultLabel="titi" className="taipy-file-selector" />);
  28. const elt = getByText("toto");
  29. expect(elt.parentElement).toHaveClass("taipy-file-selector");
  30. });
  31. it("displays the default value", async () => {
  32. const { getByText } = render(<FileSelector defaultLabel="titi" label={undefined as unknown as string} />);
  33. getByText("titi");
  34. });
  35. it("is disabled", async () => {
  36. const { getByText } = render(<FileSelector label="val" active={false} />);
  37. const elt = getByText("val");
  38. expect(elt).toHaveClass("Mui-disabled");
  39. });
  40. it("is enabled by default", async () => {
  41. const { getByText } = render(<FileSelector label="val" />);
  42. const elt = getByText("val");
  43. expect(elt).not.toHaveClass("Mui-disabled");
  44. });
  45. it("is enabled by active", async () => {
  46. const { getByText } = render(<FileSelector label="val" active={true} />);
  47. const elt = getByText("val");
  48. expect(elt).not.toHaveClass("Mui-disabled");
  49. });
  50. //looks like userEvent upload does not fire onchange
  51. xit("dispatch a well formed message on file selection", async () => {
  52. const file = new File(["(⌐□_□)"], "chucknorris.png", { type: "image/png" });
  53. const dispatch = jest.fn();
  54. const state: TaipyState = INITIAL_STATE;
  55. const { getByText } = render(
  56. <TaipyContext.Provider value={{ state, dispatch }}>
  57. <FileSelector label="FileSelector" onAction="on_action" />
  58. </TaipyContext.Provider>
  59. );
  60. const elt = getByText("FileSelector");
  61. const inputElt = elt.parentElement?.querySelector("input");
  62. expect(inputElt).toBeInTheDocument();
  63. inputElt && await userEvent.upload(inputElt, file);
  64. expect(dispatch).toHaveBeenCalledWith({
  65. name: "",
  66. payload: { args: [], action: "on_action" },
  67. type: "SEND_ACTION_ACTION",
  68. })
  69. });
  70. it("dispatch a specific text on file drop", async () => {
  71. const file = new File(["(⌐□_□)"], "chucknorris2.png", { type: "image/png" });
  72. const { getByRole, getByText } = render(<FileSelector label="FileSelectorDrop" />);
  73. const elt = getByRole("button");
  74. const inputElt = elt.parentElement?.querySelector("input");
  75. expect(inputElt).toBeInTheDocument();
  76. waitFor(() => getByText("Drop here to Upload"));
  77. inputElt &&
  78. fireEvent.drop(inputElt, {
  79. dataTransfer: {
  80. files: [file],
  81. },
  82. });
  83. });
  84. it("displays a dropped custom message", async () => {
  85. const file = new File(["(⌐□_□)"], "chucknorris2.png", { type: "image/png" });
  86. const { getByRole, getByText } = render(<FileSelector label="FileSelectorDrop" dropMessage="drop here those files" />);
  87. const elt = getByRole("button");
  88. const inputElt = elt.parentElement?.querySelector("input");
  89. expect(inputElt).toBeInTheDocument();
  90. waitFor(() => getByText("drop here those files"));
  91. inputElt &&
  92. fireEvent.drop(inputElt, {
  93. dataTransfer: {
  94. files: [file],
  95. },
  96. });
  97. });
  98. });