1
1
import React from 'react' ;
2
2
import { shallow } from 'enzyme' ;
3
3
4
+ import { downloadAllLimit , downloadSingleLimit } from 'data/constants/files' ;
5
+
4
6
import { formatMessage } from 'testUtils' ;
5
7
import { SubmissionFiles } from './SubmissionFiles' ;
8
+ import messages from './messages' ;
6
9
7
10
jest . mock ( './components/FileNameCell' , ( ) => jest . fn ( ) . mockName ( 'FileNameCell' ) ) ;
8
11
jest . mock ( './components/FileExtensionCell' , ( ) => jest . fn ( ) . mockName ( 'FileExtensionCell' ) ) ;
@@ -16,25 +19,34 @@ describe('SubmissionFiles', () => {
16
19
name : 'some file name.jpg' ,
17
20
description : 'description for the file' ,
18
21
downloadURL : '/valid-url-wink-wink' ,
22
+ size : 0 ,
19
23
} ,
20
24
{
21
25
name : 'file number 2.jpg' ,
22
26
description : 'description for this file' ,
23
27
downloadURL : '/url-2' ,
28
+ size : 0 ,
24
29
} ,
25
30
] ,
26
31
} ;
27
32
let el ;
28
- beforeAll ( ( ) => {
29
- el = shallow ( < SubmissionFiles intl = { { formatMessage } } /> ) ;
33
+ beforeEach ( ( ) => {
34
+ el = shallow ( < SubmissionFiles intl = { { formatMessage } } { ... props } /> ) ;
30
35
} ) ;
31
36
32
37
describe ( 'snapshot' , ( ) => {
38
+ test ( 'files existed for props' , ( ) => {
39
+ expect ( el ) . toMatchSnapshot ( ) ;
40
+ } ) ;
41
+
33
42
test ( 'files does not exist' , ( ) => {
43
+ el . setProps ( { files : [ ] } ) ;
44
+
34
45
expect ( el ) . toMatchSnapshot ( ) ;
35
46
} ) ;
36
- test ( 'files exited for props' , ( ) => {
37
- el . setProps ( { ...props } ) ;
47
+ test ( 'files size exceed' , ( ) => {
48
+ const files = props . files . map ( file => ( { ...file , size : downloadSingleLimit + 1 } ) ) ;
49
+ el . setProps ( { files } ) ;
38
50
expect ( el ) . toMatchSnapshot ( ) ;
39
51
} ) ;
40
52
} ) ;
@@ -43,12 +55,47 @@ describe('SubmissionFiles', () => {
43
55
test ( 'title' , ( ) => {
44
56
const titleEl = el . find ( '.submission-files-title>h3' ) ;
45
57
expect ( titleEl . text ( ) ) . toEqual (
46
- `Submission Files (${ props . files . length } )` ,
58
+ `${ formatMessage ( messages . submissionFiles ) } (${ props . files . length } )` ,
47
59
) ;
48
60
expect ( el . instance ( ) . title ) . toEqual (
49
- `Submission Files (${ props . files . length } )` ,
61
+ `${ formatMessage ( messages . submissionFiles ) } (${ props . files . length } )` ,
50
62
) ;
51
63
} ) ;
64
+
65
+ describe ( 'canDownload' , ( ) => {
66
+ test ( 'normal file size' , ( ) => {
67
+ expect ( el . instance ( ) . canDownload ) . toEqual ( true ) ;
68
+ } ) ;
69
+
70
+ test ( 'one of the file exceed the limit' , ( ) => {
71
+ const oneFileExceed = [ { ...props . files [ 0 ] , size : downloadSingleLimit + 1 } , props . files [ 1 ] ] ;
72
+
73
+ oneFileExceed . forEach ( file => expect ( file . size < downloadAllLimit ) . toEqual ( true ) ) ;
74
+
75
+ el . setProps ( { files : oneFileExceed } ) ;
76
+ expect ( el . instance ( ) . canDownload ) . toEqual ( false ) ;
77
+
78
+ const warningEl = el . find ( 'span.exceed-download-text' ) ;
79
+ expect ( warningEl . text ( ) . trim ( ) ) . toEqual ( formatMessage ( messages . exceedFileSize ) ) ;
80
+ } ) ;
81
+
82
+ test ( 'total file size exceed the limit' , ( ) => {
83
+ const length = 20 ;
84
+ const totalFilesExceed = new Array ( length ) . fill ( {
85
+ name : 'some file name.jpg' ,
86
+ description : 'description for the file' ,
87
+ downloadURL : '/valid-url-wink-wink' ,
88
+ size : ( downloadAllLimit + 1 ) / length ,
89
+ } ) ;
90
+ totalFilesExceed . forEach ( file => {
91
+ expect ( file . size < downloadAllLimit ) . toEqual ( true ) ;
92
+ expect ( file . size < downloadSingleLimit ) . toEqual ( true ) ;
93
+ } ) ;
94
+
95
+ el . setProps ( { files : totalFilesExceed } ) ;
96
+ expect ( el . instance ( ) . canDownload ) . toEqual ( false ) ;
97
+ } ) ;
98
+ } ) ;
52
99
} ) ;
53
100
} ) ;
54
101
} ) ;
0 commit comments