@@ -14,7 +14,7 @@ import encrypt, { IEncryptedFile } from "matrix-encrypt-attachment";
1414
1515import ContentMessages , { UploadCanceledError , uploadFile } from "../../src/ContentMessages" ;
1616import { doMaybeLocalRoomAction } from "../../src/utils/local-room" ;
17- import { createTestClient , mkEvent } from "../test-utils" ;
17+ import { createTestClient , flushPromises , mkEvent } from "../test-utils" ;
1818import { BlurhashEncoder } from "../../src/BlurhashEncoder" ;
1919
2020jest . mock ( "matrix-encrypt-attachment" , ( ) => ( { encryptAttachment : jest . fn ( ) . mockResolvedValue ( { } ) } ) ) ;
@@ -43,13 +43,7 @@ describe("ContentMessages", () => {
4343 let prom : Promise < ISendEventResponse > ;
4444
4545 beforeEach ( ( ) => {
46- client = {
47- getSafeUserId : jest . fn ( ) . mockReturnValue ( "@alice:test" ) ,
48- sendStickerMessage : jest . fn ( ) ,
49- sendMessage : jest . fn ( ) ,
50- isRoomEncrypted : jest . fn ( ) . mockReturnValue ( false ) ,
51- uploadContent : jest . fn ( ) . mockResolvedValue ( { content_uri : "mxc://server/file" } ) ,
52- } as unknown as MatrixClient ;
46+ client = createTestClient ( ) ;
5347 contentMessages = new ContentMessages ( ) ;
5448 prom = Promise . resolve < ISendEventResponse > ( { event_id : "$event_id" } ) ;
5549 } ) ;
@@ -262,6 +256,7 @@ describe("ContentMessages", () => {
262256
263257 expect ( upload . loaded ) . toBe ( 0 ) ;
264258 expect ( upload . total ) . toBe ( file . size ) ;
259+ await flushPromises ( ) ;
265260 const { progressHandler } = mocked ( client . uploadContent ) . mock . calls [ 0 ] [ 1 ] ! ;
266261 progressHandler ! ( { loaded : 123 , total : 1234 } ) ;
267262 expect ( upload . loaded ) . toBe ( 123 ) ;
@@ -342,6 +337,7 @@ describe("ContentMessages", () => {
342337 mocked ( client . uploadContent ) . mockReturnValue ( deferred . promise ) ;
343338 const file1 = new File ( [ ] , "file1" ) ;
344339 const prom = contentMessages . sendContentToRoom ( file1 , roomId , undefined , client , undefined ) ;
340+ await flushPromises ( ) ;
345341 const { abortController } = mocked ( client . uploadContent ) . mock . calls [ 0 ] [ 1 ] ! ;
346342 expect ( abortController ! . signal . aborted ) . toBeFalsy ( ) ;
347343 const [ upload ] = contentMessages . getCurrentUploads ( ) ;
@@ -354,14 +350,14 @@ describe("ContentMessages", () => {
354350} ) ;
355351
356352describe ( "uploadFile" , ( ) => {
353+ let client : MatrixClient ;
354+
357355 beforeEach ( ( ) => {
358356 jest . clearAllMocks ( ) ;
357+ client = createTestClient ( ) ;
359358 } ) ;
360359
361- const client = createTestClient ( ) ;
362-
363360 it ( "should not encrypt the file if the room isn't encrypted" , async ( ) => {
364- mocked ( client . isRoomEncrypted ) . mockReturnValue ( false ) ;
365361 mocked ( client . uploadContent ) . mockResolvedValue ( { content_uri : "mxc://server/file" } ) ;
366362 const progressHandler = jest . fn ( ) ;
367363 const file = new Blob ( [ ] ) ;
@@ -375,7 +371,7 @@ describe("uploadFile", () => {
375371 } ) ;
376372
377373 it ( "should encrypt the file if the room is encrypted" , async ( ) => {
378- mocked ( client . isRoomEncrypted ) . mockReturnValue ( true ) ;
374+ jest . spyOn ( client . getCrypto ( ) ! , "isEncryptionEnabledInRoom" ) . mockResolvedValue ( true ) ;
379375 mocked ( client . uploadContent ) . mockResolvedValue ( { content_uri : "mxc://server/file" } ) ;
380376 mocked ( encrypt . encryptAttachment ) . mockResolvedValue ( {
381377 data : new ArrayBuffer ( 123 ) ,
@@ -405,14 +401,13 @@ describe("uploadFile", () => {
405401 } ) ;
406402
407403 it ( "should throw UploadCanceledError upon aborting the upload" , async ( ) => {
408- mocked ( client . isRoomEncrypted ) . mockReturnValue ( false ) ;
409- const deferred = defer < UploadResponse > ( ) ;
410- mocked ( client . uploadContent ) . mockReturnValue ( deferred . promise ) ;
404+ mocked ( client . uploadContent ) . mockResolvedValue ( { content_uri : "mxc://foo/bar" } ) ;
411405 const file = new Blob ( [ ] ) ;
406+ const controller = new AbortController ( ) ;
407+ controller . abort ( ) ;
412408
413- const prom = uploadFile ( client , "!roomId:server" , file ) ;
414- mocked ( client . uploadContent ) . mock . calls [ 0 ] [ 1 ] ! . abortController ! . abort ( ) ;
415- deferred . resolve ( { content_uri : "mxc://foo/bar" } ) ;
416- await expect ( prom ) . rejects . toThrow ( UploadCanceledError ) ;
409+ await expect ( uploadFile ( client , "!roomId:server" , file , undefined , controller ) ) . rejects . toThrow (
410+ UploadCanceledError ,
411+ ) ;
417412 } ) ;
418413} ) ;
0 commit comments