Ensure we include the client on rendered messages.

This commit is contained in:
Will Hunt
2025-04-15 11:43:28 +01:00
parent a0b6613e01
commit f4152334fc
4 changed files with 24 additions and 7 deletions

View File

@@ -8,13 +8,15 @@ Please see LICENSE files in the repository root for full details.
import React from "react"; import React from "react";
import { fireEvent, render, screen } from "jest-matrix-react"; import { fireEvent, render, screen } from "jest-matrix-react";
import { MatrixEvent } from "matrix-js-sdk/src/matrix"; import { MatrixEvent, type MatrixClient } from "matrix-js-sdk/src/matrix";
import { HideActionButton } from "../../../../../src/components/views/messages/HideActionButton"; import { HideActionButton } from "../../../../../src/components/views/messages/HideActionButton";
import SettingsStore from "../../../../../src/settings/SettingsStore"; import SettingsStore from "../../../../../src/settings/SettingsStore";
import { SettingLevel } from "../../../../../src/settings/SettingLevel"; import { SettingLevel } from "../../../../../src/settings/SettingLevel";
import type { Settings } from "../../../../../src/settings/Settings"; import type { Settings } from "../../../../../src/settings/Settings";
import { MediaPreviewValue } from "../../../../../src/@types/media_preview"; import { MediaPreviewValue } from "../../../../../src/@types/media_preview";
import { getMockClientWithEventEmitter, withClientContextRenderOptions } from "../../../../test-utils";
import type { MockedObject } from "jest-mock";
function mockSetting(mediaPreviews: MediaPreviewValue, showMediaEventIds: Settings["showMediaEventIds"]["default"]) { function mockSetting(mediaPreviews: MediaPreviewValue, showMediaEventIds: Settings["showMediaEventIds"]["default"]) {
jest.spyOn(SettingsStore, "getValue").mockImplementation((settingName) => { jest.spyOn(SettingsStore, "getValue").mockImplementation((settingName) => {
@@ -42,32 +44,38 @@ const event = new MatrixEvent({
}); });
describe("HideActionButton", () => { describe("HideActionButton", () => {
let cli: MockedObject<MatrixClient>;
beforeEach(() => {
cli = getMockClientWithEventEmitter({
getRoom: jest.fn(),
});
});
afterEach(() => { afterEach(() => {
jest.restoreAllMocks(); jest.restoreAllMocks();
}); });
it("should show button when event is visible by showMediaEventIds setting", async () => { it("should show button when event is visible by showMediaEventIds setting", async () => {
mockSetting(MediaPreviewValue.Off, { [EVENT_ID]: true }); mockSetting(MediaPreviewValue.Off, { [EVENT_ID]: true });
render(<HideActionButton mxEvent={event} />); render(<HideActionButton mxEvent={event} />, withClientContextRenderOptions(cli));
expect(screen.getByRole("button")).toBeVisible(); expect(screen.getByRole("button")).toBeVisible();
}); });
it("should show button when event is visible by mediaPreviewConfig setting", async () => { it("should show button when event is visible by mediaPreviewConfig setting", async () => {
mockSetting(MediaPreviewValue.On, {}); mockSetting(MediaPreviewValue.On, {});
render(<HideActionButton mxEvent={event} />); render(<HideActionButton mxEvent={event} />, withClientContextRenderOptions(cli));
expect(screen.getByRole("button")).toBeVisible(); expect(screen.getByRole("button")).toBeVisible();
}); });
it("should hide button when event is hidden by showMediaEventIds setting", async () => { it("should hide button when event is hidden by showMediaEventIds setting", async () => {
mockSetting(MediaPreviewValue.Off, { [EVENT_ID]: false }); mockSetting(MediaPreviewValue.Off, { [EVENT_ID]: false });
render(<HideActionButton mxEvent={event} />); render(<HideActionButton mxEvent={event} />, withClientContextRenderOptions(cli));
expect(screen.queryByRole("button")).toBeNull(); expect(screen.queryByRole("button")).toBeNull();
}); });
it("should hide button when event is hidden by showImages setting", async () => { it("should hide button when event is hidden by showImages setting", async () => {
mockSetting(MediaPreviewValue.Off, {}); mockSetting(MediaPreviewValue.Off, {});
render(<HideActionButton mxEvent={event} />); render(<HideActionButton mxEvent={event} />, withClientContextRenderOptions(cli));
expect(screen.queryByRole("button")).toBeNull(); expect(screen.queryByRole("button")).toBeNull();
}); });
it("should store event as hidden when clicked", async () => { it("should store event as hidden when clicked", async () => {
const spy = jest.spyOn(SettingsStore, "setValue"); const spy = jest.spyOn(SettingsStore, "setValue");
render(<HideActionButton mxEvent={event} />); render(<HideActionButton mxEvent={event} />, withClientContextRenderOptions(cli));
fireEvent.click(screen.getByRole("button")); fireEvent.click(screen.getByRole("button"));
expect(spy).toHaveBeenCalledWith("showMediaEventIds", null, SettingLevel.DEVICE, { "$foo:bar": false }); expect(spy).toHaveBeenCalledWith("showMediaEventIds", null, SettingLevel.DEVICE, { "$foo:bar": false });
// Button should be hidden after the setting is set. // Button should be hidden after the setting is set.

View File

@@ -100,6 +100,7 @@ describe("<MImageBody/>", () => {
mxEvent={encryptedMediaEvent} mxEvent={encryptedMediaEvent}
mediaEventHelper={new MediaEventHelper(encryptedMediaEvent)} mediaEventHelper={new MediaEventHelper(encryptedMediaEvent)}
/>, />,
withClientContextRenderOptions(cli),
); );
// thumbnail with dimensions present // thumbnail with dimensions present
@@ -115,6 +116,7 @@ describe("<MImageBody/>", () => {
mxEvent={encryptedMediaEvent} mxEvent={encryptedMediaEvent}
mediaEventHelper={new MediaEventHelper(encryptedMediaEvent)} mediaEventHelper={new MediaEventHelper(encryptedMediaEvent)}
/>, />,
withClientContextRenderOptions(cli),
); );
expect(fetchMock).toHaveBeenCalledWith(url); expect(fetchMock).toHaveBeenCalledWith(url);
@@ -132,6 +134,7 @@ describe("<MImageBody/>", () => {
mxEvent={encryptedMediaEvent} mxEvent={encryptedMediaEvent}
mediaEventHelper={new MediaEventHelper(encryptedMediaEvent)} mediaEventHelper={new MediaEventHelper(encryptedMediaEvent)}
/>, />,
withClientContextRenderOptions(cli),
); );
await screen.findByText("Error decrypting image"); await screen.findByText("Error decrypting image");
@@ -212,6 +215,7 @@ describe("<MImageBody/>", () => {
const { container } = render( const { container } = render(
<MImageBody {...props} mxEvent={event} mediaEventHelper={new MediaEventHelper(event)} />, <MImageBody {...props} mxEvent={event} mediaEventHelper={new MediaEventHelper(event)} />,
withClientContextRenderOptions(cli),
); );
const img = container.querySelector(".mx_MImageBody_thumbnail")!; const img = container.querySelector(".mx_MImageBody_thumbnail")!;
@@ -265,6 +269,7 @@ describe("<MImageBody/>", () => {
const { container } = render( const { container } = render(
<MImageBody {...props} mxEvent={event} mediaEventHelper={new MediaEventHelper(event)} />, <MImageBody {...props} mxEvent={event} mediaEventHelper={new MediaEventHelper(event)} />,
withClientContextRenderOptions(cli),
); );
// Wait for spinners to go away // Wait for spinners to go away
@@ -290,6 +295,7 @@ describe("<MImageBody/>", () => {
const { container } = render( const { container } = render(
<MImageBody {...props} mxEvent={event} mediaEventHelper={new MediaEventHelper(event)} />, <MImageBody {...props} mxEvent={event} mediaEventHelper={new MediaEventHelper(event)} />,
withClientContextRenderOptions(cli),
); );
const img = container.querySelector(".mx_MImageBody_thumbnail")!; const img = container.querySelector(".mx_MImageBody_thumbnail")!;

View File

@@ -19,6 +19,7 @@ import {
mockClientMethodsDevice, mockClientMethodsDevice,
mockClientMethodsServer, mockClientMethodsServer,
mockClientMethodsUser, mockClientMethodsUser,
withClientContextRenderOptions,
} from "../../../../test-utils"; } from "../../../../test-utils";
import SettingsStore from "../../../../../src/settings/SettingsStore"; import SettingsStore from "../../../../../src/settings/SettingsStore";
import MStickerBody from "../../../../../src/components/views/messages/MStickerBody"; import MStickerBody from "../../../../../src/components/views/messages/MStickerBody";
@@ -31,6 +32,7 @@ describe("<MStickerBody/>", () => {
...mockClientMethodsServer(), ...mockClientMethodsServer(),
...mockClientMethodsDevice(deviceId), ...mockClientMethodsDevice(deviceId),
...mockClientMethodsCrypto(), ...mockClientMethodsCrypto(),
getRoom: jest.fn(),
getRooms: jest.fn().mockReturnValue([]), getRooms: jest.fn().mockReturnValue([]),
getIgnoredUsers: jest.fn(), getIgnoredUsers: jest.fn(),
getVersions: jest.fn().mockResolvedValue({ getVersions: jest.fn().mockResolvedValue({
@@ -76,7 +78,7 @@ describe("<MStickerBody/>", () => {
it("should show a tooltip on hover", async () => { it("should show a tooltip on hover", async () => {
fetchMock.getOnce(url, { status: 200 }); fetchMock.getOnce(url, { status: 200 });
render(<MStickerBody {...props} mxEvent={mediaEvent} />); render(<MStickerBody {...props} mxEvent={mediaEvent} />, withClientContextRenderOptions(cli));
expect(screen.queryByRole("tooltip")).toBeNull(); expect(screen.queryByRole("tooltip")).toBeNull();
await userEvent.hover(screen.getByRole("img")); await userEvent.hover(screen.getByRole("img"));

View File

@@ -98,6 +98,7 @@ describe("MVideoBody", () => {
fetchMock.getOnce(thumbUrl, { status: 200 }); fetchMock.getOnce(thumbUrl, { status: 200 });
const { asFragment } = render( const { asFragment } = render(
<MVideoBody mxEvent={encryptedMediaEvent} mediaEventHelper={new MediaEventHelper(encryptedMediaEvent)} />, <MVideoBody mxEvent={encryptedMediaEvent} mediaEventHelper={new MediaEventHelper(encryptedMediaEvent)} />,
withClientContextRenderOptions(cli),
); );
expect(asFragment()).toMatchSnapshot(); expect(asFragment()).toMatchSnapshot();
}); });