@@ -3,7 +3,7 @@ import { assertion, renderer, wrap } from '@dojo/framework/testing/renderer';
33import { Button } from '../../../button' ;
44import { FileUploadInput } from '../../index' ;
55import { Label } from '../../../label' ;
6- import { noop } from '../../../common/tests/support/test-helpers' ;
6+ import { noop , stubEvent } from '../../../common/tests/support/test-helpers' ;
77
88import bundle from '../../nls/FileUploadInput' ;
99import * as baseCss from '../../../theme/default/base.m.css' ;
@@ -13,15 +13,19 @@ import * as fixedCss from '../../styles/file-upload-input.m.css';
1313import * as labelCss from '../../../theme/default/label.m.css' ;
1414
1515const { it, describe } = intern . getInterface ( 'bdd' ) ;
16+ const { assert } = intern . getPlugin ( 'chai' ) ;
1617const { messages } = bundle ;
1718
1819describe ( 'FileUploadInput' , function ( ) {
1920 const WrappedRoot = wrap ( 'div' ) ;
2021 const WrappedWrapper = wrap ( 'div' ) ;
22+ const WrappedInput = wrap ( 'input' ) ;
23+ const WrappedButton = wrap ( Button ) ;
2124 const WrappedLabel = wrap ( 'span' ) ;
2225
2326 const baseRootProperties = {
2427 key : 'root' ,
28+ 'aria-disabled' : false ,
2529 classes : [ null , fixedCss . root , css . root , false , false ] ,
2630 ondragenter : noop ,
2731 ondragover : noop ,
@@ -32,10 +36,10 @@ describe('FileUploadInput', function() {
3236 return (
3337 < WrappedRoot { ...baseRootProperties } >
3438 < WrappedWrapper classes = { [ css . wrapper ] } >
35- < input
39+ < WrappedInput
3640 key = "nativeInput"
3741 accept = { undefined }
38- aria = " hidden"
42+ aria- hidden = { true }
3943 classes = { [ baseCss . hidden ] }
4044 click = { noop }
4145 disabled = { false }
@@ -45,7 +49,7 @@ describe('FileUploadInput', function() {
4549 required = { false }
4650 type = "file"
4751 />
48- < Button
52+ < WrappedButton
4953 disabled = { false }
5054 onClick = { noop }
5155 theme = { {
@@ -59,7 +63,7 @@ describe('FileUploadInput', function() {
5963 } }
6064 >
6165 { messages . chooseFiles }
62- </ Button >
66+ </ WrappedButton >
6367
6468 < WrappedLabel classes = { [ css . dndLabel ] } > { messages . orDropFilesHere } </ WrappedLabel >
6569 </ WrappedWrapper >
@@ -149,14 +153,34 @@ describe('FileUploadInput', function() {
149153 ) ;
150154 } ) ;
151155
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+
152176 it ( 'handles dragenter, dragleave, and the overlay' , function ( ) {
153177 const r = renderer ( function ( ) {
154178 return < FileUploadInput /> ;
155179 } ) ;
156180 const WrappedOverlay = wrap ( 'div' ) ;
157181
158182 r . expect ( baseAssertion ) ;
159- r . property ( WrappedRoot , 'ondragenter' , { preventDefault : noop } ) ;
183+ r . property ( WrappedRoot , 'ondragenter' , stubEvent ) ;
160184
161185 r . expect (
162186 baseAssertion
@@ -178,9 +202,88 @@ describe('FileUploadInput', function() {
178202 } )
179203 ) ;
180204
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);
184207 // r.expect(baseAssertion);
185208 } ) ;
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+ } ) ;
186289} ) ;
0 commit comments