@@ -6,6 +6,8 @@ import sinon from 'sinon';
6
6
import Upload from '../src' ;
7
7
import { UploadProps , RcFile } from '../src/interface' ;
8
8
9
+ const delay = ( timeout = 0 ) => new Promise ( resolve => setTimeout ( resolve , timeout ) ) ;
10
+
9
11
describe ( 'Upload.Batch' , ( ) => {
10
12
function getFile ( name : string ) : RcFile {
11
13
return {
@@ -64,60 +66,85 @@ describe('Upload.Batch', () => {
64
66
} , 10 ) ;
65
67
} ) ;
66
68
67
- it ( 'beforeUpload return false' , done => {
68
- const onBatchUpload = jest . fn ( ) ;
69
-
70
- const wrapper = mount (
71
- < Upload onBatchUpload = { onBatchUpload } beforeUpload = { ( ) => false } { ...genProps ( ) } /> ,
72
- ) ;
73
- triggerUpload ( wrapper ) ;
74
-
75
- setTimeout ( ( ) => {
76
- expect ( onBatchUpload ) . toHaveBeenCalledWith ( [
77
- expect . objectContaining ( firstFile ) ,
78
- expect . objectContaining ( secondFile ) ,
79
- ] ) ;
80
- done ( ) ;
81
- } , 10 ) ;
82
- } ) ;
83
-
84
- it ( 'beforeUpload return promise file' , done => {
85
- const onBatchUpload = jest . fn ( ) ;
86
-
87
- const wrapper = mount (
88
- < Upload
89
- onBatchUpload = { onBatchUpload }
90
- beforeUpload = { file => Promise . resolve ( file ) }
91
- { ...genProps ( ) }
92
- /> ,
93
- ) ;
94
- triggerUpload ( wrapper ) ;
95
-
96
- setTimeout ( ( ) => {
97
- expect ( onBatchUpload ) . toHaveBeenCalledWith ( [
98
- expect . objectContaining ( firstFile ) ,
99
- expect . objectContaining ( secondFile ) ,
100
- ] ) ;
101
- done ( ) ;
102
- } , 10 ) ;
103
- } ) ;
104
-
105
- it ( 'beforeUpload return promise rejection' , done => {
106
- const onBatchUpload = jest . fn ( ) ;
107
-
108
- const wrapper = mount (
109
- < Upload
110
- onBatchUpload = { onBatchUpload }
111
- beforeUpload = { ( ) => Promise . reject ( ) }
112
- { ...genProps ( ) }
113
- /> ,
114
- ) ;
115
- triggerUpload ( wrapper ) ;
116
-
117
- setTimeout ( ( ) => {
118
- expect ( onBatchUpload ) . toHaveBeenCalledWith ( [ ] ) ;
119
- done ( ) ;
120
- } , 10 ) ;
69
+ describe ( 'beforeUpload' , ( ) => {
70
+ it ( 'return false' , done => {
71
+ const onBatchUpload = jest . fn ( ) ;
72
+
73
+ const wrapper = mount (
74
+ < Upload onBatchUpload = { onBatchUpload } beforeUpload = { ( ) => false } { ...genProps ( ) } /> ,
75
+ ) ;
76
+ triggerUpload ( wrapper ) ;
77
+
78
+ setTimeout ( ( ) => {
79
+ expect ( onBatchUpload ) . toHaveBeenCalledWith ( [
80
+ expect . objectContaining ( firstFile ) ,
81
+ expect . objectContaining ( secondFile ) ,
82
+ ] ) ;
83
+ done ( ) ;
84
+ } , 10 ) ;
85
+ } ) ;
86
+
87
+ it ( 'return promise file' , done => {
88
+ const onBatchUpload = jest . fn ( ) ;
89
+
90
+ const wrapper = mount (
91
+ < Upload
92
+ onBatchUpload = { onBatchUpload }
93
+ beforeUpload = { file => Promise . resolve ( file ) }
94
+ { ...genProps ( ) }
95
+ /> ,
96
+ ) ;
97
+ triggerUpload ( wrapper ) ;
98
+
99
+ setTimeout ( ( ) => {
100
+ expect ( onBatchUpload ) . toHaveBeenCalledWith ( [
101
+ expect . objectContaining ( firstFile ) ,
102
+ expect . objectContaining ( secondFile ) ,
103
+ ] ) ;
104
+ done ( ) ;
105
+ } , 10 ) ;
106
+ } ) ;
107
+
108
+ it ( 'return promise rejection' , done => {
109
+ const onBatchUpload = jest . fn ( ) ;
110
+
111
+ const wrapper = mount (
112
+ < Upload
113
+ onBatchUpload = { onBatchUpload }
114
+ beforeUpload = { ( ) => Promise . reject ( ) }
115
+ { ...genProps ( ) }
116
+ /> ,
117
+ ) ;
118
+ triggerUpload ( wrapper ) ;
119
+
120
+ setTimeout ( ( ) => {
121
+ expect ( onBatchUpload ) . toHaveBeenCalledWith ( [ ] ) ;
122
+ done ( ) ;
123
+ } , 10 ) ;
124
+ } ) ;
125
+
126
+ it ( 'beforeUpload delay for the first' , done => {
127
+ const onBatchUpload = jest . fn ( ) ;
128
+
129
+ const wrapper = mount (
130
+ < Upload
131
+ onBatchUpload = { onBatchUpload }
132
+ beforeUpload = { async file => {
133
+ if ( file === firstFile ) {
134
+ await delay ( 100 ) ;
135
+ }
136
+ return file ;
137
+ } }
138
+ { ...genProps ( ) }
139
+ /> ,
140
+ ) ;
141
+ triggerUpload ( wrapper ) ;
142
+
143
+ setTimeout ( ( ) => {
144
+ expect ( onBatchUpload ) . toHaveBeenCalledWith ( [ ] ) ;
145
+ done ( ) ;
146
+ } , 1000 ) ;
147
+ } ) ;
121
148
} ) ;
122
149
} ) ;
123
150
} ) ;
0 commit comments