Layout.spec.tsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 {render} from "@testing-library/react";
  15. import "@testing-library/jest-dom";
  16. import mediaQuery from "css-mediaquery";
  17. import { ThemeProvider, createTheme } from "@mui/material/styles";
  18. import Layout from './Layout';
  19. describe("Layout Component", () => {
  20. it("renders", async () => {
  21. const {getByText} = render(<Layout><div>bar</div></Layout>);
  22. const elt = getByText("bar");
  23. expect(elt.parentElement?.tagName).toBe("DIV");
  24. expect(elt.parentElement).toHaveStyle({display: 'grid'})
  25. })
  26. it("displays the right info for string", async () => {
  27. const {getByText} = render(<Layout className="taipy-layout"><div>bar</div></Layout>);
  28. const elt = getByText("bar");
  29. expect(elt.parentElement).toHaveClass("taipy-layout");
  30. })
  31. it("displays the default value", async () => {
  32. const {getByText} = render(<Layout gap="1rem" columns="1 1 1 1" ><div>foo</div></Layout>);
  33. const elt = getByText("foo");
  34. expect(elt.parentElement).toHaveStyle({"grid-template-columns": "1fr 1fr 1fr 1fr"})
  35. })
  36. it("keeps the grid definition", async () => {
  37. const {getByText} = render(<Layout gap="1rem" columns="10px 1em 1rem 1fr" ><div>foo</div></Layout>);
  38. const elt = getByText("foo");
  39. expect(elt.parentElement).toHaveStyle({"grid-template-columns": "10px 1em 1rem 1fr"})
  40. })
  41. it("handles the concise column expression", async () => {
  42. const {getByText} = render(<Layout gap="1rem" columns="1*3px 1 3*1em 2 3 3*2" ><div>foo</div></Layout>);
  43. const elt = getByText("foo");
  44. expect(elt.parentElement).toHaveStyle({"grid-template-columns": "3px 1fr 1em 1em 1em 2fr 3fr 2fr 2fr 2fr"})
  45. })
  46. it("displays the default value for mobile", async () => {
  47. Object.defineProperty(window, "matchMedia", {
  48. writable: true,
  49. value: jest.fn().mockImplementation(query => ({
  50. matches: mediaQuery.match(query, {
  51. width: 10,
  52. }),
  53. media: query,
  54. onchange: null,
  55. addListener: jest.fn(), // Deprecated
  56. removeListener: jest.fn(), // Deprecated
  57. addEventListener: jest.fn(),
  58. removeEventListener: jest.fn(),
  59. dispatchEvent: jest.fn(),
  60. })),});
  61. const {getByText} = render(<ThemeProvider theme={createTheme()}><Layout gap="1rem" columns="1 1 1 1" ><div>foo</div></Layout></ThemeProvider>);
  62. const elt = getByText("foo");
  63. expect(elt.parentElement).toHaveStyle({"grid-template-columns": "1fr"});
  64. })
  65. });