Field.spec.tsx 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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, waitFor } from "@testing-library/react";
  15. import "@testing-library/jest-dom";
  16. import Field from "./Field";
  17. describe("Field Component", () => {
  18. it("renders", async () => {
  19. const { getByText } = render(<Field value="toto" />);
  20. const elt = getByText("toto");
  21. expect(elt.tagName).toBe("SPAN");
  22. });
  23. it("displays the right info for string", async () => {
  24. const { getByText } = render(<Field value="toto" defaultValue="titi" className="taipy-field" />);
  25. const elt = getByText("toto");
  26. expect(elt).toHaveClass("taipy-field");
  27. });
  28. it("displays the default value", async () => {
  29. const { getByText } = render(<Field defaultValue="titi" value={undefined as unknown as string} />);
  30. getByText("titi");
  31. });
  32. it("displays a date with format", async () => {
  33. const myDate = new Date();
  34. myDate.setMonth(1, 1);
  35. const { getByText } = render(
  36. <Field defaultValue="titi" value={myDate.toISOString()} dataType="datetime" format="MM/dd" />
  37. );
  38. getByText("02/01");
  39. });
  40. it("displays a int with format", async () => {
  41. const { getByText } = render(<Field defaultValue="titi" value={12} dataType="int" format="%.2f" />);
  42. getByText("12.00");
  43. });
  44. it("displays a float with format", async () => {
  45. const { getByText } = render(
  46. <Field defaultValue="titi" value={12.1} dataType="float" format="float is %.0f" />
  47. );
  48. getByText("float is 12");
  49. });
  50. it("displays with width=70%", async () => {
  51. const { getByText } = render(<Field value="titi" width="70%" />);
  52. const elt = getByText("titi");
  53. expect(elt).toHaveStyle("width: 70%");
  54. });
  55. it("displays with width=500", async () => {
  56. const { getByText } = render(<Field value="titi" width={500} />);
  57. const elt = getByText("titi");
  58. expect(elt).toHaveStyle("width: 500px");
  59. });
  60. it("can render markdown", async () => {
  61. const { container, getByText, findByText } = render(<Field value="titi" className="taipy-text" mode="md" />);
  62. getByText(/markdown/i);
  63. // https://stackoverflow.com/questions/72382316/jest-encountered-an-unexpected-token-react-markdown
  64. // expect(await findByText(/titi/i)).toBeInTheDocument();
  65. });
  66. it("can render pre", async () => {
  67. const { container } = render(<Field value="titi" className="taipy-text" mode="pre" />);
  68. const elt = container.querySelector("pre.taipy-text-pre");
  69. expect(elt).toBeInTheDocument();
  70. });
  71. describe("latex mode", () => {
  72. it("renders LaTeX as block math", async () => {
  73. const { container, getByText } = render(
  74. <Field value={"$$x = y + 1$$"} className="taipy-text" mode="latex" />
  75. );
  76. getByText(/latex/i);
  77. await waitFor(() => expect(container.querySelector(".taipy-text-latex")).toBeInTheDocument());
  78. });
  79. it("renders LaTeX as inline math", async () => {
  80. const { container, getByText, findByText } = render(
  81. <Field value={"This is inline $x = y + 1$ math."} className="taipy-text" mode="latex" />
  82. );
  83. // getByText(/latex/i); // already loaded ?
  84. await waitFor(() => expect(container.querySelector(".taipy-text-latex")).toBeInTheDocument());
  85. expect(await findByText(/inline/i)).toBeInTheDocument();
  86. });
  87. });
  88. });