@@ -16,8 +16,9 @@ limitations under the License.
1616
1717import React from "react" ;
1818import { MatrixClient , MatrixEvent } from "matrix-js-sdk/src/matrix" ;
19- import { MockedObject } from "jest-mock" ;
19+ import { mocked , MockedObject } from "jest-mock" ;
2020import { render } from "@testing-library/react" ;
21+ import * as prettier from "prettier" ;
2122
2223import { getMockClientWithEventEmitter , mkEvent , mkMessage , mkStubRoom } from "../../../test-utils" ;
2324import { MatrixClientPeg } from "../../../../src/MatrixClientPeg" ;
@@ -28,10 +29,18 @@ import MatrixClientContext from "../../../../src/contexts/MatrixClientContext";
2829import { RoomPermalinkCreator } from "../../../../src/utils/permalinks/Permalinks" ;
2930import { MediaEventHelper } from "../../../../src/utils/MediaEventHelper" ;
3031
31- const mkRoomTextMessage = ( body : string ) : MatrixEvent => {
32+ const room1Id = "!room1:example.com" ;
33+ const room2Id = "!room2:example.com" ;
34+ const room2Name = "Room 2" ;
35+
36+ interface MkRoomTextMessageOpts {
37+ roomId ?: string ;
38+ }
39+
40+ const mkRoomTextMessage = ( body : string , mkRoomTextMessageOpts ?: MkRoomTextMessageOpts ) : MatrixEvent => {
3241 return mkMessage ( {
3342 msg : body ,
34- room : "room_id" ,
43+ room : mkRoomTextMessageOpts ?. roomId ?? room1Id ,
3544 user : "sender" ,
3645 event : true ,
3746 } ) ;
@@ -42,7 +51,7 @@ const mkFormattedMessage = (body: string, formattedBody: string): MatrixEvent =>
4251 msg : body ,
4352 formattedMsg : formattedBody ,
4453 format : "org.matrix.custom.html" ,
45- room : "room_id" ,
54+ room : room1Id ,
4655 user : "sender" ,
4756 event : true ,
4857 } ) ;
@@ -53,12 +62,29 @@ describe("<TextualBody />", () => {
5362 jest . spyOn ( MatrixClientPeg , "get" ) . mockRestore ( ) ;
5463 } ) ;
5564
56- const defaultRoom = mkStubRoom ( "room_id" , "test room" , undefined ) ;
65+ const defaultRoom = mkStubRoom ( room1Id , "test room" , undefined ) ;
66+ const otherRoom = mkStubRoom ( room2Id , room2Name , undefined ) ;
5767 let defaultMatrixClient : MockedObject < MatrixClient > ;
68+
69+ const defaultEvent = mkEvent ( {
70+ type : "m.room.message" ,
71+ room : room1Id ,
72+ user : "sender" ,
73+ content : {
74+ body : "winks" ,
75+ msgtype : "m.emote" ,
76+ } ,
77+ event : true ,
78+ } ) ;
79+
5880 beforeEach ( ( ) => {
5981 defaultMatrixClient = getMockClientWithEventEmitter ( {
60- getRoom : ( ) => defaultRoom ,
61- getRooms : ( ) => [ defaultRoom ] ,
82+ getRoom : ( roomId : string | undefined ) => {
83+ if ( roomId === room1Id ) return defaultRoom ;
84+ if ( roomId === room2Id ) return otherRoom ;
85+ return null ;
86+ } ,
87+ getRooms : ( ) => [ defaultRoom , otherRoom ] ,
6288 getAccountData : ( ) : MatrixEvent | undefined => undefined ,
6389 isGuest : ( ) => false ,
6490 mxcUrlToHttp : ( s : string ) => s ,
@@ -67,18 +93,13 @@ describe("<TextualBody />", () => {
6793 throw new Error ( "MockClient event not found" ) ;
6894 } ,
6995 } ) ;
70- } ) ;
7196
72- const defaultEvent = mkEvent ( {
73- type : "m.room.message" ,
74- room : "room_id" ,
75- user : "sender" ,
76- content : {
77- body : "winks" ,
78- msgtype : "m.emote" ,
79- } ,
80- event : true ,
97+ mocked ( defaultRoom ) . findEventById . mockImplementation ( ( eventId : string ) => {
98+ if ( eventId === defaultEvent . getId ( ) ) return defaultEvent ;
99+ return undefined ;
100+ } ) ;
81101 } ) ;
102+
82103 const defaultProps = {
83104 mxEvent : defaultEvent ,
84105 highlights : [ ] as string [ ] ,
@@ -88,6 +109,7 @@ describe("<TextualBody />", () => {
88109 permalinkCreator : new RoomPermalinkCreator ( defaultRoom ) ,
89110 mediaEventHelper : { } as MediaEventHelper ,
90111 } ;
112+
91113 const getComponent = ( props = { } , matrixClient : MatrixClient = defaultMatrixClient , renderingFn ?: any ) =>
92114 ( renderingFn ?? render ) (
93115 < MatrixClientContext . Provider value = { matrixClient } >
@@ -100,7 +122,7 @@ describe("<TextualBody />", () => {
100122
101123 const ev = mkEvent ( {
102124 type : "m.room.message" ,
103- room : "room_id" ,
125+ room : room1Id ,
104126 user : "sender" ,
105127 content : {
106128 body : "winks" ,
@@ -120,7 +142,7 @@ describe("<TextualBody />", () => {
120142
121143 const ev = mkEvent ( {
122144 type : "m.room.message" ,
123- room : "room_id" ,
145+ room : room1Id ,
124146 user : "bot_sender" ,
125147 content : {
126148 body : "this is a notice, probably from a bot" ,
@@ -196,13 +218,42 @@ describe("<TextualBody />", () => {
196218 `"Visit <span><bdi><a class="mx_Pill mx_RoomPill" href="https://matrix.to/#/#room:example.com"><div class="mx_Pill_LinkIcon mx_BaseAvatar mx_BaseAvatar_image"></div><span class="mx_Pill_text">#room:example.com</span></a></bdi></span>"` ,
197219 ) ;
198220 } ) ;
221+
222+ it ( "should pillify a permalink to a message in the same room with the label »Message from Member«" , ( ) => {
223+ const ev = mkRoomTextMessage ( `Visit https://matrix.to/#/${ room1Id } /${ defaultEvent . getId ( ) } ` ) ;
224+ const { container } = getComponent ( { mxEvent : ev } ) ;
225+ const content = container . querySelector ( ".mx_EventTile_body" ) ;
226+ expect (
227+ prettier . format ( content . innerHTML . replace ( defaultEvent . getId ( ) , "%event_id%" ) , {
228+ parser : "html" ,
229+ } ) ,
230+ ) . toMatchSnapshot ( ) ;
231+ } ) ;
232+
233+ it ( "should pillify a permalink to an unknown message in the same room with the label »Message«" , ( ) => {
234+ const ev = mkRoomTextMessage ( `Visit https://matrix.to/#/${ room1Id } /!abc123` ) ;
235+ const { container } = getComponent ( { mxEvent : ev } ) ;
236+ const content = container . querySelector ( ".mx_EventTile_body" ) ;
237+ expect ( content ) . toMatchSnapshot ( ) ;
238+ } ) ;
239+
240+ it ( "should pillify a permalink to an event in another room with the label »Message in Room 2«" , ( ) => {
241+ const ev = mkRoomTextMessage ( `Visit https://matrix.to/#/${ room2Id } /${ defaultEvent . getId ( ) } ` ) ;
242+ const { container } = getComponent ( { mxEvent : ev } ) ;
243+ const content = container . querySelector ( ".mx_EventTile_body" ) ;
244+ expect (
245+ prettier . format ( content . innerHTML . replace ( defaultEvent . getId ( ) , "%event_id%" ) , {
246+ parser : "html" ,
247+ } ) ,
248+ ) . toMatchSnapshot ( ) ;
249+ } ) ;
199250 } ) ;
200251
201252 describe ( "renders formatted m.text correctly" , ( ) => {
202253 let matrixClient : MatrixClient ;
203254 beforeEach ( ( ) => {
204255 matrixClient = getMockClientWithEventEmitter ( {
205- getRoom : ( ) => mkStubRoom ( "room_id" , "room name" , undefined ) ,
256+ getRoom : ( ) => mkStubRoom ( room1Id , "room name" , undefined ) ,
206257 getAccountData : ( ) : MatrixEvent | undefined => undefined ,
207258 getUserId : ( ) => "@me:my_server" ,
208259 getHomeserverUrl : ( ) => "https://my_server/" ,
0 commit comments