From 6d2d13fdee8a6068419acdfd3c0ba33e71effc0a Mon Sep 17 00:00:00 2001 From: alunturner <56027671+alunturner@users.noreply.github.com> Date: Thu, 29 Dec 2022 12:44:18 +0000 Subject: [PATCH] Enzyme to rtl: QuickThemeSwitcher (#9843) --- .../views/spaces/QuickThemeSwitcher-test.tsx | 50 ++++++++----------- 1 file changed, 21 insertions(+), 29 deletions(-) diff --git a/test/components/views/spaces/QuickThemeSwitcher-test.tsx b/test/components/views/spaces/QuickThemeSwitcher-test.tsx index ecd80a32c1..87b393ec9f 100644 --- a/test/components/views/spaces/QuickThemeSwitcher-test.tsx +++ b/test/components/views/spaces/QuickThemeSwitcher-test.tsx @@ -15,16 +15,14 @@ limitations under the License. */ import React from "react"; -// eslint-disable-next-line deprecate/import -import { mount } from "enzyme"; +import { render, screen, waitFor } from "@testing-library/react"; +import userEvent from "@testing-library/user-event"; import { mocked } from "jest-mock"; -import { act } from "react-dom/test-utils"; import QuickThemeSwitcher from "../../../../src/components/views/spaces/QuickThemeSwitcher"; import { getOrderedThemes } from "../../../../src/theme"; import ThemeChoicePanel from "../../../../src/components/views/settings/ThemeChoicePanel"; import SettingsStore from "../../../../src/settings/SettingsStore"; -import { findById } from "../../../test-utils"; import { SettingLevel } from "../../../../src/settings/SettingLevel"; import dis from "../../../../src/dispatcher/dispatcher"; import { Action } from "../../../../src/dispatcher/actions"; @@ -52,7 +50,7 @@ describe("", () => { const defaultProps = { requestClose: jest.fn(), }; - const getComponent = (props = {}) => mount(); + const renderComponent = (props = {}) => render(); beforeEach(() => { mocked(getOrderedThemes) @@ -69,27 +67,21 @@ describe("", () => { mocked(dis).dispatch.mockClear(); }); - const getSelectedLabel = (component) => - findById(component, "mx_QuickSettingsButton_themePickerDropdown_value").text(); - - const openDropdown = (component) => - act(async () => { - component.find(".mx_Dropdown_input").at(0).simulate("click"); - component.setProps({}); + const selectFromDropdown = async (getByTextArg: RegExp | string) => { + const dropdown = screen.getByRole("button", { name: "Space selection" }); + await userEvent.click(dropdown); + await waitFor(() => { + expect(dropdown).toHaveAttribute("aria-expanded", "true"); }); - const getOption = (component, themeId) => - findById(component, `mx_QuickSettingsButton_themePickerDropdown__${themeId}`).at(0); - - const selectOption = async (component, themeId: string) => { - await openDropdown(component); - await act(async () => { - getOption(component, themeId).simulate("click"); + await userEvent.click(screen.getByText(getByTextArg)); + return waitFor(() => { + expect(dropdown).toHaveAttribute("aria-expanded", "false"); }); }; it("renders dropdown correctly when light theme is selected", () => { - const component = getComponent(); - expect(getSelectedLabel(component)).toEqual("Light"); + renderComponent(); + expect(screen.getByText("Light")).toBeInTheDocument(); }); it("renders dropdown correctly when use system theme is truthy", () => { @@ -97,15 +89,15 @@ describe("", () => { theme: "light", useSystemTheme: true, }); - const component = getComponent(); - expect(getSelectedLabel(component)).toEqual("Match system"); + renderComponent(); + expect(screen.getByText("Match system")).toBeInTheDocument(); }); it("updates settings when match system is selected", async () => { const requestClose = jest.fn(); - const component = getComponent({ requestClose }); + renderComponent({ requestClose }); - await selectOption(component, "MATCH_SYSTEM_THEME_ID"); + await selectFromDropdown(/match system/i); expect(SettingsStore.setValue).toHaveBeenCalledTimes(1); expect(SettingsStore.setValue).toHaveBeenCalledWith("use_system_theme", null, SettingLevel.DEVICE, true); @@ -117,9 +109,9 @@ describe("", () => { it("updates settings when a theme is selected", async () => { // ie not match system const requestClose = jest.fn(); - const component = getComponent({ requestClose }); + renderComponent({ requestClose }); - await selectOption(component, "dark"); + await selectFromDropdown(/dark/i); expect(SettingsStore.setValue).toHaveBeenCalledWith("use_system_theme", null, SettingLevel.DEVICE, false); expect(SettingsStore.setValue).toHaveBeenCalledWith("theme", null, SettingLevel.DEVICE, "dark"); @@ -131,9 +123,9 @@ describe("", () => { it("rechecks theme when setting theme fails", async () => { mocked(SettingsStore.setValue).mockRejectedValue("oops"); const requestClose = jest.fn(); - const component = getComponent({ requestClose }); + renderComponent({ requestClose }); - await selectOption(component, "MATCH_SYSTEM_THEME_ID"); + await selectFromDropdown(/match system/i); expect(dis.dispatch).toHaveBeenCalledWith({ action: Action.RecheckTheme }); expect(requestClose).toHaveBeenCalled();