@@ -3,7 +3,7 @@ import { assertion, renderer, wrap } from '@dojo/framework/testing/renderer';
3
3
import { Button } from '../../../button' ;
4
4
import { FileUploadInput } from '../../index' ;
5
5
import { Label } from '../../../label' ;
6
- import { noop } from '../../../common/tests/support/test-helpers' ;
6
+ import { noop , stubEvent } from '../../../common/tests/support/test-helpers' ;
7
7
8
8
import bundle from '../../nls/FileUploadInput' ;
9
9
import * as baseCss from '../../../theme/default/base.m.css' ;
@@ -13,15 +13,19 @@ import * as fixedCss from '../../styles/file-upload-input.m.css';
13
13
import * as labelCss from '../../../theme/default/label.m.css' ;
14
14
15
15
const { it, describe } = intern . getInterface ( 'bdd' ) ;
16
+ const { assert } = intern . getPlugin ( 'chai' ) ;
16
17
const { messages } = bundle ;
17
18
18
19
describe ( 'FileUploadInput' , function ( ) {
19
20
const WrappedRoot = wrap ( 'div' ) ;
20
21
const WrappedWrapper = wrap ( 'div' ) ;
22
+ const WrappedInput = wrap ( 'input' ) ;
23
+ const WrappedButton = wrap ( Button ) ;
21
24
const WrappedLabel = wrap ( 'span' ) ;
22
25
23
26
const baseRootProperties = {
24
27
key : 'root' ,
28
+ 'aria-disabled' : false ,
25
29
classes : [ null , fixedCss . root , css . root , false , false ] ,
26
30
ondragenter : noop ,
27
31
ondragover : noop ,
@@ -32,10 +36,10 @@ describe('FileUploadInput', function() {
32
36
return (
33
37
< WrappedRoot { ...baseRootProperties } >
34
38
< WrappedWrapper classes = { [ css . wrapper ] } >
35
- < input
39
+ < WrappedInput
36
40
key = "nativeInput"
37
41
accept = { undefined }
38
- aria = " hidden"
42
+ aria- hidden = { true }
39
43
classes = { [ baseCss . hidden ] }
40
44
click = { noop }
41
45
disabled = { false }
@@ -45,7 +49,7 @@ describe('FileUploadInput', function() {
45
49
required = { false }
46
50
type = "file"
47
51
/>
48
- < Button
52
+ < WrappedButton
49
53
disabled = { false }
50
54
onClick = { noop }
51
55
theme = { {
@@ -59,7 +63,7 @@ describe('FileUploadInput', function() {
59
63
} }
60
64
>
61
65
{ messages . chooseFiles }
62
- </ Button >
66
+ </ WrappedButton >
63
67
64
68
< WrappedLabel classes = { [ css . dndLabel ] } > { messages . orDropFilesHere } </ WrappedLabel >
65
69
</ WrappedWrapper >
@@ -149,14 +153,34 @@ describe('FileUploadInput', function() {
149
153
) ;
150
154
} ) ;
151
155
156
+ it ( 'renders disabled' , function ( ) {
157
+ const r = renderer ( function ( ) {
158
+ return < FileUploadInput disabled = { true } /> ;
159
+ } ) ;
160
+
161
+ r . expect (
162
+ baseAssertion
163
+ . setProperty ( WrappedRoot , 'aria-disabled' , true )
164
+ . setProperty ( WrappedRoot , 'classes' , [
165
+ null ,
166
+ fixedCss . root ,
167
+ css . root ,
168
+ false ,
169
+ css . disabled
170
+ ] )
171
+ . setProperty ( WrappedInput , 'disabled' , true )
172
+ . setProperty ( WrappedButton , 'disabled' , true )
173
+ ) ;
174
+ } ) ;
175
+
152
176
it ( 'handles dragenter, dragleave, and the overlay' , function ( ) {
153
177
const r = renderer ( function ( ) {
154
178
return < FileUploadInput /> ;
155
179
} ) ;
156
180
const WrappedOverlay = wrap ( 'div' ) ;
157
181
158
182
r . expect ( baseAssertion ) ;
159
- r . property ( WrappedRoot , 'ondragenter' , { preventDefault : noop } ) ;
183
+ r . property ( WrappedRoot , 'ondragenter' , stubEvent ) ;
160
184
161
185
r . expect (
162
186
baseAssertion
@@ -178,9 +202,88 @@ describe('FileUploadInput', function() {
178
202
} )
179
203
) ;
180
204
181
- // TODO: enable when testing bug is fixed
182
- // https://github.com/dojo/framework/issues/839
183
- // r.property(WrappedOverlay, 'ondragleave');
205
+ // TODO: enable when https://github.com/dojo/framework/pull/840 is merged
206
+ // r.property(WrappedOverlay, 'ondragleave', stubEvent);
184
207
// r.expect(baseAssertion);
185
208
} ) ;
209
+
210
+ it ( 'handles file drop event' , function ( ) {
211
+ const testValues = [ 1 , 2 , 3 ] ;
212
+ let receivedFiles : number [ ] = [ ] ;
213
+
214
+ function onValue ( value : any [ ] ) {
215
+ receivedFiles = value ;
216
+ }
217
+
218
+ const r = renderer ( function ( ) {
219
+ return < FileUploadInput onValue = { onValue } /> ;
220
+ } ) ;
221
+
222
+ r . expect ( baseAssertion ) ;
223
+ r . property ( WrappedRoot , 'ondrop' , {
224
+ preventDefault : noop ,
225
+ dataTransfer : {
226
+ files : testValues
227
+ }
228
+ } ) ;
229
+ r . expect ( baseAssertion ) ;
230
+
231
+ assert . sameOrderedMembers ( receivedFiles , testValues ) ;
232
+ } ) ;
233
+
234
+ it ( 'validates files based on "accept"' , function ( ) {
235
+ const accept = 'image/jpeg,image/*,.gif' ;
236
+ const testFiles = [
237
+ { name : 'file1.jpg' , type : 'image/jpeg' } , // test direct match: image/jpeg
238
+ { name : 'file2.png' , type : 'image/png' } , // test wildcard match: image/*
239
+ { name : 'file3.gif' , type : 'bad/type' } , // test extension match: .gif
240
+ { name : 'file4.doc' , type : 'application/word' } // test match failure
241
+ ] ;
242
+ const validFiles = testFiles . slice ( 0 , 3 ) ;
243
+ let receivedFiles : Array < typeof testFiles [ 0 ] > = [ ] ;
244
+
245
+ function onValue ( value : any [ ] ) {
246
+ receivedFiles = value ;
247
+ }
248
+
249
+ const r = renderer ( function ( ) {
250
+ return < FileUploadInput onValue = { onValue } accept = { accept } /> ;
251
+ } ) ;
252
+ const acceptAssertion = baseAssertion . setProperty ( WrappedInput , 'accept' , accept ) ;
253
+
254
+ r . expect ( acceptAssertion ) ;
255
+ r . property ( WrappedRoot , 'ondrop' , {
256
+ preventDefault : noop ,
257
+ dataTransfer : {
258
+ files : testFiles
259
+ }
260
+ } ) ;
261
+ r . expect ( acceptAssertion ) ;
262
+
263
+ assert . sameOrderedMembers ( receivedFiles , validFiles ) ;
264
+ } ) ;
265
+
266
+ it ( 'calls onValue when files are selected from input' , function ( ) {
267
+ const testValues = [ 1 , 2 , 3 ] ;
268
+ let receivedFiles : number [ ] = [ ] ;
269
+
270
+ function onValue ( value : any [ ] ) {
271
+ receivedFiles = value ;
272
+ }
273
+
274
+ const r = renderer ( function ( ) {
275
+ return < FileUploadInput onValue = { onValue } /> ;
276
+ } ) ;
277
+
278
+ r . expect ( baseAssertion ) ;
279
+ r . property ( WrappedInput , 'onchange' , {
280
+ target : {
281
+ files : testValues
282
+ }
283
+ } ) ;
284
+ // TODO: the queued onchange is not triggering because it is for a node with a different id than expected
285
+ r . expect ( baseAssertion ) ;
286
+
287
+ assert . sameOrderedMembers ( receivedFiles , testValues ) ;
288
+ } ) ;
186
289
} ) ;
0 commit comments