@@ -36,6 +36,8 @@ import {
3636 FileValidationError ,
3737 isFileSizeError ,
3838 isTooManyFilesError ,
39+ IsValidFileName ,
40+ isValidFileName ,
3941 TooManyFilesError ,
4042} from "../utils" ;
4143
@@ -633,9 +635,69 @@ describe("useFileUpload", () => {
633635 totalBytes : 0 ,
634636 totalFiles : 0 ,
635637 totalFileSize : MAX_UPLOAD_SIZE ,
638+ isValidFileName,
636639 } ) ;
637640 } ) ;
638641
642+ it ( "should allow for a custom isValidFileName so that files without extensions can be uploaded" , ( ) => {
643+ const allowExtensionsAndLicense : IsValidFileName = (
644+ file ,
645+ extensionRegExp ,
646+ extensions
647+ ) =>
648+ isValidFileName ( file , extensionRegExp , extensions ) ||
649+ / ^ L I C E N S E $ / i. test ( file . name ) ;
650+
651+ const customIsValidFileName = jest . fn ( allowExtensionsAndLicense ) ;
652+ const extensions = [ "md" , "txt" ] ;
653+ const extensionRegExp = new RegExp ( "\\.(md|txt)$" , "i" ) ;
654+
655+ const { getByLabelText } = renderComplex ( {
656+ extensions,
657+ isValidFileName : customIsValidFileName ,
658+ } ) ;
659+
660+ const input = getByLabelText ( / U p l o a d / ) as HTMLInputElement ;
661+ expect ( customIsValidFileName ) . not . toBeCalled ( ) ;
662+
663+ const md = createFile ( "file2.md" , 1024 ) ;
664+ userEvent . upload ( input , md ) ;
665+ expect ( customIsValidFileName ) . toBeCalledWith (
666+ md ,
667+ extensionRegExp ,
668+ extensions
669+ ) ;
670+ expect ( getErrorDialog ) . toThrow ( ) ;
671+
672+ const txt = createFile ( "file2.txt" , 1024 ) ;
673+ userEvent . upload ( input , txt ) ;
674+ expect ( customIsValidFileName ) . toBeCalledWith (
675+ txt ,
676+ extensionRegExp ,
677+ extensions
678+ ) ;
679+ expect ( getErrorDialog ) . toThrow ( ) ;
680+
681+ const license = createFile ( "LICENSE" , 1024 ) ;
682+ userEvent . upload ( input , license ) ;
683+ expect ( customIsValidFileName ) . toBeCalledWith (
684+ license ,
685+ extensionRegExp ,
686+ extensions
687+ ) ;
688+ expect ( getErrorDialog ) . toThrow ( ) ;
689+
690+ const png = createFile ( "file1.png" , 1024 ) ;
691+ userEvent . upload ( input , png ) ;
692+ expect ( customIsValidFileName ) . toBeCalledWith (
693+ png ,
694+ extensionRegExp ,
695+ extensions
696+ ) ;
697+ expect ( getErrorDialog ) . not . toThrow ( ) ;
698+ fireEvent . click ( getByRoleGlobal ( document . body , "button" , { name : "Okay" } ) ) ;
699+ } ) ;
700+
639701 it ( "should throw a TooManyFilesError if too many files are uploaded" , ( ) => {
640702 const { getByLabelText, getByText } = renderComplex ( { maxFiles : 1 } ) ;
641703 const input = getByLabelText ( / U p l o a d / ) as HTMLInputElement ;
0 commit comments