@@ -15,7 +15,7 @@ limitations under the License.
1515*/
1616
1717import React from "react" ;
18- import { render , screen } from "@testing-library/react" ;
18+ import { fireEvent , render , screen } from "@testing-library/react" ;
1919import { EventType , MatrixEvent , Room } from "matrix-js-sdk/src/matrix" ;
2020import fetchMock from "fetch-mock-jest" ;
2121import encrypt from "matrix-encrypt-attachment" ;
@@ -31,6 +31,7 @@ import {
3131 mockClientMethodsUser ,
3232} from "../../../test-utils" ;
3333import { MediaEventHelper } from "../../../../src/utils/MediaEventHelper" ;
34+ import SettingsStore from "../../../../src/settings/SettingsStore" ;
3435
3536jest . 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