@@ -7,111 +7,123 @@ Please see LICENSE files in the repository root for full details.
7
7
*/
8
8
9
9
import React from "react" ;
10
- import { EventTimeline , MatrixEvent , Room , RoomMember } from "matrix-js-sdk/src/matrix" ;
11
- import { render , RenderOptions } from "jest-matrix-react" ;
10
+ import { MatrixClient , MatrixEvent , Room , RoomMember } from "matrix-js-sdk/src/matrix" ;
11
+ import { render , screen , act } from "jest-matrix-react" ;
12
12
13
- import { MatrixClientPeg } from "../../../../../src/MatrixClientPeg" ;
14
13
import SettingsStore from "../../../../../src/settings/SettingsStore" ;
15
- import MatrixClientContext from "../../../../../src/contexts/MatrixClientContext" ;
16
- import { _t } from "../../../../../src/languageHandler" ;
17
14
import { ShareDialog } from "../../../../../src/components/views/dialogs/ShareDialog" ;
18
15
import { UIFeature } from "../../../../../src/settings/UIFeature" ;
19
- import { stubClient } from "../../../../test-utils" ;
20
- jest . mock ( "../../../../../src/utils/ShieldUtils" ) ;
21
-
22
- function getWrapper ( ) : RenderOptions {
23
- return {
24
- wrapper : ( { children } ) => (
25
- < MatrixClientContext . Provider value = { MatrixClientPeg . safeGet ( ) } > { children } </ MatrixClientContext . Provider >
26
- ) ,
27
- } ;
28
- }
16
+ import { stubClient , withClientContextRenderOptions } from "../../../../test-utils" ;
17
+ import * as StringsModule from "../../../../../src/utils/strings" ;
18
+ import { RoomPermalinkCreator } from "../../../../../src/utils/permalinks/Permalinks.ts" ;
29
19
30
20
describe ( "ShareDialog" , ( ) => {
21
+ let client : MatrixClient ;
31
22
let room : Room ;
32
-
33
- const ROOM_ID = "!1:example.org" ;
23
+ const copyTextFunc = jest . fn ( ) ;
34
24
35
25
beforeEach ( async ( ) => {
36
- stubClient ( ) ;
37
- room = new Room ( ROOM_ID , MatrixClientPeg . get ( ) ! , "@alice:example.org" ) ;
26
+ client = stubClient ( ) ;
27
+ room = new Room ( "!1:example.org" , client , "@alice:example.org" ) ;
28
+ jest . spyOn ( StringsModule , "copyPlaintext" ) . mockImplementation ( copyTextFunc ) ;
38
29
} ) ;
39
30
40
31
afterEach ( ( ) => {
41
32
jest . restoreAllMocks ( ) ;
33
+ copyTextFunc . mockClear ( ) ;
42
34
} ) ;
43
35
44
- it ( "renders room share dialog" , ( ) => {
45
- const { container : withoutEvents } = render ( < ShareDialog target = { room } onFinished = { jest . fn ( ) } /> , getWrapper ( ) ) ;
46
- expect ( withoutEvents ) . toHaveTextContent ( _t ( "share|title_room" ) ) ;
36
+ function renderComponent ( target : Room | RoomMember | URL ) {
37
+ return render ( < ShareDialog target = { target } onFinished = { jest . fn ( ) } /> , withClientContextRenderOptions ( client ) ) ;
38
+ }
39
+
40
+ const getUrl = ( ) => new URL ( "https://matrix.org/" ) ;
41
+ const getRoomMember = ( ) => new RoomMember ( room . roomId , "@alice:example.org" ) ;
42
+
43
+ test . each ( [
44
+ { name : "an URL" , title : "Share Link" , url : "https://matrix.org/" , getTarget : getUrl } ,
45
+ {
46
+ name : "a room member" ,
47
+ title : "Share User" ,
48
+ url : "https://matrix.to/#/@alice:example.org" ,
49
+ getTarget : getRoomMember ,
50
+ } ,
51
+ ] ) ( "should render a share dialog for $name" , ( { title, url, getTarget } ) => {
52
+ const { asFragment } = renderComponent ( getTarget ( ) ) ;
53
+
54
+ expect ( screen . getByRole ( "heading" , { name : title } ) ) . toBeInTheDocument ( ) ;
55
+ expect ( screen . getByText ( url ) ) . toBeInTheDocument ( ) ;
56
+ expect ( asFragment ( ) ) . toMatchSnapshot ( ) ;
47
57
48
- jest . spyOn ( room , "getLiveTimeline" ) . mockReturnValue ( { getEvents : ( ) => [ { } as MatrixEvent ] } as EventTimeline ) ;
49
- const { container : withEvents } = render ( < ShareDialog target = { room } onFinished = { jest . fn ( ) } /> , getWrapper ( ) ) ;
50
- expect ( withEvents ) . toHaveTextContent ( _t ( "share|permalink_most_recent" ) ) ;
58
+ screen . getByRole ( "button" , { name : "Copy link" } ) . click ( ) ;
59
+ expect ( copyTextFunc ) . toHaveBeenCalledWith ( url ) ;
51
60
} ) ;
52
61
53
- it ( "renders user share dialog" , ( ) => {
54
- mockRoomMembers ( room , 1 ) ;
55
- const { container } = render (
56
- < ShareDialog target = { room . getJoinedMembers ( ) [ 0 ] } onFinished = { jest . fn ( ) } /> ,
57
- getWrapper ( ) ,
58
- ) ;
59
- expect ( container ) . toHaveTextContent ( _t ( "share|title_user" ) ) ;
62
+ it ( "should render a share dialog for a room" , ( ) => {
63
+ const expectedURL = "https://matrix.to/#/!1:example.org" ;
64
+ jest . spyOn ( room . getLiveTimeline ( ) , "getEvents" ) . mockReturnValue ( [ new MatrixEvent ( { event_id : "!eventId" } ) ] ) ;
65
+
66
+ const { asFragment } = renderComponent ( room ) ;
67
+ expect ( screen . getByRole ( "heading" , { name : "Share Room" } ) ) . toBeInTheDocument ( ) ;
68
+ expect ( screen . getByText ( expectedURL ) ) . toBeInTheDocument ( ) ;
69
+ expect ( screen . getByRole ( "checkbox" , { name : "Link to most recent message" } ) ) . toBeInTheDocument ( ) ;
70
+ expect ( asFragment ( ) ) . toMatchSnapshot ( ) ;
71
+
72
+ screen . getByRole ( "button" , { name : "Copy link" } ) . click ( ) ;
73
+ expect ( copyTextFunc ) . toHaveBeenCalledWith ( expectedURL ) ;
74
+
75
+ // Click on the checkbox to link to the most recent message
76
+ act ( ( ) => screen . getByRole ( "checkbox" , { name : "Link to most recent message" } ) . click ( ) ) ;
77
+ const newExpectedURL = "https://matrix.to/#/!1:example.org/!eventId" ;
78
+ expect ( screen . getByText ( newExpectedURL ) ) . toBeInTheDocument ( ) ;
79
+
80
+ screen . getByRole ( "button" , { name : "Copy link" } ) . click ( ) ;
81
+ expect ( copyTextFunc ) . toHaveBeenCalledWith ( newExpectedURL ) ;
60
82
} ) ;
61
83
62
- it ( "renders link share dialog" , ( ) => {
63
- mockRoomMembers ( room , 1 ) ;
64
- const { container } = render (
65
- < ShareDialog target = { new URL ( "https://matrix.org" ) } onFinished = { jest . fn ( ) } /> ,
66
- getWrapper ( ) ,
84
+ it ( "should render a share dialog for a matrix event" , ( ) => {
85
+ const matrixEvent = new MatrixEvent ( { event_id : "!eventId" } ) ;
86
+ const permalinkCreator = new RoomPermalinkCreator ( room ) ;
87
+ const expectedURL = "https://matrix.to/#/!1:example.org/!eventId" ;
88
+
89
+ const { asFragment } = render (
90
+ < ShareDialog target = { matrixEvent } permalinkCreator = { permalinkCreator } onFinished = { jest . fn ( ) } /> ,
91
+ withClientContextRenderOptions ( client ) ,
67
92
) ;
68
- expect ( container ) . toHaveTextContent ( _t ( "share|title_link" ) ) ;
93
+ expect ( screen . getByRole ( "heading" , { name : "Share Room Message" } ) ) . toBeInTheDocument ( ) ;
94
+ expect ( screen . getByText ( expectedURL ) ) . toBeInTheDocument ( ) ;
95
+ expect ( screen . getByRole ( "checkbox" , { name : "Link to selected message" } ) ) . toBeChecked ( ) ;
96
+ expect ( asFragment ( ) ) . toMatchSnapshot ( ) ;
97
+
98
+ screen . getByRole ( "button" , { name : "Copy link" } ) . click ( ) ;
99
+ expect ( copyTextFunc ) . toHaveBeenCalledWith ( expectedURL ) ;
100
+
101
+ // Click on the checkbox to link to the room
102
+ act ( ( ) => screen . getByRole ( "checkbox" , { name : "Link to selected message" } ) . click ( ) ) ;
103
+ expect ( screen . getByText ( "https://matrix.to/#/!1:example.org" ) ) . toBeInTheDocument ( ) ;
69
104
} ) ;
70
105
71
- it ( "renders the QR code if configured " , ( ) => {
106
+ it ( "should not render the QR code if disabled " , ( ) => {
72
107
const originalGetValue = SettingsStore . getValue ;
73
108
jest . spyOn ( SettingsStore , "getValue" ) . mockImplementation ( ( feature ) => {
74
- if ( feature === UIFeature . ShareQRCode ) return true ;
109
+ if ( feature === UIFeature . ShareQRCode ) return false ;
75
110
return originalGetValue ( feature ) ;
76
111
} ) ;
77
- const { container } = render ( < ShareDialog target = { room } onFinished = { jest . fn ( ) } /> , getWrapper ( ) ) ;
78
- const qrCodesVisible = container . getElementsByClassName ( "mx_ShareDialog_qrcode_container" ) . length > 0 ;
79
- expect ( qrCodesVisible ) . toBe ( true ) ;
112
+
113
+ const { asFragment } = renderComponent ( room ) ;
114
+ expect ( screen . queryByRole ( "img" , { name : "QR code" } ) ) . toBeNull ( ) ;
115
+ expect ( asFragment ( ) ) . toMatchSnapshot ( ) ;
80
116
} ) ;
81
117
82
- it ( "renders the social button if configured " , ( ) => {
118
+ it ( "should not render the socials if disabled " , ( ) => {
83
119
const originalGetValue = SettingsStore . getValue ;
84
120
jest . spyOn ( SettingsStore , "getValue" ) . mockImplementation ( ( feature ) => {
85
- if ( feature === UIFeature . ShareSocial ) return true ;
121
+ if ( feature === UIFeature . ShareSocial ) return false ;
86
122
return originalGetValue ( feature ) ;
87
123
} ) ;
88
- const { container } = render ( < ShareDialog target = { room } onFinished = { jest . fn ( ) } /> , getWrapper ( ) ) ;
89
- const qrCodesVisible = container . getElementsByClassName ( "mx_ShareDialog_social_container" ) . length > 0 ;
90
- expect ( qrCodesVisible ) . toBe ( true ) ;
91
- } ) ;
92
- it ( "renders custom title and subtitle" , ( ) => {
93
- const { container } = render (
94
- < ShareDialog
95
- target = { room }
96
- customTitle = "test_title_123"
97
- subtitle = "custom_subtitle_1234"
98
- onFinished = { jest . fn ( ) }
99
- /> ,
100
- getWrapper ( ) ,
101
- ) ;
102
- expect ( container ) . toHaveTextContent ( "test_title_123" ) ;
103
- expect ( container ) . toHaveTextContent ( "custom_subtitle_1234" ) ;
124
+
125
+ const { asFragment } = renderComponent ( room ) ;
126
+ expect ( screen . queryByRole ( "link" , { name : "Reddit" } ) ) . toBeNull ( ) ;
127
+ expect ( asFragment ( ) ) . toMatchSnapshot ( ) ;
104
128
} ) ;
105
129
} ) ;
106
- /**
107
- *
108
- * @param count the number of users to create
109
- */
110
- function mockRoomMembers ( room : Room , count : number ) {
111
- const members = Array ( count )
112
- . fill ( 0 )
113
- . map ( ( _ , index ) => new RoomMember ( room . roomId , "@alice:example.org" ) ) ;
114
-
115
- room . currentState . setJoinedMemberCount ( members . length ) ;
116
- room . getJoinedMembers = jest . fn ( ) . mockReturnValue ( members ) ;
117
- }
0 commit comments