22
33import type { AddressInfo } from "node:net" ;
44import express from "express" ;
5- import { describe , expect , expectTypeOf , it as rawIt } from "vitest" ;
5+ import { describe , expect , expectTypeOf , it as rawIt , vi } from "vitest" ;
66
77import { genUploader } from "../src/client" ;
88import { createRouteHandler , createUploadthing } from "../src/express" ;
@@ -34,6 +34,13 @@ export const setupUTServer = async () => {
3434 } )
3535 . onUploadError ( onErrorMock )
3636 . onUploadComplete ( uploadCompleteMock ) ,
37+ multi : f ( { text : { maxFileSize : "16MB" , maxFileCount : 2 } } )
38+ . middleware ( ( opts ) => {
39+ middlewareMock ( opts ) ;
40+ return { } ;
41+ } )
42+ . onUploadError ( onErrorMock )
43+ . onUploadComplete ( uploadCompleteMock ) ,
3744 withServerData : f (
3845 { text : { maxFileSize : "4MB" } } ,
3946 { awaitServerData : true } ,
@@ -264,6 +271,21 @@ describe("uploadFiles", () => {
264271 await close ( ) ;
265272 } ) ;
266273
274+ it ( "handles too many files errors" , async ( { db } ) => {
275+ const { uploadFiles, close } = await setupUTServer ( ) ;
276+
277+ const file1 = new File ( [ "foo" ] , "foo.txt" , { type : "text/plain" } ) ;
278+ const file2 = new File ( [ "bar" ] , "bar.txt" , { type : "text/plain" } ) ;
279+
280+ await expect (
281+ uploadFiles ( "foo" , { files : [ file1 , file2 ] } ) ,
282+ ) . rejects . toThrowErrorMatchingInlineSnapshot (
283+ `[UploadThingError: Invalid config: FileCountMismatch]` ,
284+ ) ;
285+
286+ await close ( ) ;
287+ } ) ;
288+
267289 it ( "handles invalid file type errors" , async ( { db } ) => {
268290 const { uploadFiles, close } = await setupUTServer ( ) ;
269291
@@ -277,4 +299,38 @@ describe("uploadFiles", () => {
277299
278300 await close ( ) ;
279301 } ) ;
302+
303+ it ( "runs onUploadBegin before uploading (single file)" , async ( ) => {
304+ const { uploadFiles, close } = await setupUTServer ( ) ;
305+
306+ const file = new File ( [ "foo" ] , "foo.txt" , { type : "text/plain" } ) ;
307+ const onUploadBegin = vi . fn ( ) ;
308+
309+ await uploadFiles ( "foo" , {
310+ files : [ file ] ,
311+ onUploadBegin,
312+ } ) ;
313+
314+ expect ( onUploadBegin ) . toHaveBeenCalledWith ( { file : "foo.txt" } ) ;
315+
316+ await close ( ) ;
317+ } ) ;
318+
319+ it ( "runs onUploadBegin before uploading (multi file)" , async ( ) => {
320+ const { uploadFiles, close } = await setupUTServer ( ) ;
321+
322+ const file1 = new File ( [ "foo" ] , "foo.txt" , { type : "text/plain" } ) ;
323+ const file2 = new File ( [ "bar" ] , "bar.txt" , { type : "text/plain" } ) ;
324+ const onUploadBegin = vi . fn ( ) ;
325+
326+ await uploadFiles ( "multi" , {
327+ files : [ file1 , file2 ] ,
328+ onUploadBegin,
329+ } ) ;
330+
331+ expect ( onUploadBegin ) . toHaveBeenCalledWith ( { file : "foo.txt" } ) ;
332+ expect ( onUploadBegin ) . toHaveBeenCalledWith ( { file : "bar.txt" } ) ;
333+
334+ await close ( ) ;
335+ } ) ;
280336} ) ;
0 commit comments