@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
1414limitations under the License.
1515*/
1616
17- import { EventStatus } from "matrix-js-sdk/src/matrix" ;
17+ import { EventStatus , IEventRelation } from "matrix-js-sdk/src/matrix" ;
1818
1919import { IRoomState } from "../../../../../../src/components/structures/RoomView" ;
2020import { editMessage , sendMessage } from "../../../../../../src/components/views/rooms/wysiwyg_composer/utils/message" ;
@@ -25,6 +25,8 @@ import { SettingLevel } from "../../../../../../src/settings/SettingLevel";
2525import { RoomPermalinkCreator } from "../../../../../../src/utils/permalinks/Permalinks" ;
2626import EditorStateTransfer from "../../../../../../src/utils/EditorStateTransfer" ;
2727import * as ConfirmRedactDialog from "../../../../../../src/components/views/dialogs/ConfirmRedactDialog" ;
28+ import * as SlashCommands from "../../../../../../src/SlashCommands" ;
29+ import * as Commands from "../../../../../../src/editor/commands" ;
2830
2931describe ( "message" , ( ) => {
3032 const permalinkCreator = {
@@ -226,6 +228,97 @@ describe("message", () => {
226228 // Then
227229 expect ( spyDispatcher ) . toHaveBeenCalledWith ( { action : "effects.confetti" } ) ;
228230 } ) ;
231+
232+ describe . only ( "slash commands" , ( ) => {
233+ afterEach ( ( ) => {
234+ jest . restoreAllMocks ( ) ;
235+ } ) ;
236+
237+ it ( "calls getCommand for a message starting with a valid command" , async ( ) => {
238+ // When
239+ const validCommand = "/spoiler" ;
240+ const getCommandSpy = jest . spyOn ( SlashCommands , "getCommand" ) ;
241+ await sendMessage ( validCommand , true , {
242+ roomContext : defaultRoomContext ,
243+ mxClient : mockClient ,
244+ permalinkCreator,
245+ } ) ;
246+
247+ // Then
248+ expect ( getCommandSpy ) . toHaveBeenCalledWith ( validCommand ) ;
249+ } ) ;
250+
251+ it ( "does not call getCommand for valid command with invalid prefix" , async ( ) => {
252+ // When
253+ const invalidPrefixCommand = "//spoiler" ;
254+ const getCommandSpy = jest . spyOn ( SlashCommands , "getCommand" ) ;
255+ await sendMessage ( invalidPrefixCommand , true , {
256+ roomContext : defaultRoomContext ,
257+ mxClient : mockClient ,
258+ permalinkCreator,
259+ } ) ;
260+
261+ // Then
262+ expect ( getCommandSpy ) . toHaveBeenCalledTimes ( 0 ) ;
263+ } ) ;
264+
265+ // TODO amend test when TS fixes are made - this currently can't actually return undefined
266+ // according to the TS types
267+ it ( "returns undefined when the command is not successful" , async ( ) => {
268+ // When
269+ const validCommand = "/spoiler" ;
270+ jest . spyOn ( Commands , "runSlashCommand" ) . mockResolvedValue ( [ { content : "mock content" } , false ] ) ;
271+
272+ const result = await sendMessage ( validCommand , true , {
273+ roomContext : defaultRoomContext ,
274+ mxClient : mockClient ,
275+ permalinkCreator,
276+ } ) ;
277+
278+ // Then
279+ expect ( result ) . toBeUndefined ( ) ;
280+ } ) ;
281+
282+ // /spoiler is a .messages category command, /fireworks is an .effect category command
283+ const messagesAndEffectCategoryTestCases = [ "/spoiler text" , "/fireworks" ] ;
284+
285+ it . each ( messagesAndEffectCategoryTestCases ) (
286+ "does not add relations for a .messages or .effects category command if there is no relation to add" ,
287+ async ( inputText ) => {
288+ await sendMessage ( inputText , true , {
289+ roomContext : defaultRoomContext ,
290+ mxClient : mockClient ,
291+ permalinkCreator,
292+ } ) ;
293+ expect ( mockClient . sendMessage ) . toHaveBeenCalledWith (
294+ "myfakeroom" ,
295+ null ,
296+ expect . not . objectContaining ( { "m.relates_to" : expect . any } ) ,
297+ ) ;
298+ } ,
299+ ) ;
300+
301+ it . each ( messagesAndEffectCategoryTestCases ) (
302+ "adds relations for a .messages or .effects category command if there is a relation" ,
303+ async ( inputText ) => {
304+ const mockRelation : IEventRelation = {
305+ rel_type : "mock relation type" ,
306+ } ;
307+ await sendMessage ( inputText , true , {
308+ roomContext : defaultRoomContext ,
309+ mxClient : mockClient ,
310+ permalinkCreator,
311+ relation : mockRelation ,
312+ } ) ;
313+
314+ expect ( mockClient . sendMessage ) . toHaveBeenCalledWith (
315+ "myfakeroom" ,
316+ null ,
317+ expect . objectContaining ( { "m.relates_to" : expect . objectContaining ( mockRelation ) } ) ,
318+ ) ;
319+ } ,
320+ ) ;
321+ } ) ;
229322 } ) ;
230323
231324 describe ( "editMessage" , ( ) => {
0 commit comments