@@ -37,6 +37,17 @@ const mkRoomTextMessage = (body: string): MatrixEvent => {
37
37
} ) ;
38
38
} ;
39
39
40
+ const mkFormattedMessage = ( body : string , formattedBody : string ) : MatrixEvent => {
41
+ return mkMessage ( {
42
+ msg : body ,
43
+ formattedMsg : formattedBody ,
44
+ format : "org.matrix.custom.html" ,
45
+ room : "room_id" ,
46
+ user : "sender" ,
47
+ event : true ,
48
+ } ) ;
49
+ } ;
50
+
40
51
describe ( "<TextualBody />" , ( ) => {
41
52
afterEach ( ( ) => {
42
53
jest . spyOn ( MatrixClientPeg , "get" ) . mockRestore ( ) ;
@@ -156,6 +167,15 @@ describe("<TextualBody />", () => {
156
167
) ;
157
168
} ) ;
158
169
170
+ it ( "should pillify an MXID permalink" , ( ) => {
171
+ const ev = mkRoomTextMessage ( "Chat with https://matrix.to/#/@user:example.com" ) ;
172
+ const { container } = getComponent ( { mxEvent : ev } ) ;
173
+ const content = container . querySelector ( ".mx_EventTile_body" ) ;
174
+ expect ( content . innerHTML ) . toMatchInlineSnapshot (
175
+ `"Chat with <span><bdi><a class="mx_Pill mx_UserPill mx_UserPill_me" href="https://matrix.to/#/@user:example.com"><img class="mx_BaseAvatar mx_BaseAvatar_image" src="mxc://avatar.url/image.png" style="width: 16px; height: 16px;" alt="" data-testid="avatar-img" aria-hidden="true"><span class="mx_Pill_linkText">Member</span></a></bdi></span>"` ,
176
+ ) ;
177
+ } ) ;
178
+
159
179
it ( "should not pillify room aliases" , ( ) => {
160
180
const ev = mkRoomTextMessage ( "Visit #room:example.com" ) ;
161
181
const { container } = getComponent ( { mxEvent : ev } ) ;
@@ -164,6 +184,15 @@ describe("<TextualBody />", () => {
164
184
`"Visit <a href="https://matrix.to/#/#room:example.com" class="linkified" rel="noreferrer noopener">#room:example.com</a>"` ,
165
185
) ;
166
186
} ) ;
187
+
188
+ it ( "should pillify a room alias permalink" , ( ) => {
189
+ const ev = mkRoomTextMessage ( "Visit https://matrix.to/#/#room:example.com" ) ;
190
+ const { container } = getComponent ( { mxEvent : ev } ) ;
191
+ const content = container . querySelector ( ".mx_EventTile_body" ) ;
192
+ expect ( content . innerHTML ) . toMatchInlineSnapshot (
193
+ `"Visit <span><bdi><a class="mx_Pill mx_RoomPill" href="https://matrix.to/#/#room:example.com"><span class="mx_Pill_linkText">#room:example.com</span></a></bdi></span>"` ,
194
+ ) ;
195
+ } ) ;
167
196
} ) ;
168
197
169
198
describe ( "renders formatted m.text correctly" , ( ) => {
@@ -183,19 +212,10 @@ describe("<TextualBody />", () => {
183
212
} ) ;
184
213
185
214
it ( "italics, bold, underline and strikethrough render as expected" , ( ) => {
186
- const ev = mkEvent ( {
187
- type : "m.room.message" ,
188
- room : "room_id" ,
189
- user : "sender" ,
190
- content : {
191
- body : "foo *baz* __bar__ <del>del</del> <u>u</u>" ,
192
- msgtype : "m.text" ,
193
- format : "org.matrix.custom.html" ,
194
- formatted_body : "foo <em>baz</em> <strong>bar</strong> <del>del</del> <u>u</u>" ,
195
- } ,
196
- event : true ,
197
- } ) ;
198
-
215
+ const ev = mkFormattedMessage (
216
+ "foo *baz* __bar__ <del>del</del> <u>u</u>" ,
217
+ "foo <em>baz</em> <strong>bar</strong> <del>del</del> <u>u</u>" ,
218
+ ) ;
199
219
const { container } = getComponent ( { mxEvent : ev } , matrixClient ) ;
200
220
expect ( container ) . toHaveTextContent ( "foo baz bar del u" ) ;
201
221
const content = container . querySelector ( ".mx_EventTile_body" ) ;
@@ -207,19 +227,10 @@ describe("<TextualBody />", () => {
207
227
} ) ;
208
228
209
229
it ( "spoilers get injected properly into the DOM" , ( ) => {
210
- const ev = mkEvent ( {
211
- type : "m.room.message" ,
212
- room : "room_id" ,
213
- user : "sender" ,
214
- content : {
215
- body : "Hey [Spoiler for movie](mxc://someserver/somefile)" ,
216
- msgtype : "m.text" ,
217
- format : "org.matrix.custom.html" ,
218
- formatted_body : 'Hey <span data-mx-spoiler="movie">the movie was awesome</span>' ,
219
- } ,
220
- event : true ,
221
- } ) ;
222
-
230
+ const ev = mkFormattedMessage (
231
+ "Hey [Spoiler for movie](mxc://someserver/somefile)" ,
232
+ 'Hey <span data-mx-spoiler="movie">the movie was awesome</span>' ,
233
+ ) ;
223
234
const { container } = getComponent ( { mxEvent : ev } , matrixClient ) ;
224
235
expect ( container ) . toHaveTextContent ( "Hey (movie) the movie was awesome" ) ;
225
236
const content = container . querySelector ( ".mx_EventTile_body" ) ;
@@ -234,19 +245,10 @@ describe("<TextualBody />", () => {
234
245
} ) ;
235
246
236
247
it ( "linkification is not applied to code blocks" , ( ) => {
237
- const ev = mkEvent ( {
238
- type : "m.room.message" ,
239
- room : "room_id" ,
240
- user : "sender" ,
241
- content : {
242
- body : "Visit `https://matrix.org/`\n```\nhttps://matrix.org/\n```" ,
243
- msgtype : "m.text" ,
244
- format : "org.matrix.custom.html" ,
245
- formatted_body : "<p>Visit <code>https://matrix.org/</code></p>\n<pre>https://matrix.org/\n</pre>\n" ,
246
- } ,
247
- event : true ,
248
- } ) ;
249
-
248
+ const ev = mkFormattedMessage (
249
+ "Visit `https://matrix.org/`\n```\nhttps://matrix.org/\n```" ,
250
+ "<p>Visit <code>https://matrix.org/</code></p>\n<pre>https://matrix.org/\n</pre>\n" ,
251
+ ) ;
250
252
const { container } = getComponent ( { mxEvent : ev } , matrixClient ) ;
251
253
expect ( container ) . toHaveTextContent ( "Visit https://matrix.org/ 1https://matrix.org/" ) ;
252
254
const content = container . querySelector ( ".mx_EventTile_body" ) ;
@@ -255,63 +257,32 @@ describe("<TextualBody />", () => {
255
257
256
258
// If pills were rendered within a Portal/same shadow DOM then it'd be easier to test
257
259
it ( "pills get injected correctly into the DOM" , ( ) => {
258
- const ev = mkEvent ( {
259
- type : "m.room.message" ,
260
- room : "room_id" ,
261
- user : "sender" ,
262
- content : {
263
- body : "Hey User" ,
264
- msgtype : "m.text" ,
265
- format : "org.matrix.custom.html" ,
266
- formatted_body : 'Hey <a href="https://matrix.to/#/@user:server">Member</a>' ,
267
- } ,
268
- event : true ,
269
- } ) ;
270
-
260
+ const ev = mkFormattedMessage ( "Hey User" , 'Hey <a href="https://matrix.to/#/@user:server">Member</a>' ) ;
271
261
const { container } = getComponent ( { mxEvent : ev } , matrixClient ) ;
272
262
expect ( container ) . toHaveTextContent ( "Hey Member" ) ;
273
263
const content = container . querySelector ( ".mx_EventTile_body" ) ;
274
264
expect ( content ) . toMatchSnapshot ( ) ;
275
265
} ) ;
276
266
277
267
it ( "pills do not appear in code blocks" , ( ) => {
278
- const ev = mkEvent ( {
279
- type : "m.room.message" ,
280
- room : "room_id" ,
281
- user : "sender" ,
282
- content : {
283
- body : "`@room`\n```\n@room\n```" ,
284
- msgtype : "m.text" ,
285
- format : "org.matrix.custom.html" ,
286
- formatted_body : "<p><code>@room</code></p>\n<pre><code>@room\n</code></pre>\n" ,
287
- } ,
288
- event : true ,
289
- } ) ;
290
-
268
+ const ev = mkFormattedMessage (
269
+ "`@room`\n```\n@room\n```" ,
270
+ "<p><code>@room</code></p>\n<pre><code>@room\n</code></pre>\n" ,
271
+ ) ;
291
272
const { container } = getComponent ( { mxEvent : ev } ) ;
292
273
expect ( container ) . toHaveTextContent ( "@room 1@room" ) ;
293
274
const content = container . querySelector ( ".mx_EventTile_body" ) ;
294
275
expect ( content ) . toMatchSnapshot ( ) ;
295
276
} ) ;
296
277
297
278
it ( "pills do not appear for event permalinks" , ( ) => {
298
- const ev = mkEvent ( {
299
- type : "m.room.message" ,
300
- room : "room_id" ,
301
- user : "sender" ,
302
- content : {
303
- body :
304
- "An [event link](https://matrix.to/#/!ZxbRYPQXDXKGmDnJNg:example.com/" +
305
- "$16085560162aNpaH:example.com?via=example.com) with text" ,
306
- msgtype : "m.text" ,
307
- format : "org.matrix.custom.html" ,
308
- formatted_body :
309
- 'An <a href="https://matrix.to/#/!ZxbRYPQXDXKGmDnJNg:example.com/' +
310
- '$16085560162aNpaH:example.com?via=example.com">event link</a> with text' ,
311
- } ,
312
- event : true ,
313
- } ) ;
279
+ const ev = mkFormattedMessage (
280
+ "An [event link](https://matrix.to/#/!ZxbRYPQXDXKGmDnJNg:example.com/" +
281
+ "$16085560162aNpaH:example.com?via=example.com) with text" ,
314
282
283
+ 'An <a href="https://matrix.to/#/!ZxbRYPQXDXKGmDnJNg:example.com/' +
284
+ '$16085560162aNpaH:example.com?via=example.com">event link</a> with text' ,
285
+ ) ;
315
286
const { container } = getComponent ( { mxEvent : ev } , matrixClient ) ;
316
287
expect ( container ) . toHaveTextContent ( "An event link with text" ) ;
317
288
const content = container . querySelector ( ".mx_EventTile_body" ) ;
@@ -324,23 +295,12 @@ describe("<TextualBody />", () => {
324
295
} ) ;
325
296
326
297
it ( "pills appear for room links with vias" , ( ) => {
327
- const ev = mkEvent ( {
328
- type : "m.room.message" ,
329
- room : "room_id" ,
330
- user : "sender" ,
331
- content : {
332
- body :
333
- "A [room link](https://matrix.to/#/!ZxbRYPQXDXKGmDnJNg:example.com" +
334
- "?via=example.com&via=bob.com) with vias" ,
335
- msgtype : "m.text" ,
336
- format : "org.matrix.custom.html" ,
337
- formatted_body :
338
- 'A <a href="https://matrix.to/#/!ZxbRYPQXDXKGmDnJNg:example.com' +
339
- '?via=example.com&via=bob.com">room link</a> with vias' ,
340
- } ,
341
- event : true ,
342
- } ) ;
343
-
298
+ const ev = mkFormattedMessage (
299
+ "A [room link](https://matrix.to/#/!ZxbRYPQXDXKGmDnJNg:example.com" +
300
+ "?via=example.com&via=bob.com) with vias" ,
301
+ 'A <a href="https://matrix.to/#/!ZxbRYPQXDXKGmDnJNg:example.com' +
302
+ '?via=example.com&via=bob.com">room link</a> with vias' ,
303
+ ) ;
344
304
const { container } = getComponent ( { mxEvent : ev } , matrixClient ) ;
345
305
expect ( container ) . toHaveTextContent ( "A room name with vias" ) ;
346
306
const content = container . querySelector ( ".mx_EventTile_body" ) ;
@@ -356,6 +316,16 @@ describe("<TextualBody />", () => {
356
316
) ;
357
317
} ) ;
358
318
319
+ it ( "pills appear for an MXID permalink" , ( ) => {
320
+ const ev = mkFormattedMessage (
321
+ "Chat with [@user:example.com](https://matrix.to/#/@user:example.com)" ,
322
+ 'Chat with <a href="https://matrix.to/#/@user:example.com">@user:example.com</a>' ,
323
+ ) ;
324
+ const { container } = getComponent ( { mxEvent : ev } , matrixClient ) ;
325
+ const content = container . querySelector ( ".mx_EventTile_body" ) ;
326
+ expect ( content ) . toMatchSnapshot ( ) ;
327
+ } ) ;
328
+
359
329
it ( "renders formatted body without html corretly" , ( ) => {
360
330
const ev = mkEvent ( {
361
331
type : "m.room.message" ,
0 commit comments