Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 25c719b

Browse files
Kerrygithub-actions[bot]
Kerry
authored andcommitted
Fix: reveal images when image previews are disabled (#10781)
* fix image wrapping when showImage previews is disabled * strict v2 (cherry picked from commit 542bf68)
1 parent 23365ed commit 25c719b

File tree

2 files changed

+70
-9
lines changed

2 files changed

+70
-9
lines changed

src/components/views/messages/MImageBody.tsx

+17-8
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ export default class MImageBody extends React.Component<IBodyProps, IState> {
389389
thumbUrl: string | null,
390390
content: IMediaEventContent,
391391
forcedHeight?: number,
392-
): JSX.Element {
392+
): ReactNode {
393393
if (!thumbUrl) thumbUrl = contentUrl; // fallback
394394

395395
// magic number
@@ -524,16 +524,25 @@ export default class MImageBody extends React.Component<IBodyProps, IState> {
524524
</div>
525525
);
526526

527-
return contentUrl ? this.wrapImage(contentUrl, thumbnail) : thumbnail;
527+
return this.wrapImage(contentUrl, thumbnail);
528528
}
529529

530530
// Overridden by MStickerBody
531-
protected wrapImage(contentUrl: string, children: JSX.Element): JSX.Element {
532-
return (
533-
<a href={contentUrl} target={this.props.forExport ? "_blank" : undefined} onClick={this.onClick}>
534-
{children}
535-
</a>
536-
);
531+
protected wrapImage(contentUrl: string | null | undefined, children: JSX.Element): ReactNode {
532+
if (contentUrl) {
533+
return (
534+
<a href={contentUrl} target={this.props.forExport ? "_blank" : undefined} onClick={this.onClick}>
535+
{children}
536+
</a>
537+
);
538+
} else if (!this.state.showImage) {
539+
return (
540+
<div role="button" onClick={this.onClick}>
541+
{children}
542+
</div>
543+
);
544+
}
545+
return children;
537546
}
538547

539548
// Overridden by MStickerBody

test/components/views/messages/MImageBody-test.tsx

+53-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ limitations under the License.
1515
*/
1616

1717
import React from "react";
18-
import { render, screen } from "@testing-library/react";
18+
import { fireEvent, render, screen } from "@testing-library/react";
1919
import { EventType, MatrixEvent, Room } from "matrix-js-sdk/src/matrix";
2020
import fetchMock from "fetch-mock-jest";
2121
import encrypt from "matrix-encrypt-attachment";
@@ -31,6 +31,7 @@ import {
3131
mockClientMethodsUser,
3232
} from "../../../test-utils";
3333
import { MediaEventHelper } from "../../../../src/utils/MediaEventHelper";
34+
import SettingsStore from "../../../../src/settings/SettingsStore";
3435

3536
jest.mock("matrix-encrypt-attachment", () => ({
3637
decryptAttachment: jest.fn(),
@@ -61,6 +62,7 @@ describe("<MImageBody/>", () => {
6162
sender: userId,
6263
type: EventType.RoomMessage,
6364
content: {
65+
body: "alt for a test image",
6466
info: {
6567
w: 40,
6668
h: 50,
@@ -70,12 +72,18 @@ describe("<MImageBody/>", () => {
7072
},
7173
},
7274
});
75+
7376
const props = {
7477
onHeightChanged: jest.fn(),
7578
onMessageAllowed: jest.fn(),
7679
permalinkCreator: new RoomPermalinkCreator(new Room(encryptedMediaEvent.getRoomId()!, cli, cli.getUserId()!)),
7780
};
7881

82+
beforeEach(() => {
83+
jest.spyOn(SettingsStore, "getValue").mockRestore();
84+
fetchMock.mockReset();
85+
});
86+
7987
it("should show a thumbnail while image is being downloaded", async () => {
8088
fetchMock.getOnce(url, { status: 200 });
8189

@@ -102,6 +110,8 @@ describe("<MImageBody/>", () => {
102110
/>,
103111
);
104112

113+
expect(fetchMock).toHaveBeenCalledWith(url);
114+
105115
await screen.findByText("Error downloading image");
106116
});
107117

@@ -119,4 +129,46 @@ describe("<MImageBody/>", () => {
119129

120130
await screen.findByText("Error decrypting image");
121131
});
132+
133+
describe("with image previews/thumbnails disabled", () => {
134+
beforeEach(() => {
135+
jest.spyOn(SettingsStore, "getValue").mockReturnValue(false);
136+
});
137+
138+
it("should not download image", async () => {
139+
fetchMock.getOnce(url, { status: 200 });
140+
141+
render(
142+
<MImageBody
143+
{...props}
144+
mxEvent={encryptedMediaEvent}
145+
mediaEventHelper={new MediaEventHelper(encryptedMediaEvent)}
146+
/>,
147+
);
148+
149+
expect(fetchMock).not.toHaveFetched(url);
150+
});
151+
152+
it("should render hidden image placeholder", async () => {
153+
fetchMock.getOnce(url, { status: 200 });
154+
155+
render(
156+
<MImageBody
157+
{...props}
158+
mxEvent={encryptedMediaEvent}
159+
mediaEventHelper={new MediaEventHelper(encryptedMediaEvent)}
160+
/>,
161+
);
162+
163+
expect(screen.getByText("Show image")).toBeInTheDocument();
164+
165+
fireEvent.click(screen.getByRole("button"));
166+
167+
// image fetched after clicking show image
168+
expect(fetchMock).toHaveFetched(url);
169+
170+
// spinner while downloading image
171+
expect(screen.getByRole("progressbar")).toBeInTheDocument();
172+
});
173+
});
122174
});

0 commit comments

Comments
 (0)