Enzyme to rtl: QuickThemeSwitcher (#9843)

t3chguy/dedup-icons-17oct
alunturner 2022-12-29 12:44:18 +00:00 committed by GitHub
parent 064a5ad422
commit 6d2d13fdee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 21 additions and 29 deletions

View File

@ -15,16 +15,14 @@ limitations under the License.
*/ */
import React from "react"; import React from "react";
// eslint-disable-next-line deprecate/import import { render, screen, waitFor } from "@testing-library/react";
import { mount } from "enzyme"; import userEvent from "@testing-library/user-event";
import { mocked } from "jest-mock"; import { mocked } from "jest-mock";
import { act } from "react-dom/test-utils";
import QuickThemeSwitcher from "../../../../src/components/views/spaces/QuickThemeSwitcher"; import QuickThemeSwitcher from "../../../../src/components/views/spaces/QuickThemeSwitcher";
import { getOrderedThemes } from "../../../../src/theme"; import { getOrderedThemes } from "../../../../src/theme";
import ThemeChoicePanel from "../../../../src/components/views/settings/ThemeChoicePanel"; import ThemeChoicePanel from "../../../../src/components/views/settings/ThemeChoicePanel";
import SettingsStore from "../../../../src/settings/SettingsStore"; import SettingsStore from "../../../../src/settings/SettingsStore";
import { findById } from "../../../test-utils";
import { SettingLevel } from "../../../../src/settings/SettingLevel"; import { SettingLevel } from "../../../../src/settings/SettingLevel";
import dis from "../../../../src/dispatcher/dispatcher"; import dis from "../../../../src/dispatcher/dispatcher";
import { Action } from "../../../../src/dispatcher/actions"; import { Action } from "../../../../src/dispatcher/actions";
@ -52,7 +50,7 @@ describe("<QuickThemeSwitcher />", () => {
const defaultProps = { const defaultProps = {
requestClose: jest.fn(), requestClose: jest.fn(),
}; };
const getComponent = (props = {}) => mount(<QuickThemeSwitcher {...defaultProps} {...props} />); const renderComponent = (props = {}) => render(<QuickThemeSwitcher {...defaultProps} {...props} />);
beforeEach(() => { beforeEach(() => {
mocked(getOrderedThemes) mocked(getOrderedThemes)
@ -69,27 +67,21 @@ describe("<QuickThemeSwitcher />", () => {
mocked(dis).dispatch.mockClear(); mocked(dis).dispatch.mockClear();
}); });
const getSelectedLabel = (component) => const selectFromDropdown = async (getByTextArg: RegExp | string) => {
findById(component, "mx_QuickSettingsButton_themePickerDropdown_value").text(); const dropdown = screen.getByRole("button", { name: "Space selection" });
await userEvent.click(dropdown);
const openDropdown = (component) => await waitFor(() => {
act(async () => { expect(dropdown).toHaveAttribute("aria-expanded", "true");
component.find(".mx_Dropdown_input").at(0).simulate("click");
component.setProps({});
}); });
const getOption = (component, themeId) => await userEvent.click(screen.getByText(getByTextArg));
findById(component, `mx_QuickSettingsButton_themePickerDropdown__${themeId}`).at(0); return waitFor(() => {
expect(dropdown).toHaveAttribute("aria-expanded", "false");
const selectOption = async (component, themeId: string) => {
await openDropdown(component);
await act(async () => {
getOption(component, themeId).simulate("click");
}); });
}; };
it("renders dropdown correctly when light theme is selected", () => { it("renders dropdown correctly when light theme is selected", () => {
const component = getComponent(); renderComponent();
expect(getSelectedLabel(component)).toEqual("Light"); expect(screen.getByText("Light")).toBeInTheDocument();
}); });
it("renders dropdown correctly when use system theme is truthy", () => { it("renders dropdown correctly when use system theme is truthy", () => {
@ -97,15 +89,15 @@ describe("<QuickThemeSwitcher />", () => {
theme: "light", theme: "light",
useSystemTheme: true, useSystemTheme: true,
}); });
const component = getComponent(); renderComponent();
expect(getSelectedLabel(component)).toEqual("Match system"); expect(screen.getByText("Match system")).toBeInTheDocument();
}); });
it("updates settings when match system is selected", async () => { it("updates settings when match system is selected", async () => {
const requestClose = jest.fn(); 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).toHaveBeenCalledTimes(1);
expect(SettingsStore.setValue).toHaveBeenCalledWith("use_system_theme", null, SettingLevel.DEVICE, true); expect(SettingsStore.setValue).toHaveBeenCalledWith("use_system_theme", null, SettingLevel.DEVICE, true);
@ -117,9 +109,9 @@ describe("<QuickThemeSwitcher />", () => {
it("updates settings when a theme is selected", async () => { it("updates settings when a theme is selected", async () => {
// ie not match system // ie not match system
const requestClose = jest.fn(); 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("use_system_theme", null, SettingLevel.DEVICE, false);
expect(SettingsStore.setValue).toHaveBeenCalledWith("theme", null, SettingLevel.DEVICE, "dark"); expect(SettingsStore.setValue).toHaveBeenCalledWith("theme", null, SettingLevel.DEVICE, "dark");
@ -131,9 +123,9 @@ describe("<QuickThemeSwitcher />", () => {
it("rechecks theme when setting theme fails", async () => { it("rechecks theme when setting theme fails", async () => {
mocked(SettingsStore.setValue).mockRejectedValue("oops"); mocked(SettingsStore.setValue).mockRejectedValue("oops");
const requestClose = jest.fn(); 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(dis.dispatch).toHaveBeenCalledWith({ action: Action.RecheckTheme });
expect(requestClose).toHaveBeenCalled(); expect(requestClose).toHaveBeenCalled();