11import { expect } from '@esm-bundle/chai' ;
2- import { fixtureSync , isFirefox } from '@vaadin/testing-helpers' ;
2+ import { change , click , fixtureSync , makeSoloTouchEvent , touchend } from '@vaadin/testing-helpers' ;
33import sinon from 'sinon' ;
44import '../vaadin-upload.js' ;
55import { createFile , createFiles , touchDevice , xhrCreator } from './common.js' ;
@@ -27,76 +27,66 @@ describe('file list', () => {
2727
2828 it ( 'should notify files property changes' , ( ) => {
2929 const spy = sinon . spy ( ) ;
30- // when setting arrays Polymer mutation observer works differently
3130 upload . addEventListener ( 'files-changed' , spy ) ;
3231 upload . files = files ;
3332 expect ( spy . calledOnce ) . to . be . true ;
3433 } ) ;
3534 } ) ;
3635
37- ( isFirefox ? describe . skip : describe ) ( 'with add button' , ( ) => {
36+ describe ( 'with add button' , ( ) => {
37+ let input ;
38+ let addFiles ;
39+ let inputClickSpy ;
40+
41+ beforeEach ( ( ) => {
42+ addFiles = upload . $ . addFiles ;
43+ input = upload . $ . fileInput ;
44+ // While the synthetic "Add Files" button click event is not trusted and
45+ // it should generate a non-trusted click event on the hidden file input,
46+ // at the time of writing Chrome and Firefox still open the file dialog.
47+ // Use stub calling `preventDefault` to prevent dialog from opening.
48+ inputClickSpy = sinon . stub ( ) . callsFake ( ( e ) => e . preventDefault ( ) ) ;
49+ input . addEventListener ( 'click' , inputClickSpy ) ;
50+ } ) ;
51+
3852 it ( 'should open file dialog by click' , ( ) => {
39- // This test checks if the 'click' event is synchronously dispatched
40- // on the hidden file input when user clicks Add Files button. The
41- // file dialog is actually not getting opened during testing.
42- //
43- // The mock Add Files button click event fired by this test is not
44- // trusted and hence it should generate a non-trusted click event on
45- // the hidden file input. For security reasons non-IE browsers don't
46- // open file dialog when file input click is non-trusted.
47- const clickSpy = sinon . spy ( ) ;
48- upload . $ . fileInput . addEventListener ( 'click' , clickSpy ) ;
49-
50- upload . $ . addFiles . dispatchEvent ( new MouseEvent ( 'click' ) ) ;
51- expect ( clickSpy . calledOnce ) . to . be . true ;
53+ click ( addFiles ) ;
54+ expect ( inputClickSpy . calledOnce ) . to . be . true ;
5255 } ) ;
5356
5457 it ( 'should open file dialog by touchend' , ( ) => {
55- const clickSpy = sinon . spy ( ) ;
56- upload . $ . fileInput . addEventListener ( 'click' , clickSpy ) ;
57-
58- const e = new CustomEvent ( 'touchend' , { cancelable : true } ) ;
59- upload . $ . addFiles . dispatchEvent ( e ) ;
60- expect ( clickSpy . calledOnce ) . to . be . true ;
61- expect ( e . defaultPrevented ) . to . be . true ;
58+ const event = makeSoloTouchEvent ( 'touchend' , null , addFiles ) ;
59+ expect ( inputClickSpy . calledOnce ) . to . be . true ;
60+ expect ( event . defaultPrevented ) . to . be . true ;
6261 } ) ;
6362
6463 it ( 'should invoke Polymer.Gestures.resetMouseCanceller before open file dialog' , ( ) => {
6564 // NOTE(platosha): With ES modules we can’t put a spy on the actual
6665 // Polymer.Gestures.resetMouseCanceller. Have to use a separate
6766 // wrapper method for testing.
6867 const spy = sinon . spy ( upload , '__resetMouseCanceller' ) ;
69-
70- const e = new CustomEvent ( 'touchend' , { cancelable : true } ) ;
71- upload . $ . addFiles . dispatchEvent ( e ) ;
68+ touchend ( addFiles ) ;
7269 expect ( spy . calledOnce ) . to . be . true ;
7370 } ) ;
7471
75- it ( 'should reset input.value before dialog' , ( done ) => {
76- const input = upload . $ . fileInput ;
72+ it ( 'should reset input.value before dialog' , ( ) => {
7773 // We can't simply assign `files` property of input[type="file"].
7874 // Tweaking __proto__ to make it assignable below.
7975 input . __proto__ = HTMLElement . prototype ;
8076 delete input . value ;
8177 input . value = 'foo' ;
8278
83- input . addEventListener ( 'click' , ( ) => {
84- expect ( input . value ) . to . be . empty ;
85- done ( ) ;
86- } ) ;
87-
88- upload . $ . addFiles . dispatchEvent ( new MouseEvent ( 'click' ) ) ;
79+ click ( addFiles ) ;
80+ expect ( input . value ) . to . be . empty ;
8981 } ) ;
9082
9183 it ( 'should add files from dialog' , ( ) => {
9284 // We can't simply assign `files` property of input[type="file"].
9385 // Tweaking __proto__ to make it assignable below.
94- upload . $ . fileInput . __proto__ = HTMLElement . prototype ;
95- upload . $ . fileInput . files = files ;
86+ input . __proto__ = HTMLElement . prototype ;
87+ input . files = files ;
9688
97- const e = document . createEvent ( 'HTMLEvents' ) ;
98- e . initEvent ( 'change' , false , true ) ;
99- upload . $ . fileInput . dispatchEvent ( e ) ;
89+ change ( input ) ;
10090
10191 expect ( upload . files [ 0 ] ) . to . equal ( files [ 1 ] ) ;
10292 expect ( upload . files [ 1 ] ) . to . equal ( files [ 0 ] ) ;
@@ -117,10 +107,8 @@ describe('file list', () => {
117107
118108 it ( 'should not open upload dialog when max files added' , ( ) => {
119109 upload . maxFiles = 0 ;
120- const clickSpy = sinon . spy ( ) ;
121- upload . $ . fileInput . addEventListener ( 'click' , clickSpy ) ;
122- upload . $ . addFiles . dispatchEvent ( new MouseEvent ( 'click' ) ) ;
123- expect ( clickSpy . called ) . to . be . false ;
110+ click ( addFiles ) ;
111+ expect ( inputClickSpy . called ) . to . be . false ;
124112 } ) ;
125113
126114 it ( 'should set max-files-reached style attribute when max files added' , ( ) => {
0 commit comments