1
0
mirror of https://github.com/matrix-org/matrix-react-sdk.git synced 2025-08-09 08:42:50 +03:00

Apply prettier formatting

This commit is contained in:
Michael Weimann
2022-12-12 12:24:14 +01:00
parent 1cac306093
commit 526645c791
1576 changed files with 65385 additions and 62478 deletions

View File

@@ -14,54 +14,56 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
import React from 'react';
import React from "react";
// eslint-disable-next-line deprecate/import
import { mount } from 'enzyme';
import { mocked } from 'jest-mock';
import { act } from 'react-dom/test-utils';
import { mount } from "enzyme";
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';
import { mockPlatformPeg } from '../../../test-utils/platform';
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";
import { mockPlatformPeg } from "../../../test-utils/platform";
jest.mock('../../../../src/theme');
jest.mock('../../../../src/components/views/settings/ThemeChoicePanel', () => ({
jest.mock("../../../../src/theme");
jest.mock("../../../../src/components/views/settings/ThemeChoicePanel", () => ({
calculateThemeState: jest.fn(),
}));
jest.mock('../../../../src/settings/SettingsStore', () => ({
jest.mock("../../../../src/settings/SettingsStore", () => ({
setValue: jest.fn(),
getValue: jest.fn(),
monitorSetting: jest.fn(),
watchSetting: jest.fn(),
}));
jest.mock('../../../../src/dispatcher/dispatcher', () => ({
jest.mock("../../../../src/dispatcher/dispatcher", () => ({
dispatch: jest.fn(),
register: jest.fn(),
}));
mockPlatformPeg({ overrideBrowserShortcuts: jest.fn().mockReturnValue(false) });
describe('<QuickThemeSwitcher />', () => {
describe("<QuickThemeSwitcher />", () => {
const defaultProps = {
requestClose: jest.fn(),
};
const getComponent = (props = {}) =>
mount(<QuickThemeSwitcher {...defaultProps} {...props} />);
const getComponent = (props = {}) => mount(<QuickThemeSwitcher {...defaultProps} {...props} />);
beforeEach(() => {
mocked(getOrderedThemes).mockClear().mockReturnValue([
{ id: 'light', name: 'Light' },
{ id: 'dark', name: 'Dark' },
]);
mocked(getOrderedThemes)
.mockClear()
.mockReturnValue([
{ id: "light", name: "Light" },
{ id: "dark", name: "Dark" },
]);
mocked(ThemeChoicePanel).calculateThemeState.mockClear().mockReturnValue({
theme: 'light', useSystemTheme: false,
theme: "light",
useSystemTheme: false,
});
mocked(SettingsStore).setValue.mockClear().mockResolvedValue();
mocked(dis).dispatch.mockClear();
@@ -70,66 +72,68 @@ describe('<QuickThemeSwitcher />', () => {
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 openDropdown = (component) =>
act(async () => {
component.find(".mx_Dropdown_input").at(0).simulate("click");
component.setProps({});
});
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');
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();
expect(getSelectedLabel(component)).toEqual('Light');
expect(getSelectedLabel(component)).toEqual("Light");
});
it('renders dropdown correctly when use system theme is truthy', () => {
it("renders dropdown correctly when use system theme is truthy", () => {
mocked(ThemeChoicePanel).calculateThemeState.mockClear().mockReturnValue({
theme: 'light', useSystemTheme: true,
theme: "light",
useSystemTheme: true,
});
const component = getComponent();
expect(getSelectedLabel(component)).toEqual('Match system');
expect(getSelectedLabel(component)).toEqual("Match system");
});
it('updates settings when match system is selected', async () => {
it("updates settings when match system is selected", async () => {
const requestClose = jest.fn();
const component = getComponent({ requestClose });
await selectOption(component, 'MATCH_SYSTEM_THEME_ID');
await selectOption(component, "MATCH_SYSTEM_THEME_ID");
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);
expect(dis.dispatch).not.toHaveBeenCalled();
expect(requestClose).toHaveBeenCalled();
});
it('updates settings when a theme is selected', async () => {
it("updates settings when a theme is selected", async () => {
// ie not match system
const requestClose = jest.fn();
const component = getComponent({ requestClose });
await selectOption(component, 'dark');
await selectOption(component, "dark");
expect(SettingsStore.setValue).toHaveBeenCalledWith('use_system_theme', null, SettingLevel.DEVICE, false);
expect(SettingsStore.setValue).toHaveBeenCalledWith('theme', null, SettingLevel.DEVICE, 'dark');
expect(SettingsStore.setValue).toHaveBeenCalledWith("use_system_theme", null, SettingLevel.DEVICE, false);
expect(SettingsStore.setValue).toHaveBeenCalledWith("theme", null, SettingLevel.DEVICE, "dark");
expect(dis.dispatch).toHaveBeenCalledWith({ action: Action.RecheckTheme, forceTheme: 'dark' });
expect(dis.dispatch).toHaveBeenCalledWith({ action: Action.RecheckTheme, forceTheme: "dark" });
expect(requestClose).toHaveBeenCalled();
});
it('rechecks theme when setting theme fails', async () => {
mocked(SettingsStore.setValue).mockRejectedValue('oops');
it("rechecks theme when setting theme fails", async () => {
mocked(SettingsStore.setValue).mockRejectedValue("oops");
const requestClose = jest.fn();
const component = getComponent({ requestClose });
await selectOption(component, 'MATCH_SYSTEM_THEME_ID');
await selectOption(component, "MATCH_SYSTEM_THEME_ID");
expect(dis.dispatch).toHaveBeenCalledWith({ action: Action.RecheckTheme });
expect(requestClose).toHaveBeenCalled();

View File

@@ -14,64 +14,64 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
import React from 'react';
import React from "react";
import { render, screen, fireEvent } from "@testing-library/react";
import { mocked } from 'jest-mock';
import { MatrixClient } from 'matrix-js-sdk/src/matrix';
import { mocked } from "jest-mock";
import { MatrixClient } from "matrix-js-sdk/src/matrix";
import SpacePanel from '../../../../src/components/views/spaces/SpacePanel';
import { MatrixClientPeg } from '../../../../src/MatrixClientPeg';
import { SpaceKey } from '../../../../src/stores/spaces';
import { shouldShowComponent } from '../../../../src/customisations/helpers/UIComponents';
import { UIComponent } from '../../../../src/settings/UIFeature';
import SpacePanel from "../../../../src/components/views/spaces/SpacePanel";
import { MatrixClientPeg } from "../../../../src/MatrixClientPeg";
import { SpaceKey } from "../../../../src/stores/spaces";
import { shouldShowComponent } from "../../../../src/customisations/helpers/UIComponents";
import { UIComponent } from "../../../../src/settings/UIFeature";
jest.mock('../../../../src/stores/spaces/SpaceStore', () => {
jest.mock("../../../../src/stores/spaces/SpaceStore", () => {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const EventEmitter = require("events");
class MockSpaceStore extends EventEmitter {
invitedSpaces = [];
enabledMetaSpaces = [];
spacePanelSpaces = [];
activeSpace: SpaceKey = '!space1';
activeSpace: SpaceKey = "!space1";
}
return {
instance: new MockSpaceStore(),
};
});
jest.mock('../../../../src/customisations/helpers/UIComponents', () => ({
jest.mock("../../../../src/customisations/helpers/UIComponents", () => ({
shouldShowComponent: jest.fn(),
}));
describe('<SpacePanel />', () => {
describe("<SpacePanel />", () => {
const mockClient = {
getUserId: jest.fn().mockReturnValue('@test:test'),
getUserId: jest.fn().mockReturnValue("@test:test"),
isGuest: jest.fn(),
getAccountData: jest.fn(),
} as unknown as MatrixClient;
beforeAll(() => {
jest.spyOn(MatrixClientPeg, 'get').mockReturnValue(mockClient);
jest.spyOn(MatrixClientPeg, "get").mockReturnValue(mockClient);
});
beforeEach(() => {
mocked(shouldShowComponent).mockClear().mockReturnValue(true);
});
describe('create new space button', () => {
it('renders create space button when UIComponent.CreateSpaces component should be shown', () => {
describe("create new space button", () => {
it("renders create space button when UIComponent.CreateSpaces component should be shown", () => {
render(<SpacePanel />);
screen.getByTestId("create-space-button");
});
it('does not render create space button when UIComponent.CreateSpaces component should not be shown', () => {
it("does not render create space button when UIComponent.CreateSpaces component should not be shown", () => {
mocked(shouldShowComponent).mockReturnValue(false);
render(<SpacePanel />);
expect(shouldShowComponent).toHaveBeenCalledWith(UIComponent.CreateSpaces);
expect(screen.queryByTestId("create-space-button")).toBeFalsy();
});
it('opens context menu on create space button click', () => {
it("opens context menu on create space button click", () => {
render(<SpacePanel />);
fireEvent.click(screen.getByTestId("create-space-button"));
screen.getByTestId("create-space-button");

View File

@@ -15,44 +15,50 @@ limitations under the License.
*/
import React from "react";
import { mocked } from 'jest-mock';
import {
renderIntoDocument,
Simulate,
} from 'react-dom/test-utils';
import { mocked } from "jest-mock";
import { renderIntoDocument, Simulate } from "react-dom/test-utils";
import { act } from "react-dom/test-utils";
import { EventType, MatrixClient, Room } from 'matrix-js-sdk/src/matrix';
import { GuestAccess, HistoryVisibility, JoinRule } from 'matrix-js-sdk/src/@types/partials';
import { EventType, MatrixClient, Room } from "matrix-js-sdk/src/matrix";
import { GuestAccess, HistoryVisibility, JoinRule } from "matrix-js-sdk/src/@types/partials";
import _SpaceSettingsVisibilityTab from "../../../../src/components/views/spaces/SpaceSettingsVisibilityTab";
import { createTestClient, mkEvent, wrapInMatrixClientContext } from '../../../test-utils';
import { mkSpace, mockStateEventImplementation } from '../../../test-utils';
import { MatrixClientPeg } from '../../../../src/MatrixClientPeg';
import { createTestClient, mkEvent, wrapInMatrixClientContext } from "../../../test-utils";
import { mkSpace, mockStateEventImplementation } from "../../../test-utils";
import { MatrixClientPeg } from "../../../../src/MatrixClientPeg";
const SpaceSettingsVisibilityTab = wrapInMatrixClientContext(_SpaceSettingsVisibilityTab);
jest.useFakeTimers();
describe('<SpaceSettingsVisibilityTab />', () => {
describe("<SpaceSettingsVisibilityTab />", () => {
const mockMatrixClient = createTestClient() as MatrixClient;
const makeJoinEvent = (rule: JoinRule = JoinRule.Invite) => mkEvent({
type: EventType.RoomJoinRules, event: true, content: {
join_rule: rule,
},
} as any);
const makeGuestAccessEvent = (rule: GuestAccess = GuestAccess.CanJoin) => mkEvent({
type: EventType.RoomGuestAccess, event: true, content: {
guest_access: rule,
},
} as any);
const makeHistoryEvent = (rule: HistoryVisibility = HistoryVisibility.Shared) => mkEvent({
type: EventType.RoomHistoryVisibility, event: true, content: {
history_visibility: rule,
},
} as any);
const makeJoinEvent = (rule: JoinRule = JoinRule.Invite) =>
mkEvent({
type: EventType.RoomJoinRules,
event: true,
content: {
join_rule: rule,
},
} as any);
const makeGuestAccessEvent = (rule: GuestAccess = GuestAccess.CanJoin) =>
mkEvent({
type: EventType.RoomGuestAccess,
event: true,
content: {
guest_access: rule,
},
} as any);
const makeHistoryEvent = (rule: HistoryVisibility = HistoryVisibility.Shared) =>
mkEvent({
type: EventType.RoomHistoryVisibility,
event: true,
content: {
history_visibility: rule,
},
} as any);
const mockSpaceId = 'mock-space';
const mockSpaceId = "mock-space";
// TODO case for canonical
const makeMockSpace = (
@@ -61,11 +67,7 @@ describe('<SpaceSettingsVisibilityTab />', () => {
guestRule: GuestAccess = GuestAccess.CanJoin,
historyRule: HistoryVisibility = HistoryVisibility.WorldReadable,
): Room => {
const events = [
makeJoinEvent(joinRule),
makeGuestAccessEvent(guestRule),
makeHistoryEvent(historyRule),
];
const events = [makeJoinEvent(joinRule), makeGuestAccessEvent(guestRule), makeHistoryEvent(historyRule)];
const space = mkSpace(client, mockSpaceId);
const getStateEvents = mockStateEventImplementation(events);
mocked(space.currentState).getStateEvents.mockImplementation(getStateEvents);
@@ -92,14 +94,14 @@ describe('<SpaceSettingsVisibilityTab />', () => {
const getByTestId = (container: Element, id: string) => container.querySelector(`[data-test-id=${id}]`);
const toggleGuestAccessSection = async (component) => {
const toggleButton = getByTestId(component, 'toggle-guest-access-btn');
const toggleButton = getByTestId(component, "toggle-guest-access-btn");
await act(async () => {
Simulate.click(toggleButton);
});
};
const getGuestAccessToggle = component => component.querySelector('[aria-label="Enable guest access"');
const getHistoryVisibilityToggle = component => component.querySelector('[aria-label="Preview Space"');
const getErrorMessage = component => getByTestId(component, 'space-settings-error')?.textContent;
const getGuestAccessToggle = (component) => component.querySelector('[aria-label="Enable guest access"');
const getHistoryVisibilityToggle = (component) => component.querySelector('[aria-label="Preview Space"');
const getErrorMessage = (component) => getByTestId(component, "space-settings-error")?.textContent;
beforeEach(() => {
(mockMatrixClient.sendStateEvent as jest.Mock).mockClear().mockResolvedValue({});
@@ -110,29 +112,29 @@ describe('<SpaceSettingsVisibilityTab />', () => {
jest.runAllTimers();
});
it('renders container', () => {
it("renders container", () => {
const component = getComponent();
expect(component).toMatchSnapshot();
});
describe('for a private space', () => {
describe("for a private space", () => {
const joinRule = JoinRule.Invite;
it('does not render addresses section', () => {
it("does not render addresses section", () => {
const space = makeMockSpace(mockMatrixClient, joinRule);
const component = getComponent({ space });
expect(getByTestId(component, 'published-address-fieldset')).toBeFalsy();
expect(getByTestId(component, 'local-address-fieldset')).toBeFalsy();
expect(getByTestId(component, "published-address-fieldset")).toBeFalsy();
expect(getByTestId(component, "local-address-fieldset")).toBeFalsy();
});
});
describe('for a public space', () => {
describe("for a public space", () => {
const joinRule = JoinRule.Public;
const guestRule = GuestAccess.CanJoin;
const historyRule = HistoryVisibility.Joined;
describe('Access', () => {
it('renders guest access section toggle', async () => {
describe("Access", () => {
it("renders guest access section toggle", async () => {
const space = makeMockSpace(mockMatrixClient, joinRule, guestRule);
const component = getComponent({ space });
@@ -141,14 +143,14 @@ describe('<SpaceSettingsVisibilityTab />', () => {
expect(getGuestAccessToggle(component)).toMatchSnapshot();
});
it('send guest access event on toggle', async () => {
it("send guest access event on toggle", async () => {
const space = makeMockSpace(mockMatrixClient, joinRule, guestRule);
const component = getComponent({ space });
await toggleGuestAccessSection(component);
const guestAccessInput = getGuestAccessToggle(component);
expect(guestAccessInput.getAttribute('aria-checked')).toEqual("true");
expect(guestAccessInput.getAttribute("aria-checked")).toEqual("true");
await act(async () => {
Simulate.click(guestAccessInput);
@@ -163,10 +165,10 @@ describe('<SpaceSettingsVisibilityTab />', () => {
);
// toggled off
expect(guestAccessInput.getAttribute('aria-checked')).toEqual("false");
expect(guestAccessInput.getAttribute("aria-checked")).toEqual("false");
});
it('renders error message when update fails', async () => {
it("renders error message when update fails", async () => {
const space = makeMockSpace(mockMatrixClient, joinRule, guestRule);
(mockMatrixClient.sendStateEvent as jest.Mock).mockRejectedValue({});
const component = getComponent({ space });
@@ -178,32 +180,32 @@ describe('<SpaceSettingsVisibilityTab />', () => {
expect(getErrorMessage(component)).toEqual("Failed to update the guest access of this space");
});
it('disables guest access toggle when setting guest access is not allowed', async () => {
it("disables guest access toggle when setting guest access is not allowed", async () => {
const space = makeMockSpace(mockMatrixClient, joinRule, guestRule);
(space.currentState.maySendStateEvent as jest.Mock).mockReturnValue(false);
const component = getComponent({ space });
await toggleGuestAccessSection(component);
expect(getGuestAccessToggle(component).getAttribute('aria-disabled')).toEqual("true");
expect(getGuestAccessToggle(component).getAttribute("aria-disabled")).toEqual("true");
});
});
describe('Preview', () => {
it('renders preview space toggle', () => {
describe("Preview", () => {
it("renders preview space toggle", () => {
const space = makeMockSpace(mockMatrixClient, joinRule, guestRule, historyRule);
const component = getComponent({ space });
// toggle off because space settings is != WorldReadable
expect(getHistoryVisibilityToggle(component).getAttribute('aria-checked')).toEqual("false");
expect(getHistoryVisibilityToggle(component).getAttribute("aria-checked")).toEqual("false");
});
it('updates history visibility on toggle', async () => {
it("updates history visibility on toggle", async () => {
const space = makeMockSpace(mockMatrixClient, joinRule, guestRule, historyRule);
const component = getComponent({ space });
// toggle off because space settings is != WorldReadable
expect(getHistoryVisibilityToggle(component).getAttribute('aria-checked')).toEqual("false");
expect(getHistoryVisibilityToggle(component).getAttribute("aria-checked")).toEqual("false");
await act(async () => {
Simulate.click(getHistoryVisibilityToggle(component));
@@ -216,10 +218,10 @@ describe('<SpaceSettingsVisibilityTab />', () => {
"",
);
expect(getHistoryVisibilityToggle(component).getAttribute('aria-checked')).toEqual("true");
expect(getHistoryVisibilityToggle(component).getAttribute("aria-checked")).toEqual("true");
});
it('renders error message when history update fails', async () => {
it("renders error message when history update fails", async () => {
const space = makeMockSpace(mockMatrixClient, joinRule, guestRule, historyRule);
(mockMatrixClient.sendStateEvent as jest.Mock).mockRejectedValue({});
const component = getComponent({ space });
@@ -231,20 +233,20 @@ describe('<SpaceSettingsVisibilityTab />', () => {
expect(getErrorMessage(component)).toEqual("Failed to update the history visibility of this space");
});
it('disables room preview toggle when history visability changes are not allowed', () => {
it("disables room preview toggle when history visability changes are not allowed", () => {
const space = makeMockSpace(mockMatrixClient, joinRule, guestRule, historyRule);
(space.currentState.maySendStateEvent as jest.Mock).mockReturnValue(false);
const component = getComponent({ space });
expect(getHistoryVisibilityToggle(component).getAttribute('aria-disabled')).toEqual("true");
expect(getHistoryVisibilityToggle(component).getAttribute("aria-disabled")).toEqual("true");
});
});
it('renders addresses section', () => {
it("renders addresses section", () => {
const space = makeMockSpace(mockMatrixClient, joinRule, guestRule);
const component = getComponent({ space });
expect(getByTestId(component, 'published-address-fieldset')).toBeTruthy();
expect(getByTestId(component, 'local-address-fieldset')).toBeTruthy();
expect(getByTestId(component, "published-address-fieldset")).toBeTruthy();
expect(getByTestId(component, "local-address-fieldset")).toBeTruthy();
});
});
});

View File

@@ -48,11 +48,9 @@ describe("SpaceButton", () => {
describe("real space", () => {
it("activates the space on click", () => {
const { container } = render(<SpaceButton
space={space}
selected={false}
label="My space"
data-testid='create-space-button' />);
const { container } = render(
<SpaceButton space={space} selected={false} label="My space" data-testid="create-space-button" />,
);
expect(SpaceStore.instance.setActiveSpace).not.toHaveBeenCalled();
fireEvent.click(getByTestId(container, "create-space-button"));
@@ -60,11 +58,9 @@ describe("SpaceButton", () => {
});
it("navigates to the space home on click if already active", () => {
const { container } = render(<SpaceButton
space={space}
selected={true}
label="My space"
data-testid='create-space-button' />);
const { container } = render(
<SpaceButton space={space} selected={true} label="My space" data-testid="create-space-button" />,
);
expect(dispatchSpy).not.toHaveBeenCalled();
fireEvent.click(getByTestId(container, "create-space-button"));
@@ -74,11 +70,14 @@ describe("SpaceButton", () => {
describe("metaspace", () => {
it("activates the metaspace on click", () => {
const { container } = render(<SpaceButton
spaceKey={MetaSpace.People}
selected={false}
label="People"
data-testid='create-space-button' />);
const { container } = render(
<SpaceButton
spaceKey={MetaSpace.People}
selected={false}
label="People"
data-testid="create-space-button"
/>,
);
expect(SpaceStore.instance.setActiveSpace).not.toHaveBeenCalled();
fireEvent.click(getByTestId(container, "create-space-button"));
@@ -86,11 +85,14 @@ describe("SpaceButton", () => {
});
it("does nothing on click if already active", () => {
const { container } = render(<SpaceButton
spaceKey={MetaSpace.People}
selected={true}
label="People"
data-testid='create-space-button' />);
const { container } = render(
<SpaceButton
spaceKey={MetaSpace.People}
selected={true}
label="People"
data-testid="create-space-button"
/>,
);
fireEvent.click(getByTestId(container, "create-space-button"));
expect(dispatchSpy).not.toHaveBeenCalled();