@@ -20,14 +20,18 @@ import { AccountAuthInfo } from "@matrix-org/react-sdk-module-api/lib/types/Acco
20
20
import { DialogContent , DialogProps } from "@matrix-org/react-sdk-module-api/lib/components/DialogContent" ;
21
21
import { screen , within } from "@testing-library/react" ;
22
22
import userEvent from "@testing-library/user-event" ;
23
+ import { MatrixClient } from "matrix-js-sdk/src/matrix" ;
24
+ import { Mocked } from "jest-mock" ;
23
25
24
26
import { ProxiedModuleApi } from "../../src/modules/ProxiedModuleApi" ;
25
- import { stubClient } from "../test-utils" ;
27
+ import { getMockClientWithEventEmitter , mkRoom , stubClient } from "../test-utils" ;
26
28
import { setLanguage } from "../../src/languageHandler" ;
27
29
import { ModuleRunner } from "../../src/modules/ModuleRunner" ;
28
30
import { registerMockModule } from "./MockModule" ;
29
31
import defaultDispatcher from "../../src/dispatcher/dispatcher" ;
30
32
import { Action } from "../../src/dispatcher/actions" ;
33
+ import WidgetStore , { IApp } from "../../src/stores/WidgetStore" ;
34
+ import { Container , WidgetLayoutStore } from "../../src/stores/widgets/WidgetLayoutStore" ;
31
35
32
36
describe ( "ProxiedApiModule" , ( ) => {
33
37
afterEach ( ( ) => {
@@ -254,4 +258,103 @@ describe("ProxiedApiModule", () => {
254
258
expect ( dialog ) . not . toBeInTheDocument ( ) ;
255
259
} ) ;
256
260
} ) ;
261
+
262
+ describe ( "getApps" , ( ) => {
263
+ it ( "should return apps from the widget store" , ( ) => {
264
+ const api = new ProxiedModuleApi ( ) ;
265
+ const app = { } as unknown as IApp ;
266
+ const apps : IApp [ ] = [ app ] ;
267
+
268
+ jest . spyOn ( WidgetStore . instance , "getApps" ) . mockReturnValue ( apps ) ;
269
+ expect ( api . getApps ( "!room:example.com" ) ) . toEqual ( apps ) ;
270
+ } ) ;
271
+ } ) ;
272
+
273
+ describe ( "getAppAvatarUrl" , ( ) => {
274
+ const app = { } as unknown as IApp ;
275
+ const avatarUrl = "https://example.com/avatar.png" ;
276
+
277
+ let api : ProxiedModuleApi ;
278
+ let client : Mocked < MatrixClient > ;
279
+
280
+ beforeEach ( ( ) => {
281
+ api = new ProxiedModuleApi ( ) ;
282
+ client = getMockClientWithEventEmitter ( { mxcUrlToHttp : jest . fn ( ) . mockReturnValue ( avatarUrl ) } ) ;
283
+ } ) ;
284
+
285
+ it ( "should return null if the app has no avatar URL" , ( ) => {
286
+ expect ( api . getAppAvatarUrl ( app ) ) . toBeNull ( ) ;
287
+ } ) ;
288
+
289
+ it ( "should return the app avatar URL" , ( ) => {
290
+ expect ( api . getAppAvatarUrl ( { ...app , avatar_url : avatarUrl } ) ) . toBe ( avatarUrl ) ;
291
+ } ) ;
292
+
293
+ it ( "should support optional thumbnail params" , ( ) => {
294
+ api . getAppAvatarUrl ( { ...app , avatar_url : avatarUrl } , 1 , 2 , "3" ) ;
295
+ // eslint-disable-next-line no-restricted-properties
296
+ expect ( client . mxcUrlToHttp ) . toHaveBeenCalledWith ( avatarUrl , 1 , 2 , "3" ) ;
297
+ } ) ;
298
+ } ) ;
299
+
300
+ describe ( "isAppInContainer" , ( ) => {
301
+ const app = { } as unknown as IApp ;
302
+ const roomId = "!room:example.com" ;
303
+
304
+ let api : ProxiedModuleApi ;
305
+ let client : MatrixClient ;
306
+
307
+ beforeEach ( ( ) => {
308
+ api = new ProxiedModuleApi ( ) ;
309
+ client = stubClient ( ) ;
310
+
311
+ jest . spyOn ( WidgetLayoutStore . instance , "isInContainer" ) ;
312
+ } ) ;
313
+
314
+ it ( "should return false if there is no room" , ( ) => {
315
+ client . getRoom = jest . fn ( ) . mockReturnValue ( null ) ;
316
+
317
+ expect ( api . isAppInContainer ( app , Container . Top , roomId ) ) . toBe ( false ) ;
318
+ expect ( WidgetLayoutStore . instance . isInContainer ) . not . toHaveBeenCalled ( ) ;
319
+ } ) ;
320
+
321
+ it ( "should return false if the app is not in the container" , ( ) => {
322
+ jest . spyOn ( WidgetLayoutStore . instance , "isInContainer" ) . mockReturnValue ( false ) ;
323
+ expect ( api . isAppInContainer ( app , Container . Top , roomId ) ) . toBe ( false ) ;
324
+ } ) ;
325
+
326
+ it ( "should return true if the app is in the container" , ( ) => {
327
+ jest . spyOn ( WidgetLayoutStore . instance , "isInContainer" ) . mockReturnValue ( true ) ;
328
+ expect ( api . isAppInContainer ( app , Container . Top , roomId ) ) . toBe ( true ) ;
329
+ } ) ;
330
+ } ) ;
331
+
332
+ describe ( "moveAppToContainer" , ( ) => {
333
+ const app = { } as unknown as IApp ;
334
+ const roomId = "!room:example.com" ;
335
+
336
+ let api : ProxiedModuleApi ;
337
+ let client : MatrixClient ;
338
+
339
+ beforeEach ( ( ) => {
340
+ api = new ProxiedModuleApi ( ) ;
341
+ client = stubClient ( ) ;
342
+
343
+ jest . spyOn ( WidgetLayoutStore . instance , "moveToContainer" ) ;
344
+ } ) ;
345
+
346
+ it ( "should not move if there is no room" , ( ) => {
347
+ client . getRoom = jest . fn ( ) . mockReturnValue ( null ) ;
348
+ api . moveAppToContainer ( app , Container . Top , roomId ) ;
349
+ expect ( WidgetLayoutStore . instance . moveToContainer ) . not . toHaveBeenCalled ( ) ;
350
+ } ) ;
351
+
352
+ it ( "should move if there is a room" , ( ) => {
353
+ const room = mkRoom ( client , roomId ) ;
354
+ client . getRoom = jest . fn ( ) . mockReturnValue ( room ) ;
355
+
356
+ api . moveAppToContainer ( app , Container . Top , roomId ) ;
357
+ expect ( WidgetLayoutStore . instance . moveToContainer ) . toHaveBeenCalledWith ( room , app , Container . Top ) ;
358
+ } ) ;
359
+ } ) ;
257
360
} ) ;
0 commit comments