@@ -16,8 +16,9 @@ limitations under the License.
16
16
17
17
import React from "react" ;
18
18
import { MatrixClient , MatrixEvent } from "matrix-js-sdk/src/matrix" ;
19
- import { MockedObject } from "jest-mock" ;
19
+ import { mocked , MockedObject } from "jest-mock" ;
20
20
import { render } from "@testing-library/react" ;
21
+ import * as prettier from "prettier" ;
21
22
22
23
import { getMockClientWithEventEmitter , mkEvent , mkMessage , mkStubRoom } from "../../../test-utils" ;
23
24
import { MatrixClientPeg } from "../../../../src/MatrixClientPeg" ;
@@ -28,10 +29,18 @@ import MatrixClientContext from "../../../../src/contexts/MatrixClientContext";
28
29
import { RoomPermalinkCreator } from "../../../../src/utils/permalinks/Permalinks" ;
29
30
import { MediaEventHelper } from "../../../../src/utils/MediaEventHelper" ;
30
31
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 => {
32
41
return mkMessage ( {
33
42
msg : body ,
34
- room : "room_id" ,
43
+ room : mkRoomTextMessageOpts ?. roomId ?? room1Id ,
35
44
user : "sender" ,
36
45
event : true ,
37
46
} ) ;
@@ -42,7 +51,7 @@ const mkFormattedMessage = (body: string, formattedBody: string): MatrixEvent =>
42
51
msg : body ,
43
52
formattedMsg : formattedBody ,
44
53
format : "org.matrix.custom.html" ,
45
- room : "room_id" ,
54
+ room : room1Id ,
46
55
user : "sender" ,
47
56
event : true ,
48
57
} ) ;
@@ -53,12 +62,29 @@ describe("<TextualBody />", () => {
53
62
jest . spyOn ( MatrixClientPeg , "get" ) . mockRestore ( ) ;
54
63
} ) ;
55
64
56
- const defaultRoom = mkStubRoom ( "room_id" , "test room" , undefined ) ;
65
+ const defaultRoom = mkStubRoom ( room1Id , "test room" , undefined ) ;
66
+ const otherRoom = mkStubRoom ( room2Id , room2Name , undefined ) ;
57
67
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
+
58
80
beforeEach ( ( ) => {
59
81
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 ] ,
62
88
getAccountData : ( ) : MatrixEvent | undefined => undefined ,
63
89
isGuest : ( ) => false ,
64
90
mxcUrlToHttp : ( s : string ) => s ,
@@ -67,18 +93,13 @@ describe("<TextualBody />", () => {
67
93
throw new Error ( "MockClient event not found" ) ;
68
94
} ,
69
95
} ) ;
70
- } ) ;
71
96
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
+ } ) ;
81
101
} ) ;
102
+
82
103
const defaultProps = {
83
104
mxEvent : defaultEvent ,
84
105
highlights : [ ] as string [ ] ,
@@ -88,6 +109,7 @@ describe("<TextualBody />", () => {
88
109
permalinkCreator : new RoomPermalinkCreator ( defaultRoom ) ,
89
110
mediaEventHelper : { } as MediaEventHelper ,
90
111
} ;
112
+
91
113
const getComponent = ( props = { } , matrixClient : MatrixClient = defaultMatrixClient , renderingFn ?: any ) =>
92
114
( renderingFn ?? render ) (
93
115
< MatrixClientContext . Provider value = { matrixClient } >
@@ -100,7 +122,7 @@ describe("<TextualBody />", () => {
100
122
101
123
const ev = mkEvent ( {
102
124
type : "m.room.message" ,
103
- room : "room_id" ,
125
+ room : room1Id ,
104
126
user : "sender" ,
105
127
content : {
106
128
body : "winks" ,
@@ -120,7 +142,7 @@ describe("<TextualBody />", () => {
120
142
121
143
const ev = mkEvent ( {
122
144
type : "m.room.message" ,
123
- room : "room_id" ,
145
+ room : room1Id ,
124
146
user : "bot_sender" ,
125
147
content : {
126
148
body : "this is a notice, probably from a bot" ,
@@ -196,13 +218,42 @@ describe("<TextualBody />", () => {
196
218
`"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>"` ,
197
219
) ;
198
220
} ) ;
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
+ } ) ;
199
250
} ) ;
200
251
201
252
describe ( "renders formatted m.text correctly" , ( ) => {
202
253
let matrixClient : MatrixClient ;
203
254
beforeEach ( ( ) => {
204
255
matrixClient = getMockClientWithEventEmitter ( {
205
- getRoom : ( ) => mkStubRoom ( "room_id" , "room name" , undefined ) ,
256
+ getRoom : ( ) => mkStubRoom ( room1Id , "room name" , undefined ) ,
206
257
getAccountData : ( ) : MatrixEvent | undefined => undefined ,
207
258
getUserId : ( ) => "@me:my_server" ,
208
259
getHomeserverUrl : ( ) => "https://my_server/" ,
0 commit comments