1
1
/*
2
- Copyright 2022 The Matrix.org Foundation C.I.C.
2
+ Copyright 2022 - 2023 The Matrix.org Foundation C.I.C.
3
3
4
4
Licensed under the Apache License, Version 2.0 (the "License");
5
5
you may not use this file except in compliance with the License.
@@ -14,26 +14,36 @@ See the License for the specific language governing permissions and
14
14
limitations under the License.
15
15
*/
16
16
17
- import { mocked } from "jest-mock " ;
17
+ import { EventType , IRoomEvent , MatrixClient , Room } from "matrix-js-sdk/src/matrix " ;
18
18
19
- import { createTestClient , mkStubRoom , REPEATABLE_DATE } from "../../test-utils" ;
19
+ import { mkStubRoom , REPEATABLE_DATE , stubClient } from "../../test-utils" ;
20
20
import { ExportType , IExportOptions } from "../../../src/utils/exportUtils/exportUtils" ;
21
21
import SdkConfig from "../../../src/SdkConfig" ;
22
22
import HTMLExporter from "../../../src/utils/exportUtils/HtmlExport" ;
23
+ import DMRoomMap from "../../../src/utils/DMRoomMap" ;
24
+
25
+ jest . mock ( "jszip" ) ;
23
26
24
27
describe ( "HTMLExport" , ( ) => {
28
+ let client : jest . Mocked < MatrixClient > ;
29
+
25
30
beforeEach ( ( ) => {
26
31
jest . useFakeTimers ( ) ;
27
32
jest . setSystemTime ( REPEATABLE_DATE ) ;
28
- } ) ;
29
33
30
- afterEach ( ( ) => {
31
- mocked ( SdkConfig . get ) . mockRestore ( ) ;
34
+ client = stubClient ( ) as jest . Mocked < MatrixClient > ;
35
+ DMRoomMap . makeShared ( ) ;
32
36
} ) ;
33
37
38
+ function getMessageFile ( exporter : HTMLExporter ) : Blob {
39
+ //@ts -ignore private access
40
+ const files = exporter . files ;
41
+ const file = files . find ( ( f ) => f . name == "messages.html" ) ! ;
42
+ return file . blob ;
43
+ }
44
+
34
45
it ( "should have an SDK-branded destination file name" , ( ) => {
35
46
const roomName = "My / Test / Room: Welcome" ;
36
- const client = createTestClient ( ) ;
37
47
const stubOptions : IExportOptions = {
38
48
attachmentsIncluded : false ,
39
49
maxSize : 50000000 ,
@@ -43,10 +53,42 @@ describe("HTMLExport", () => {
43
53
44
54
expect ( exporter . destinationFileName ) . toMatchSnapshot ( ) ;
45
55
46
- jest . spyOn ( SdkConfig , "get" ) . mockImplementation ( ( ) => {
47
- return { brand : "BrandedChat/WithSlashes/ForFun" } ;
48
- } ) ;
56
+ SdkConfig . put ( { brand : "BrandedChat/WithSlashes/ForFun" } ) ;
49
57
50
58
expect ( exporter . destinationFileName ) . toMatchSnapshot ( ) ;
51
59
} ) ;
60
+
61
+ it ( "should export" , async ( ) => {
62
+ const room = new Room ( "!myroom:example.org" , client , "@me:example.org" ) ;
63
+
64
+ const events = [ ...Array ( 50 ) ] . map < IRoomEvent > ( ( _ , i ) => ( {
65
+ event_id : "$1" ,
66
+ type : EventType . RoomMessage ,
67
+ sender : `@user${ i } :example.com` ,
68
+ origin_server_ts : 5_000 + i * 1000 ,
69
+ content : {
70
+ msgtype : "m.text" ,
71
+ body : `Message #${ i } ` ,
72
+ } ,
73
+ } ) ) ;
74
+
75
+ client . getRoom . mockReturnValue ( room ) ;
76
+ client . createMessagesRequest . mockResolvedValue ( { chunk : events } ) ;
77
+
78
+ const exporter = new HTMLExporter (
79
+ room ,
80
+ ExportType . LastNMessages ,
81
+ {
82
+ attachmentsIncluded : false ,
83
+ maxSize : 1_024 * 1_024 ,
84
+ numberOfMessages : events . length ,
85
+ } ,
86
+ ( ) => { } ,
87
+ ) ;
88
+
89
+ await exporter . export ( ) ;
90
+
91
+ const file = getMessageFile ( exporter ) ;
92
+ expect ( await file . text ( ) ) . toMatchSnapshot ( ) ;
93
+ } ) ;
52
94
} ) ;
0 commit comments