@@ -16,22 +16,40 @@ limitations under the License.
16
16
17
17
import React from 'react' ;
18
18
import { mount } from 'enzyme' ;
19
+ import { mocked } from 'ts-jest/utils' ;
19
20
import '../../../skinned-sdk' ;
20
21
import { act } from "react-dom/test-utils" ;
21
22
import { Room } from 'matrix-js-sdk' ;
22
23
23
- import ExportDialog from '../../../../src/components/views/dialogs/ExportDialog' ;
24
+ import ExportDialog ,
25
+ { getSafeForceRoomExportSettings , ForceRoomExportSettings }
26
+ from '../../../../src/components/views/dialogs/ExportDialog' ;
24
27
import { ExportType , ExportFormat } from '../../../../src/utils/exportUtils/exportUtils' ;
25
28
import { createTestClient , mkStubRoom } from '../../../test-utils' ;
26
29
import { MatrixClientPeg } from '../../../../src/MatrixClientPeg' ;
27
30
import HTMLExporter from "../../../../src/utils/exportUtils/HtmlExport" ;
31
+ import SettingsStore from '../../../../src/settings/SettingsStore' ;
32
+ import PlainTextExporter from '../../../../src/utils/exportUtils/PlainTextExport' ;
28
33
29
34
jest . useFakeTimers ( ) ;
30
35
31
- const mockHtmlExporter = ( {
36
+ const htmlExporterInstance = ( {
37
+ export : jest . fn ( ) . mockResolvedValue ( { } ) ,
38
+ } ) ;
39
+ const plainTextExporterInstance = ( {
32
40
export : jest . fn ( ) . mockResolvedValue ( { } ) ,
33
41
} ) ;
34
42
jest . mock ( "../../../../src/utils/exportUtils/HtmlExport" , ( ) => jest . fn ( ) ) ;
43
+ jest . mock ( "../../../../src/utils/exportUtils/PlainTextExport" , ( ) => jest . fn ( ) ) ;
44
+
45
+ jest . mock ( '../../../../src/settings/SettingsStore' , ( ) => ( {
46
+ monitorSetting : jest . fn ( ) ,
47
+ getValue : jest . fn ( ) ,
48
+ } ) ) ;
49
+
50
+ const SettingsStoreMock = mocked ( SettingsStore ) ;
51
+ const HTMLExporterMock = mocked ( HTMLExporter ) ;
52
+ const PlainTextExporterMock = mocked ( PlainTextExporter ) ;
35
53
36
54
describe ( '<ExportDialog />' , ( ) => {
37
55
const mockClient = createTestClient ( ) ;
@@ -81,8 +99,13 @@ describe('<ExportDialog />', () => {
81
99
} ) ;
82
100
83
101
beforeEach ( ( ) => {
84
- ( HTMLExporter as jest . Mock ) . mockImplementation ( jest . fn ( ) . mockReturnValue ( mockHtmlExporter ) ) ;
85
- mockHtmlExporter . export . mockClear ( ) ;
102
+ HTMLExporterMock . mockClear ( ) . mockImplementation ( jest . fn ( ) . mockReturnValue ( htmlExporterInstance ) ) ;
103
+ PlainTextExporterMock . mockClear ( ) . mockImplementation ( jest . fn ( ) . mockReturnValue ( plainTextExporterInstance ) ) ;
104
+ htmlExporterInstance . export . mockClear ( ) ;
105
+ plainTextExporterInstance . export . mockClear ( ) ;
106
+
107
+ // default setting value
108
+ SettingsStoreMock . getValue . mockClear ( ) . mockReturnValue ( { } ) ;
86
109
} ) ;
87
110
88
111
it ( 'renders export dialog' , ( ) => {
@@ -104,7 +127,7 @@ describe('<ExportDialog />', () => {
104
127
await submitForm ( component ) ;
105
128
106
129
// 4th arg is an component function
107
- const exportConstructorProps = ( HTMLExporter as jest . Mock ) . mock . calls [ 0 ] . slice ( 0 , 3 ) ;
130
+ const exportConstructorProps = HTMLExporterMock . mock . calls [ 0 ] . slice ( 0 , 3 ) ;
108
131
expect ( exportConstructorProps ) . toEqual ( [
109
132
defaultProps . room ,
110
133
ExportType . Timeline ,
@@ -114,7 +137,32 @@ describe('<ExportDialog />', () => {
114
137
numberOfMessages : 100 ,
115
138
} ,
116
139
] ) ;
117
- expect ( mockHtmlExporter . export ) . toHaveBeenCalled ( ) ;
140
+ expect ( htmlExporterInstance . export ) . toHaveBeenCalled ( ) ;
141
+ } ) ;
142
+
143
+ it ( 'exports room using values set from ForceRoomExportSettings' , async ( ) => {
144
+ SettingsStoreMock . getValue . mockReturnValue ( {
145
+ format : ExportFormat . PlainText ,
146
+ range : ExportType . Beginning ,
147
+ sizeMb : 15000 ,
148
+ numberOfMessages : 30 ,
149
+ attachmentsIncluded : true ,
150
+ } ) ;
151
+ const component = getComponent ( ) ;
152
+ await submitForm ( component ) ;
153
+
154
+ // 4th arg is an component function
155
+ const exportConstructorProps = PlainTextExporterMock . mock . calls [ 0 ] . slice ( 0 , 3 ) ;
156
+ expect ( exportConstructorProps ) . toEqual ( [
157
+ defaultProps . room ,
158
+ ExportType . Beginning ,
159
+ {
160
+ attachmentsIncluded : false ,
161
+ maxSize : 15000 * 1024 * 1024 ,
162
+ numberOfMessages : 30 ,
163
+ } ,
164
+ ] ) ;
165
+ expect ( plainTextExporterInstance . export ) . toHaveBeenCalled ( ) ;
118
166
} ) ;
119
167
120
168
it ( 'renders success screen when export is finished' , async ( ) => {
@@ -139,6 +187,19 @@ describe('<ExportDialog />', () => {
139
187
expect ( getExportFormatInput ( component , ExportFormat . PlainText ) . props ( ) . checked ) . toBeTruthy ( ) ;
140
188
expect ( getExportFormatInput ( component , ExportFormat . Html ) . props ( ) . checked ) . toBeFalsy ( ) ;
141
189
} ) ;
190
+
191
+ it ( 'hides export format input when format is valid in ForceRoomExportSettings' , ( ) => {
192
+ const component = getComponent ( ) ;
193
+ expect ( getExportFormatInput ( component , ExportFormat . Html ) . props ( ) . checked ) . toBeTruthy ( ) ;
194
+ } ) ;
195
+
196
+ it ( 'does not render export format when set in ForceRoomExportSettings' , ( ) => {
197
+ SettingsStoreMock . getValue . mockReturnValue ( {
198
+ format : ExportFormat . PlainText ,
199
+ } ) ;
200
+ const component = getComponent ( ) ;
201
+ expect ( getExportFormatInput ( component , ExportFormat . Html ) . length ) . toBeFalsy ( ) ;
202
+ } ) ;
142
203
} ) ;
143
204
144
205
describe ( 'export type' , ( ) => {
@@ -153,6 +214,14 @@ describe('<ExportDialog />', () => {
153
214
expect ( getExportTypeInput ( component ) . props ( ) . value ) . toEqual ( ExportType . Beginning ) ;
154
215
} ) ;
155
216
217
+ it ( 'does not render export type when set in ForceRoomExportSettings' , ( ) => {
218
+ SettingsStoreMock . getValue . mockReturnValue ( {
219
+ range : ExportType . Beginning ,
220
+ } ) ;
221
+ const component = getComponent ( ) ;
222
+ expect ( getExportTypeInput ( component ) . length ) . toBeFalsy ( ) ;
223
+ } ) ;
224
+
156
225
it ( 'does not render message count input' , async ( ) => {
157
226
const component = getComponent ( ) ;
158
227
expect ( getMessageCountInput ( component ) . length ) . toBeFalsy ( ) ;
@@ -177,7 +246,7 @@ describe('<ExportDialog />', () => {
177
246
await setMessageCount ( component , 0 ) ;
178
247
await submitForm ( component ) ;
179
248
180
- expect ( mockHtmlExporter . export ) . not . toHaveBeenCalled ( ) ;
249
+ expect ( htmlExporterInstance . export ) . not . toHaveBeenCalled ( ) ;
181
250
} ) ;
182
251
183
252
it ( 'does not export when export type is lastNMessages and message count is more than max' , async ( ) => {
@@ -186,7 +255,7 @@ describe('<ExportDialog />', () => {
186
255
await setMessageCount ( component , 99999999999 ) ;
187
256
await submitForm ( component ) ;
188
257
189
- expect ( mockHtmlExporter . export ) . not . toHaveBeenCalled ( ) ;
258
+ expect ( htmlExporterInstance . export ) . not . toHaveBeenCalled ( ) ;
190
259
} ) ;
191
260
192
261
it ( 'exports when export type is NOT lastNMessages and message count is falsy' , async ( ) => {
@@ -196,7 +265,7 @@ describe('<ExportDialog />', () => {
196
265
await selectExportType ( component , ExportType . Timeline ) ;
197
266
await submitForm ( component ) ;
198
267
199
- expect ( mockHtmlExporter . export ) . toHaveBeenCalled ( ) ;
268
+ expect ( htmlExporterInstance . export ) . toHaveBeenCalled ( ) ;
200
269
} ) ;
201
270
} ) ;
202
271
@@ -217,27 +286,48 @@ describe('<ExportDialog />', () => {
217
286
await setSizeLimit ( component , 0 ) ;
218
287
await submitForm ( component ) ;
219
288
220
- expect ( mockHtmlExporter . export ) . not . toHaveBeenCalled ( ) ;
289
+ expect ( htmlExporterInstance . export ) . not . toHaveBeenCalled ( ) ;
221
290
} ) ;
222
291
223
292
it ( 'does not export when size limit is larger than max' , async ( ) => {
224
293
const component = getComponent ( ) ;
225
294
await setSizeLimit ( component , 2001 ) ;
226
295
await submitForm ( component ) ;
227
296
228
- expect ( mockHtmlExporter . export ) . not . toHaveBeenCalled ( ) ;
297
+ expect ( htmlExporterInstance . export ) . not . toHaveBeenCalled ( ) ;
229
298
} ) ;
230
299
231
300
it ( 'exports when size limit is max' , async ( ) => {
232
301
const component = getComponent ( ) ;
233
302
await setSizeLimit ( component , 2000 ) ;
234
303
await submitForm ( component ) ;
235
304
236
- expect ( mockHtmlExporter . export ) . toHaveBeenCalled ( ) ;
305
+ expect ( htmlExporterInstance . export ) . toHaveBeenCalled ( ) ;
306
+ } ) ;
307
+
308
+ it ( 'does not render size limit input when set in ForceRoomExportSettings' , ( ) => {
309
+ SettingsStoreMock . getValue . mockReturnValue ( {
310
+ sizeMb : 10000 ,
311
+ } ) ;
312
+ const component = getComponent ( ) ;
313
+ expect ( getSizeInput ( component ) . length ) . toBeFalsy ( ) ;
314
+ } ) ;
315
+
316
+ /**
317
+ * 2000mb size limit does not apply when higher limit is configured in config
318
+ */
319
+ it ( 'exports when size limit set in ForceRoomExportSettings is larger than 2000' , async ( ) => {
320
+ SettingsStoreMock . getValue . mockReturnValue ( {
321
+ sizeMb : 10000 ,
322
+ } ) ;
323
+ const component = getComponent ( ) ;
324
+ await submitForm ( component ) ;
325
+
326
+ expect ( htmlExporterInstance . export ) . toHaveBeenCalled ( ) ;
237
327
} ) ;
238
328
} ) ;
239
329
240
- describe ( 'include attachements ' , ( ) => {
330
+ describe ( 'include attachments ' , ( ) => {
241
331
it ( 'renders input with default value of false' , ( ) => {
242
332
const component = getComponent ( ) ;
243
333
expect ( getAttachmentsCheckbox ( component ) . props ( ) . checked ) . toEqual ( false ) ;
@@ -248,6 +338,43 @@ describe('<ExportDialog />', () => {
248
338
await setIncludeAttachments ( component , true ) ;
249
339
expect ( getAttachmentsCheckbox ( component ) . props ( ) . checked ) . toEqual ( true ) ;
250
340
} ) ;
341
+
342
+ it ( 'does not render input when set in ForceRoomExportSettings' , ( ) => {
343
+ SettingsStoreMock . getValue . mockReturnValue ( {
344
+ includeAttachments : false ,
345
+ } ) ;
346
+ const component = getComponent ( ) ;
347
+ expect ( getAttachmentsCheckbox ( component ) . length ) . toBeFalsy ( ) ;
348
+ } ) ;
349
+ } ) ;
350
+
351
+ describe ( 'getSafeForceRoomExportSettings()' , ( ) => {
352
+ const testCases : [ string , ForceRoomExportSettings , ForceRoomExportSettings ] [ ] = [
353
+ [ 'setting is falsy' , undefined , { } ] ,
354
+ [ 'setting is configured to string' , 'test' as unknown , { } ] ,
355
+ [ 'setting is empty' , { } , { } ] ,
356
+ [ 'format is not a valid ExportFormat' , { format : 'mp3' } , { } ] ,
357
+ [ 'format is a valid ExportFormat' , { format : ExportFormat . Html } , { format : ExportFormat . Html } ] ,
358
+ [ 'range is not a valid ExportType' , { range : 'yesterday' } , { } ] ,
359
+ [ 'range is a valid ExportType' , { range : ExportType . LastNMessages } , { range : ExportType . LastNMessages } ] ,
360
+ [ 'numberOfMessages is not a number' , { numberOfMessages : 'test' } , { } ] ,
361
+ [ 'numberOfMessages is less than 1' , { numberOfMessages : - 1 } , { } ] ,
362
+ [ 'numberOfMessages is more than 100000000' , { numberOfMessages : 9999999999 } , { } ] ,
363
+ [ 'numberOfMessages is valid' , { numberOfMessages : 2000 } , { numberOfMessages : 2000 } ] ,
364
+ [ 'sizeMb is not a number' , { sizeMb : 'test' } , { } ] ,
365
+ [ 'sizeMb is less than 1' , { sizeMb : - 1 } , { } ] ,
366
+ [ 'sizeMb is more than 1024000' , { sizeMb : Number . MAX_SAFE_INTEGER } , { } ] ,
367
+ [ 'sizeMb is valid' , { sizeMb : 50000 } , { sizeMb : 50000 } ] ,
368
+ [ 'includeAttachments is not a boolean' , { includeAttachments : 'yes' } , { } ] ,
369
+ [ 'includeAttachments is true' , { includeAttachments : true } , { includeAttachments : true } ] ,
370
+ [ 'includeAttachments is false' , { includeAttachments : false } , { includeAttachments : false } ] ,
371
+ ] ;
372
+
373
+ it . each ( testCases ) ( 'sanitizes correctly when %s' , ( _d , setting , expected ) => {
374
+ SettingsStoreMock . getValue . mockReturnValue ( setting ) ;
375
+
376
+ expect ( getSafeForceRoomExportSettings ( ) ) . toEqual ( expected ) ;
377
+ } ) ;
251
378
} ) ;
252
379
} ) ;
253
380
0 commit comments