@@ -19,11 +19,13 @@ import { mocked } from "jest-mock";
1919
2020import { Command , Commands , getCommand } from "../src/SlashCommands" ;
2121import { createTestClient } from "./test-utils" ;
22- import { MatrixClientPeg } from "../src/MatrixClientPeg" ;
2322import { LocalRoom , LOCAL_ROOM_ID_PREFIX } from "../src/models/LocalRoom" ;
2423import SettingsStore from "../src/settings/SettingsStore" ;
2524import LegacyCallHandler from "../src/LegacyCallHandler" ;
2625import { 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" ;
2729
2830describe ( "SlashCommands" , ( ) => {
2931 let client : MatrixClient ;
@@ -57,7 +59,6 @@ describe("SlashCommands", () => {
5759 jest . clearAllMocks ( ) ;
5860
5961 client = createTestClient ( ) ;
60- jest . spyOn ( MatrixClientPeg , "get" ) . mockReturnValue ( client ) ;
6162
6263 room = new Room ( roomId , client , client . getUserId ( ) ! ) ;
6364 localRoom = new LocalRoom ( localRoomId , client , client . getUserId ( ) ! ) ;
@@ -70,9 +71,16 @@ describe("SlashCommands", () => {
7071 const command = getCommand ( "/topic pizza" ) ;
7172 expect ( command . cmd ) . toBeDefined ( ) ;
7273 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 ) ;
7475 expect ( client . setRoomTopic ) . toHaveBeenCalledWith ( "room-id" , "pizza" , undefined ) ;
7576 } ) ;
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+ } ) ;
7684 } ) ;
7785
7886 describe . each ( [
@@ -104,12 +112,12 @@ describe("SlashCommands", () => {
104112 describe ( "isEnabled" , ( ) => {
105113 it ( "should return true for Room" , ( ) => {
106114 setCurrentRoom ( ) ;
107- expect ( command . isEnabled ( ) ) . toBe ( true ) ;
115+ expect ( command . isEnabled ( client ) ) . toBe ( true ) ;
108116 } ) ;
109117
110118 it ( "should return false for LocalRoom" , ( ) => {
111119 setCurrentLocalRoon ( ) ;
112- expect ( command . isEnabled ( ) ) . toBe ( false ) ;
120+ expect ( command . isEnabled ( client ) ) . toBe ( false ) ;
113121 } ) ;
114122 } ) ;
115123 } ) ;
@@ -127,12 +135,12 @@ describe("SlashCommands", () => {
127135
128136 it ( "should return true for Room" , ( ) => {
129137 setCurrentRoom ( ) ;
130- expect ( command . isEnabled ( ) ) . toBe ( true ) ;
138+ expect ( command . isEnabled ( client ) ) . toBe ( true ) ;
131139 } ) ;
132140
133141 it ( "should return false for LocalRoom" , ( ) => {
134142 setCurrentLocalRoon ( ) ;
135- expect ( command . isEnabled ( ) ) . toBe ( false ) ;
143+ expect ( command . isEnabled ( client ) ) . toBe ( false ) ;
136144 } ) ;
137145 } ) ;
138146
@@ -143,12 +151,12 @@ describe("SlashCommands", () => {
143151
144152 it ( "should return false for Room" , ( ) => {
145153 setCurrentRoom ( ) ;
146- expect ( command . isEnabled ( ) ) . toBe ( false ) ;
154+ expect ( command . isEnabled ( client ) ) . toBe ( false ) ;
147155 } ) ;
148156
149157 it ( "should return false for LocalRoom" , ( ) => {
150158 setCurrentLocalRoon ( ) ;
151- expect ( command . isEnabled ( ) ) . toBe ( false ) ;
159+ expect ( command . isEnabled ( client ) ) . toBe ( false ) ;
152160 } ) ;
153161 } ) ;
154162 } ) ;
@@ -169,12 +177,12 @@ describe("SlashCommands", () => {
169177
170178 it ( "should return true for Room" , ( ) => {
171179 setCurrentRoom ( ) ;
172- expect ( command . isEnabled ( ) ) . toBe ( true ) ;
180+ expect ( command . isEnabled ( client ) ) . toBe ( true ) ;
173181 } ) ;
174182
175183 it ( "should return false for LocalRoom" , ( ) => {
176184 setCurrentLocalRoon ( ) ;
177- expect ( command . isEnabled ( ) ) . toBe ( false ) ;
185+ expect ( command . isEnabled ( client ) ) . toBe ( false ) ;
178186 } ) ;
179187 } ) ;
180188
@@ -187,12 +195,12 @@ describe("SlashCommands", () => {
187195
188196 it ( "should return false for Room" , ( ) => {
189197 setCurrentRoom ( ) ;
190- expect ( command . isEnabled ( ) ) . toBe ( false ) ;
198+ expect ( command . isEnabled ( client ) ) . toBe ( false ) ;
191199 } ) ;
192200
193201 it ( "should return false for LocalRoom" , ( ) => {
194202 setCurrentLocalRoon ( ) ;
195- expect ( command . isEnabled ( ) ) . toBe ( false ) ;
203+ expect ( command . isEnabled ( client ) ) . toBe ( false ) ;
196204 } ) ;
197205 } ) ;
198206 } ) ;
@@ -209,7 +217,7 @@ describe("SlashCommands", () => {
209217 const command = getCommand ( "/part #foo:bar" ) ;
210218 expect ( command . cmd ) . toBeDefined ( ) ;
211219 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 ) ;
213221 expect ( client . leaveRoomChain ) . toHaveBeenCalledWith ( "room-id" , expect . anything ( ) ) ;
214222 } ) ;
215223
@@ -223,7 +231,7 @@ describe("SlashCommands", () => {
223231 const command = getCommand ( "/part #foo:bar" ) ;
224232 expect ( command . cmd ) . toBeDefined ( ) ;
225233 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 ! ) ;
227235 expect ( client . leaveRoomChain ) . toHaveBeenCalledWith ( "room-id" , expect . anything ( ) ) ;
228236 } ) ;
229237 } ) ;
@@ -232,11 +240,45 @@ describe("SlashCommands", () => {
232240 const command = findCommand ( commandName ) ! ;
233241
234242 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 ( ) ) ;
236244 } ) ;
237245
238246 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+ ) ;
240282 } ) ;
241283 } ) ;
242284} ) ;
0 commit comments