@@ -17,11 +17,11 @@ limitations under the License.
1717import React from "react" ;
1818import { render , screen } from "@testing-library/react" ;
1919import { RoomType } from "matrix-js-sdk/src/@types/event" ;
20+ import { Room } from "matrix-js-sdk/src/matrix" ;
2021
2122import InviteDialog from "../../../../src/components/views/dialogs/InviteDialog" ;
22- import { KIND_INVITE } from "../../../../src/components/views/dialogs/InviteDialogTypes" ;
23- import { getMockClientWithEventEmitter , mkStubRoom } from "../../../test-utils" ;
24- import { MatrixClientPeg } from "../../../../src/MatrixClientPeg" ;
23+ import { KIND_DM , KIND_INVITE } from "../../../../src/components/views/dialogs/InviteDialogTypes" ;
24+ import { getMockClientWithEventEmitter , mkMembership , mkMessage , mkRoomCreateEvent } from "../../../test-utils" ;
2525import DMRoomMap from "../../../../src/utils/DMRoomMap" ;
2626import SdkConfig from "../../../../src/SdkConfig" ;
2727import { ValidatedServerConfig } from "../../../../src/utils/ValidatedServerConfig" ;
@@ -37,8 +37,11 @@ jest.mock("../../../../src/IdentityAuthClient", () =>
3737describe ( "InviteDialog" , ( ) => {
3838 const roomId = "!111111111111111111:example.org" ;
3939 const aliceId = "@alice:example.org" ;
40+ const aliceEmail = "[email protected] " ; 41+ const bobId = "@bob:example.org" ;
4042 const mockClient = getMockClientWithEventEmitter ( {
41- getUserId : jest . fn ( ) . mockReturnValue ( aliceId ) ,
43+ getUserId : jest . fn ( ) . mockReturnValue ( bobId ) ,
44+ getSafeUserId : jest . fn ( ) . mockReturnValue ( bobId ) ,
4245 isGuest : jest . fn ( ) . mockReturnValue ( false ) ,
4346 getVisibleRooms : jest . fn ( ) . mockReturnValue ( [ ] ) ,
4447 getRoom : jest . fn ( ) ,
@@ -58,36 +61,61 @@ describe("InviteDialog", () => {
5861 getOpenIdToken : jest . fn ( ) . mockResolvedValue ( { } ) ,
5962 getIdentityAccount : jest . fn ( ) . mockResolvedValue ( { } ) ,
6063 getTerms : jest . fn ( ) . mockResolvedValue ( { policies : [ ] } ) ,
64+ supportsThreads : jest . fn ( ) . mockReturnValue ( false ) ,
65+ isInitialSyncComplete : jest . fn ( ) . mockReturnValue ( true ) ,
6166 } ) ;
67+ let room : Room ;
6268
6369 beforeEach ( ( ) => {
6470 SdkConfig . put ( { validated_server_config : { } as ValidatedServerConfig } as IConfigOptions ) ;
6571 DMRoomMap . makeShared ( ) ;
6672 jest . clearAllMocks ( ) ;
67- mockClient . getUserId . mockReturnValue ( "@bob:example.org" ) ;
68-
69- const room = mkStubRoom ( roomId , "Room" , mockClient ) ;
73+ mockClient . getUserId . mockReturnValue ( bobId ) ;
74+
75+ room = new Room ( roomId , mockClient , mockClient . getSafeUserId ( ) ) ;
76+ room . addLiveEvents ( [
77+ mkMessage ( {
78+ msg : "Hello" ,
79+ relatesTo : undefined ,
80+ event : true ,
81+ room : roomId ,
82+ user : mockClient . getSafeUserId ( ) ,
83+ ts : Date . now ( ) ,
84+ } ) ,
85+ ] ) ;
86+ room . currentState . setStateEvents ( [
87+ mkRoomCreateEvent ( bobId , roomId ) ,
88+ mkMembership ( {
89+ event : true ,
90+ room : roomId ,
91+ mship : "join" ,
92+ user : aliceId ,
93+ skey : aliceId ,
94+ } ) ,
95+ ] ) ;
96+ jest . spyOn ( DMRoomMap . shared ( ) , "getUniqueRoomsWithIndividuals" ) . mockReturnValue ( {
97+ [ aliceId ] : room ,
98+ } ) ;
7099 mockClient . getRooms . mockReturnValue ( [ room ] ) ;
71100 mockClient . getRoom . mockReturnValue ( room ) ;
72101 } ) ;
73102
74103 afterAll ( ( ) => {
75- jest . spyOn ( MatrixClientPeg , "get" ) . mockRestore ( ) ;
104+ jest . restoreAllMocks ( ) ;
76105 } ) ;
77106
78107 it ( "should label with space name" , ( ) => {
79- mockClient . getRoom ( roomId ) . isSpaceRoom = jest . fn ( ) . mockReturnValue ( true ) ;
80- mockClient . getRoom ( roomId ) . getType = jest . fn ( ) . mockReturnValue ( RoomType . Space ) ;
81- mockClient . getRoom ( roomId ) . name = "Space" ;
108+ room . isSpaceRoom = jest . fn ( ) . mockReturnValue ( true ) ;
109+ room . getType = jest . fn ( ) . mockReturnValue ( RoomType . Space ) ;
110+ room . name = "Space" ;
82111 render ( < InviteDialog kind = { KIND_INVITE } roomId = { roomId } onFinished = { jest . fn ( ) } /> ) ;
83112
84113 expect ( screen . queryByText ( "Invite to Space" ) ) . toBeTruthy ( ) ;
85114 } ) ;
86115
87116 it ( "should label with room name" , ( ) => {
88117 render ( < InviteDialog kind = { KIND_INVITE } roomId = { roomId } onFinished = { jest . fn ( ) } /> ) ;
89-
90- expect ( screen . queryByText ( "Invite to Room" ) ) . toBeTruthy ( ) ;
118+ expect ( screen . getByText ( `Invite to ${ roomId } ` ) ) . toBeInTheDocument ( ) ;
91119 } ) ;
92120
93121 it ( "should suggest valid MXIDs even if unknown" , async ( ) => {
@@ -116,27 +144,37 @@ describe("InviteDialog", () => {
116144 expect ( screen . queryByText ( "@localpart:server:tld" ) ) . toBeFalsy ( ) ;
117145 } ) ;
118146
119- it ( "should lookup inputs which look like email addresses" , async ( ) => {
120- mockClient . getIdentityServerUrl . mockReturnValue ( "https://identity-server" ) ;
121- mockClient . lookupThreePid . mockResolvedValue ( {
122- 123- medium : "email" ,
124- mxid : "@foobar:server" ,
125- } ) ;
126- mockClient . getProfileInfo . mockResolvedValue ( {
127- displayname : "Mr. Foo" ,
128- avatar_url : "mxc://foo/bar" ,
129- } ) ;
130-
131- render (
132- < InviteDialog kind = { KIND_INVITE } roomId = { roomId } onFinished = { jest . fn ( ) } initialText = "[email protected] " /> , 133- ) ;
134-
135- await screen . findByText ( "Mr. Foo" ) ;
136- await screen . findByText ( "@foobar:server" ) ;
137- expect ( mockClient . lookupThreePid ) . toHaveBeenCalledWith ( "email" , "[email protected] " , expect . anything ( ) ) ; 138- expect ( mockClient . getProfileInfo ) . toHaveBeenCalledWith ( "@foobar:server" ) ;
139- } ) ;
147+ it . each ( [ [ KIND_DM ] , [ KIND_INVITE ] ] ) (
148+ "should lookup inputs which look like email addresses (%s)" ,
149+ async ( kind : typeof KIND_DM | typeof KIND_INVITE ) => {
150+ mockClient . getIdentityServerUrl . mockReturnValue ( "https://identity-server" ) ;
151+ mockClient . lookupThreePid . mockResolvedValue ( {
152+ address : aliceEmail ,
153+ medium : "email" ,
154+ mxid : aliceId ,
155+ } ) ;
156+ mockClient . getProfileInfo . mockResolvedValue ( {
157+ displayname : "Mrs Alice" ,
158+ avatar_url : "mxc://foo/bar" ,
159+ } ) ;
160+
161+ render (
162+ < InviteDialog
163+ kind = { kind }
164+ roomId = { kind === KIND_INVITE ? roomId : "" }
165+ onFinished = { jest . fn ( ) }
166+ initialText = { aliceEmail }
167+ /> ,
168+ ) ;
169+
170+ await screen . findByText ( "Mrs Alice" ) ;
171+ // expect the email and MXID to be visible
172+ await screen . findByText ( aliceId ) ;
173+ await screen . findByText ( aliceEmail ) ;
174+ expect ( mockClient . lookupThreePid ) . toHaveBeenCalledWith ( "email" , aliceEmail , expect . anything ( ) ) ;
175+ expect ( mockClient . getProfileInfo ) . toHaveBeenCalledWith ( aliceId ) ;
176+ } ,
177+ ) ;
140178
141179 it ( "should suggest e-mail even if lookup fails" , async ( ) => {
142180 mockClient . getIdentityServerUrl . mockReturnValue ( "https://identity-server" ) ;
0 commit comments