88function makeDragEvent ( options ?: {
99 types ?: string [ ] ;
1010 files ?: File [ ] ;
11+ items ?: NonNullable < WorkspaceFileDragEvent [ "dataTransfer" ] [ "items" ] > ;
1112 movedWithinTarget ?: boolean ;
1213} ) {
1314 const preventDefault = vi . fn ( ) ;
@@ -16,6 +17,7 @@ function makeDragEvent(options?: {
1617 types : options ?. types ?? [ "Files" ] ,
1718 files : options ?. files ?? [ ] ,
1819 dropEffect : "none" ,
20+ ...( options ?. items === undefined ? { } : { items : options . items } ) ,
1921 } ,
2022 relatedTarget : options ?. movedWithinTarget ? ( { } as EventTarget ) : null ,
2123 currentTarget : {
@@ -29,8 +31,9 @@ function makeDragEvent(options?: {
2931function makeHost ( ) {
3032 const setDragActive = vi . fn ( ) ;
3133 const addFiles = vi . fn ( ) ;
32- const host = { setDragActive, addFiles } satisfies WorkspaceFileDropHost ;
33- return { host, setDragActive, addFiles } ;
34+ const addFolders = vi . fn ( ) ;
35+ const host = { setDragActive, addFiles, addFolders } satisfies WorkspaceFileDropHost ;
36+ return { host, setDragActive, addFiles, addFolders } ;
3437}
3538
3639describe ( "makeWorkspaceFileDropHandlers" , ( ) => {
@@ -75,4 +78,57 @@ describe("makeWorkspaceFileDropHandlers", () => {
7578 expect ( setDragActive ) . toHaveBeenCalledWith ( false ) ;
7679 expect ( addFiles ) . toHaveBeenCalledWith ( [ file ] ) ;
7780 } ) ;
81+
82+ it ( "routes mixed drops to files and folders" , ( ) => {
83+ const file = new File ( [ "contents" ] , "example.txt" , { type : "text/plain" } ) ;
84+ const folder = new File ( [ ] , "project" , { type : "" } ) ;
85+ const directory = {
86+ kind : "file" ,
87+ getAsFile : ( ) => folder ,
88+ webkitGetAsEntry : ( ) => ( { isDirectory : true } ) ,
89+ } ;
90+ const { host, addFiles, addFolders } = makeHost ( ) ;
91+ const { event } = makeDragEvent ( {
92+ items : [
93+ directory ,
94+ {
95+ kind : "file" ,
96+ getAsFile : ( ) => file ,
97+ webkitGetAsEntry : ( ) => ( { isDirectory : false } ) ,
98+ } ,
99+ ] ,
100+ } ) ;
101+
102+ makeWorkspaceFileDropHandlers ( host ) . onDrop ( event ) ;
103+
104+ expect ( addFiles ) . toHaveBeenCalledWith ( [ file ] ) ;
105+ expect ( addFolders ) . toHaveBeenCalledWith ( [ folder ] ) ;
106+ } ) ;
107+
108+ it ( "routes a folder-only drop without attaching files" , ( ) => {
109+ const folder = new File ( [ ] , "project" , { type : "" } ) ;
110+ const directory = {
111+ kind : "file" ,
112+ getAsFile : ( ) => folder ,
113+ webkitGetAsEntry : ( ) => ( { isDirectory : true } ) ,
114+ } ;
115+ const { host, addFiles, addFolders } = makeHost ( ) ;
116+ const { event } = makeDragEvent ( { items : [ directory ] } ) ;
117+
118+ makeWorkspaceFileDropHandlers ( host ) . onDrop ( event ) ;
119+
120+ expect ( addFiles ) . not . toHaveBeenCalled ( ) ;
121+ expect ( addFolders ) . toHaveBeenCalledWith ( [ folder ] ) ;
122+ } ) ;
123+
124+ it ( "uses files when the browser does not expose drag items" , ( ) => {
125+ const file = new File ( [ "contents" ] , "example.txt" , { type : "text/plain" } ) ;
126+ const { host, addFiles, addFolders } = makeHost ( ) ;
127+ const { event } = makeDragEvent ( { files : [ file ] } ) ;
128+
129+ makeWorkspaceFileDropHandlers ( host ) . onDrop ( event ) ;
130+
131+ expect ( addFiles ) . toHaveBeenCalledWith ( [ file ] ) ;
132+ expect ( addFolders ) . not . toHaveBeenCalled ( ) ;
133+ } ) ;
78134} ) ;
0 commit comments