@@ -4,6 +4,11 @@ import * as FileUtils from '../../src/libs/fileDownload/FileUtils';
44
55jest . useFakeTimers ( ) ;
66
7+ const createMockFile = ( name : string , size : number ) => ( {
8+ name,
9+ size,
10+ } ) ;
11+
712describe ( 'FileUtils' , ( ) => {
813 describe ( 'splitExtensionFromFileName' , ( ) => {
914 it ( 'should return correct file name and extension' , ( ) => {
@@ -38,4 +43,42 @@ describe('FileUtils', () => {
3843 expect ( actualFileName ) . toEqual ( expectedFileName . replace ( CONST . REGEX . ILLEGAL_FILENAME_CHARACTERS , '_' ) ) ;
3944 } ) ;
4045 } ) ;
46+
47+ describe ( 'validateAttachment' , ( ) => {
48+ it ( 'should not return FILE_TOO_SMALL when validating small attachment' , ( ) => {
49+ const file = createMockFile ( 'file.csv' , CONST . API_ATTACHMENT_VALIDATIONS . MIN_SIZE - 1 ) ;
50+ const error = FileUtils . validateAttachment ( file , false , false ) ;
51+ expect ( error ) . not . toBe ( CONST . FILE_VALIDATION_ERRORS . FILE_TOO_SMALL ) ;
52+ } ) ;
53+
54+ it ( 'should return FILE_TOO_SMALL when validating small receipt' , ( ) => {
55+ const file = createMockFile ( 'receipt.jpg' , CONST . API_ATTACHMENT_VALIDATIONS . MIN_SIZE - 1 ) ;
56+ const error = FileUtils . validateAttachment ( file , false , true ) ;
57+ expect ( error ) . toBe ( CONST . FILE_VALIDATION_ERRORS . FILE_TOO_SMALL ) ;
58+ } ) ;
59+
60+ it ( 'should return FILE_TOO_LARGE for large non-image file' , ( ) => {
61+ const file = createMockFile ( 'file.pdf' , CONST . API_ATTACHMENT_VALIDATIONS . MAX_SIZE + 1 ) ;
62+ const error = FileUtils . validateAttachment ( file ) ;
63+ expect ( error ) . toBe ( CONST . FILE_VALIDATION_ERRORS . FILE_TOO_LARGE ) ;
64+ } ) ;
65+
66+ it ( 'should return FILE_TOO_LARGE_MULTIPLE when checking multiple files' , ( ) => {
67+ const file = createMockFile ( 'file.pdf' , CONST . API_ATTACHMENT_VALIDATIONS . MAX_SIZE + 1 ) ;
68+ const error = FileUtils . validateAttachment ( file , true ) ;
69+ expect ( error ) . toBe ( CONST . FILE_VALIDATION_ERRORS . FILE_TOO_LARGE_MULTIPLE ) ;
70+ } ) ;
71+
72+ it ( 'should return WRONG_FILE_TYPE for invalid receipt extension' , ( ) => {
73+ const file = createMockFile ( 'receipt.exe' , CONST . API_ATTACHMENT_VALIDATIONS . RECEIPT_MAX_SIZE - 1 ) ;
74+ const error = FileUtils . validateAttachment ( file , false , true ) ;
75+ expect ( error ) . toBe ( CONST . FILE_VALIDATION_ERRORS . WRONG_FILE_TYPE ) ;
76+ } ) ;
77+
78+ it ( 'should return empty string for valid image receipt' , ( ) => {
79+ const file = createMockFile ( 'receipt.jpg' , CONST . API_ATTACHMENT_VALIDATIONS . RECEIPT_MAX_SIZE - 1 ) ;
80+ const error = FileUtils . validateAttachment ( file , false , true ) ;
81+ expect ( error ) . toBe ( '' ) ;
82+ } ) ;
83+ } ) ;
4184} ) ;
0 commit comments