ThemeToggle.spec.tsx 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 ThemeToggle from './ThemeToggle';
  18. import { INITIAL_STATE, TaipyState } from "../../context/taipyReducers";
  19. import { TaipyContext } from "../../context/taipyContext";
  20. let state: TaipyState = INITIAL_STATE;
  21. const dispatch = jest.fn();
  22. beforeEach(() => {
  23. state = INITIAL_STATE;
  24. state.theme.palette.mode = "light";
  25. dispatch.mockClear();
  26. });
  27. describe("ThemeToggle Component", () => {
  28. it("renders", async () => {
  29. const { getByText, getByTestId, getByTitle } = render(<TaipyContext.Provider value={{ state, dispatch }}>
  30. <ThemeToggle />
  31. </TaipyContext.Provider>);
  32. expect(getByTestId("Brightness3Icon")).toBeInTheDocument();
  33. expect(getByTestId("WbSunnyIcon")).toBeInTheDocument();
  34. expect(getByTitle("Light")).toBeInTheDocument();
  35. expect(getByTitle("Dark")).toBeInTheDocument();
  36. const label = getByText("Mode");
  37. expect(label.tagName).toBe("P");
  38. })
  39. it("uses the class", async () => {
  40. const {getByText} = render(<TaipyContext.Provider value={{ state, dispatch }}>
  41. <ThemeToggle className="taipy-toggle" />
  42. </TaipyContext.Provider>);
  43. const elt = getByText("Mode");
  44. expect(elt.parentElement).toHaveClass("taipy-toggle");
  45. })
  46. it("shows Light theme selected at start", async () => {
  47. const {getByTitle} = render(<TaipyContext.Provider value={{ state, dispatch }}>
  48. <ThemeToggle />
  49. </TaipyContext.Provider>);
  50. expect(getByTitle("Dark")).not.toHaveClass("Mui-selected");
  51. expect(getByTitle("Light")).toHaveClass("Mui-selected");
  52. });
  53. it("shows Dark theme selected at start", async () => {
  54. state.theme.palette.mode = "dark";
  55. const {getByTitle} = render(<TaipyContext.Provider value={{ state, dispatch }}>
  56. <ThemeToggle />
  57. </TaipyContext.Provider>);
  58. expect(getByTitle("Dark")).toHaveClass("Mui-selected");
  59. expect(getByTitle("Light")).not.toHaveClass("Mui-selected");
  60. });
  61. it("is disabled", async () => {
  62. const { getAllByRole } = render(<TaipyContext.Provider value={{ state, dispatch }}>
  63. <ThemeToggle active={false} />
  64. </TaipyContext.Provider>);
  65. const elts = getAllByRole("button");
  66. elts.forEach(elt => expect(elt).toBeDisabled());
  67. });
  68. it("is enabled by default", async () => {
  69. const { getAllByRole } = render(<TaipyContext.Provider value={{ state, dispatch }}>
  70. <ThemeToggle />
  71. </TaipyContext.Provider>);
  72. const elts = getAllByRole("button");
  73. elts.forEach(elt => expect(elt).not.toBeDisabled());
  74. });
  75. it("is enabled by active", async () => {
  76. const { getAllByRole } = render(<TaipyContext.Provider value={{ state, dispatch }}>
  77. <ThemeToggle active={true}/>
  78. </TaipyContext.Provider>);
  79. const elts = getAllByRole("button");
  80. elts.forEach(elt => expect(elt).not.toBeDisabled());
  81. });
  82. it("dispatch a well formed message", async () => {
  83. const { getByTitle } = render(<TaipyContext.Provider value={{ state, dispatch }}>
  84. <ThemeToggle />
  85. </TaipyContext.Provider>);
  86. const elt = getByTitle("Dark");
  87. await userEvent.click(elt);
  88. expect(dispatch).toHaveBeenCalledWith({name: "theme", payload: {value: "dark", "fromBackend": false}, "type": "SET_THEME"});
  89. });
  90. });