@@ -19,11 +19,13 @@ import { mocked } from "jest-mock";
19
19
20
20
import { Command , Commands , getCommand } from "../src/SlashCommands" ;
21
21
import { createTestClient } from "./test-utils" ;
22
- import { MatrixClientPeg } from "../src/MatrixClientPeg" ;
23
22
import { LocalRoom , LOCAL_ROOM_ID_PREFIX } from "../src/models/LocalRoom" ;
24
23
import SettingsStore from "../src/settings/SettingsStore" ;
25
24
import LegacyCallHandler from "../src/LegacyCallHandler" ;
26
25
import { SdkContextClass } from "../src/contexts/SDKContext" ;
26
+ import Modal from "../src/Modal" ;
27
+ import WidgetUtils from "../src/utils/WidgetUtils" ;
28
+ import { WidgetType } from "../src/widgets/WidgetType" ;
27
29
28
30
describe ( "SlashCommands" , ( ) => {
29
31
let client : MatrixClient ;
@@ -57,7 +59,6 @@ describe("SlashCommands", () => {
57
59
jest . clearAllMocks ( ) ;
58
60
59
61
client = createTestClient ( ) ;
60
- jest . spyOn ( MatrixClientPeg , "get" ) . mockReturnValue ( client ) ;
61
62
62
63
room = new Room ( roomId , client , client . getUserId ( ) ! ) ;
63
64
localRoom = new LocalRoom ( localRoomId , client , client . getUserId ( ) ! ) ;
@@ -70,9 +71,16 @@ describe("SlashCommands", () => {
70
71
const command = getCommand ( "/topic pizza" ) ;
71
72
expect ( command . cmd ) . toBeDefined ( ) ;
72
73
expect ( command . args ) . toBeDefined ( ) ;
73
- await command . cmd ! . run ( "room-id" , null , command . args ) ;
74
+ await command . cmd ! . run ( client , "room-id" , null , command . args ) ;
74
75
expect ( client . setRoomTopic ) . toHaveBeenCalledWith ( "room-id" , "pizza" , undefined ) ;
75
76
} ) ;
77
+
78
+ it ( "should show topic modal if no args passed" , async ( ) => {
79
+ const spy = jest . spyOn ( Modal , "createDialog" ) ;
80
+ const command = getCommand ( "/topic" ) ! ;
81
+ await command . cmd ! . run ( client , roomId , null ) ;
82
+ expect ( spy ) . toHaveBeenCalled ( ) ;
83
+ } ) ;
76
84
} ) ;
77
85
78
86
describe . each ( [
@@ -104,12 +112,12 @@ describe("SlashCommands", () => {
104
112
describe ( "isEnabled" , ( ) => {
105
113
it ( "should return true for Room" , ( ) => {
106
114
setCurrentRoom ( ) ;
107
- expect ( command . isEnabled ( ) ) . toBe ( true ) ;
115
+ expect ( command . isEnabled ( client ) ) . toBe ( true ) ;
108
116
} ) ;
109
117
110
118
it ( "should return false for LocalRoom" , ( ) => {
111
119
setCurrentLocalRoon ( ) ;
112
- expect ( command . isEnabled ( ) ) . toBe ( false ) ;
120
+ expect ( command . isEnabled ( client ) ) . toBe ( false ) ;
113
121
} ) ;
114
122
} ) ;
115
123
} ) ;
@@ -127,12 +135,12 @@ describe("SlashCommands", () => {
127
135
128
136
it ( "should return true for Room" , ( ) => {
129
137
setCurrentRoom ( ) ;
130
- expect ( command . isEnabled ( ) ) . toBe ( true ) ;
138
+ expect ( command . isEnabled ( client ) ) . toBe ( true ) ;
131
139
} ) ;
132
140
133
141
it ( "should return false for LocalRoom" , ( ) => {
134
142
setCurrentLocalRoon ( ) ;
135
- expect ( command . isEnabled ( ) ) . toBe ( false ) ;
143
+ expect ( command . isEnabled ( client ) ) . toBe ( false ) ;
136
144
} ) ;
137
145
} ) ;
138
146
@@ -143,12 +151,12 @@ describe("SlashCommands", () => {
143
151
144
152
it ( "should return false for Room" , ( ) => {
145
153
setCurrentRoom ( ) ;
146
- expect ( command . isEnabled ( ) ) . toBe ( false ) ;
154
+ expect ( command . isEnabled ( client ) ) . toBe ( false ) ;
147
155
} ) ;
148
156
149
157
it ( "should return false for LocalRoom" , ( ) => {
150
158
setCurrentLocalRoon ( ) ;
151
- expect ( command . isEnabled ( ) ) . toBe ( false ) ;
159
+ expect ( command . isEnabled ( client ) ) . toBe ( false ) ;
152
160
} ) ;
153
161
} ) ;
154
162
} ) ;
@@ -169,12 +177,12 @@ describe("SlashCommands", () => {
169
177
170
178
it ( "should return true for Room" , ( ) => {
171
179
setCurrentRoom ( ) ;
172
- expect ( command . isEnabled ( ) ) . toBe ( true ) ;
180
+ expect ( command . isEnabled ( client ) ) . toBe ( true ) ;
173
181
} ) ;
174
182
175
183
it ( "should return false for LocalRoom" , ( ) => {
176
184
setCurrentLocalRoon ( ) ;
177
- expect ( command . isEnabled ( ) ) . toBe ( false ) ;
185
+ expect ( command . isEnabled ( client ) ) . toBe ( false ) ;
178
186
} ) ;
179
187
} ) ;
180
188
@@ -187,12 +195,12 @@ describe("SlashCommands", () => {
187
195
188
196
it ( "should return false for Room" , ( ) => {
189
197
setCurrentRoom ( ) ;
190
- expect ( command . isEnabled ( ) ) . toBe ( false ) ;
198
+ expect ( command . isEnabled ( client ) ) . toBe ( false ) ;
191
199
} ) ;
192
200
193
201
it ( "should return false for LocalRoom" , ( ) => {
194
202
setCurrentLocalRoon ( ) ;
195
- expect ( command . isEnabled ( ) ) . toBe ( false ) ;
203
+ expect ( command . isEnabled ( client ) ) . toBe ( false ) ;
196
204
} ) ;
197
205
} ) ;
198
206
} ) ;
@@ -209,7 +217,7 @@ describe("SlashCommands", () => {
209
217
const command = getCommand ( "/part #foo:bar" ) ;
210
218
expect ( command . cmd ) . toBeDefined ( ) ;
211
219
expect ( command . args ) . toBeDefined ( ) ;
212
- await command . cmd ! . run ( "room-id" , null , command . args ) ;
220
+ await command . cmd ! . run ( client , "room-id" , null , command . args ) ;
213
221
expect ( client . leaveRoomChain ) . toHaveBeenCalledWith ( "room-id" , expect . anything ( ) ) ;
214
222
} ) ;
215
223
@@ -223,7 +231,7 @@ describe("SlashCommands", () => {
223
231
const command = getCommand ( "/part #foo:bar" ) ;
224
232
expect ( command . cmd ) . toBeDefined ( ) ;
225
233
expect ( command . args ) . toBeDefined ( ) ;
226
- await command . cmd ! . run ( "room-id" , null , command . args ! ) ;
234
+ await command . cmd ! . run ( client , "room-id" , null , command . args ! ) ;
227
235
expect ( client . leaveRoomChain ) . toHaveBeenCalledWith ( "room-id" , expect . anything ( ) ) ;
228
236
} ) ;
229
237
} ) ;
@@ -232,11 +240,45 @@ describe("SlashCommands", () => {
232
240
const command = findCommand ( commandName ) ! ;
233
241
234
242
it ( "should return usage if no args" , ( ) => {
235
- expect ( command . run ( roomId , null , undefined ) . error ) . toBe ( command . getUsage ( ) ) ;
243
+ expect ( command . run ( client , roomId , null , undefined ) . error ) . toBe ( command . getUsage ( ) ) ;
236
244
} ) ;
237
245
238
246
it ( "should make things rainbowy" , ( ) => {
239
- return expect ( command . run ( roomId , null , "this is a test message" ) . promise ) . resolves . toMatchSnapshot ( ) ;
247
+ return expect (
248
+ command . run ( client , roomId , null , "this is a test message" ) . promise ,
249
+ ) . resolves . toMatchSnapshot ( ) ;
250
+ } ) ;
251
+ } ) ;
252
+
253
+ describe . each ( [ "shrug" , "tableflip" , "unflip" , "lenny" ] ) ( "/%s" , ( commandName : string ) => {
254
+ const command = findCommand ( commandName ) ! ;
255
+
256
+ it ( "should match snapshot with no args" , ( ) => {
257
+ return expect ( command . run ( client , roomId , null ) . promise ) . resolves . toMatchSnapshot ( ) ;
258
+ } ) ;
259
+
260
+ it ( "should match snapshot with args" , ( ) => {
261
+ return expect (
262
+ command . run ( client , roomId , null , "this is a test message" ) . promise ,
263
+ ) . resolves . toMatchSnapshot ( ) ;
264
+ } ) ;
265
+ } ) ;
266
+
267
+ describe ( "/addwidget" , ( ) => {
268
+ it ( "should parse html iframe snippets" , async ( ) => {
269
+ jest . spyOn ( WidgetUtils , "canUserModifyWidgets" ) . mockReturnValue ( true ) ;
270
+ const spy = jest . spyOn ( WidgetUtils , "setRoomWidget" ) ;
271
+ const command = findCommand ( "addwidget" ) ! ;
272
+ await command . run ( client , roomId , null , '<iframe src="https://element.io"></iframe>' ) ;
273
+ expect ( spy ) . toHaveBeenCalledWith (
274
+ client ,
275
+ roomId ,
276
+ expect . any ( String ) ,
277
+ WidgetType . CUSTOM ,
278
+ "https://element.io" ,
279
+ "Custom" ,
280
+ { } ,
281
+ ) ;
240
282
} ) ;
241
283
} ) ;
242
284
} ) ;
0 commit comments