TaipyStyle.spec.tsx 2.1 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, waitFor } from "@testing-library/react";
  15. import "@testing-library/jest-dom";
  16. import { HelmetProvider } from "react-helmet-async";
  17. import TaipyStyle, { getStyle } from "./TaipyStyle";
  18. import Button from "./Button";
  19. const style = { td: { color: "blue" } };
  20. describe("TaipyStyle Component", () => {
  21. it("renders", async () => {
  22. render(
  23. <HelmetProvider>
  24. <TaipyStyle className="test" content={JSON.stringify(style)} />
  25. </HelmetProvider>
  26. );
  27. await waitFor(() => expect(document.querySelector("style")).toBeInTheDocument());
  28. const elt = document.querySelector("style");
  29. expect(elt).toHaveTextContent(".test{td{color:blue}}")
  30. });
  31. it("get the class", async () => {
  32. const { getByRole } = render(
  33. <HelmetProvider>
  34. <Button label="test">
  35. <TaipyStyle className="test" content={JSON.stringify(style)} />
  36. </Button>
  37. </HelmetProvider>
  38. );
  39. expect(getByRole("button")).toHaveClass("test");
  40. });
  41. it("get the style", () => {
  42. expect(getStyle({cls: {a: "b"}})).toBe("cls{a:b}");
  43. expect(getStyle({cls: {a: "b", subCls: {c: "d"}}})).toBe("cls{a:b;subCls{c:d}}");
  44. expect(getStyle({cls: {a: [1, 2], subCls: {c: "d"}, e: 1, f: undefined}})).toBe("cls{a:1,2;subCls{c:d};e:1;f:undefined}");
  45. expect(getStyle({cls: {a: "b", subCls: {c: "d", ssCls: {e: "f"}}}})).toBe("cls{a:b;subCls{c:d;ssCls{e:f}}}");
  46. });
  47. });