@@ -15,7 +15,7 @@ limitations under the License.
15
15
*/
16
16
17
17
import React from "react" ;
18
- import { render , screen } from "@testing-library/react" ;
18
+ import { fireEvent , render , screen } from "@testing-library/react" ;
19
19
import { EventType , MatrixEvent , Room } from "matrix-js-sdk/src/matrix" ;
20
20
import fetchMock from "fetch-mock-jest" ;
21
21
import encrypt from "matrix-encrypt-attachment" ;
@@ -31,6 +31,7 @@ import {
31
31
mockClientMethodsUser ,
32
32
} from "../../../test-utils" ;
33
33
import { MediaEventHelper } from "../../../../src/utils/MediaEventHelper" ;
34
+ import SettingsStore from "../../../../src/settings/SettingsStore" ;
34
35
35
36
jest . mock ( "matrix-encrypt-attachment" , ( ) => ( {
36
37
decryptAttachment : jest . fn ( ) ,
@@ -61,6 +62,7 @@ describe("<MImageBody/>", () => {
61
62
sender : userId ,
62
63
type : EventType . RoomMessage ,
63
64
content : {
65
+ body : "alt for a test image" ,
64
66
info : {
65
67
w : 40 ,
66
68
h : 50 ,
@@ -70,12 +72,18 @@ describe("<MImageBody/>", () => {
70
72
} ,
71
73
} ,
72
74
} ) ;
75
+
73
76
const props = {
74
77
onHeightChanged : jest . fn ( ) ,
75
78
onMessageAllowed : jest . fn ( ) ,
76
79
permalinkCreator : new RoomPermalinkCreator ( new Room ( encryptedMediaEvent . getRoomId ( ) ! , cli , cli . getUserId ( ) ! ) ) ,
77
80
} ;
78
81
82
+ beforeEach ( ( ) => {
83
+ jest . spyOn ( SettingsStore , "getValue" ) . mockRestore ( ) ;
84
+ fetchMock . mockReset ( ) ;
85
+ } ) ;
86
+
79
87
it ( "should show a thumbnail while image is being downloaded" , async ( ) => {
80
88
fetchMock . getOnce ( url , { status : 200 } ) ;
81
89
@@ -102,6 +110,8 @@ describe("<MImageBody/>", () => {
102
110
/> ,
103
111
) ;
104
112
113
+ expect ( fetchMock ) . toHaveBeenCalledWith ( url ) ;
114
+
105
115
await screen . findByText ( "Error downloading image" ) ;
106
116
} ) ;
107
117
@@ -119,4 +129,46 @@ describe("<MImageBody/>", () => {
119
129
120
130
await screen . findByText ( "Error decrypting image" ) ;
121
131
} ) ;
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
+ } ) ;
122
174
} ) ;
0 commit comments