@@ -15,7 +15,8 @@ import { cloneDeep } from 'lodash';
1515import { TokenService } from './token.service' ;
1616import { AuthService } from './auth.service' ;
1717import { FeatureConfigService } from './platform/v1/spender/feature-config.service' ;
18- import { BehaviorSubject , of , throwError } from 'rxjs' ;
18+ import { FileService } from './file.service' ;
19+ import { BehaviorSubject , of } from 'rxjs' ;
1920import { apiEouRes } from '../mock-data/extended-org-user.data' ;
2021import { featureConfigOptInData } from '../mock-data/feature-config.data' ;
2122import { txnCustomPropertiesData } from '../mock-data/txn-custom-properties.data' ;
@@ -26,11 +27,13 @@ describe('UtilityService', () => {
2627 let tokenService : jasmine . SpyObj < TokenService > ;
2728 let authService : jasmine . SpyObj < AuthService > ;
2829 let featureConfigService : jasmine . SpyObj < FeatureConfigService > ;
30+ let fileService : jasmine . SpyObj < FileService > ;
2931
3032 beforeEach ( ( ) => {
3133 const tokenServiceSpy = jasmine . createSpyObj ( 'TokenService' , [ 'getClusterDomain' ] ) ;
3234 const authServiceSpy = jasmine . createSpyObj ( 'AuthService' , [ 'getEou' ] ) ;
3335 const featureConfigServiceSpy = jasmine . createSpyObj ( 'FeatureConfigService' , [ 'getConfiguration' ] ) ;
36+ const fileServiceSpy = jasmine . createSpyObj ( 'FileService' , [ 'getDataUrlFromBlob' ] ) ;
3437
3538 TestBed . configureTestingModule ( {
3639 providers : [
@@ -47,12 +50,17 @@ describe('UtilityService', () => {
4750 provide : FeatureConfigService ,
4851 useValue : featureConfigServiceSpy ,
4952 } ,
53+ {
54+ provide : FileService ,
55+ useValue : fileServiceSpy ,
56+ } ,
5057 ] ,
5158 } ) ;
5259 utilityService = TestBed . inject ( UtilityService ) ;
5360 tokenService = TestBed . inject ( TokenService ) as jasmine . SpyObj < TokenService > ;
5461 authService = TestBed . inject ( AuthService ) as jasmine . SpyObj < AuthService > ;
5562 featureConfigService = TestBed . inject ( FeatureConfigService ) as jasmine . SpyObj < FeatureConfigService > ;
63+ fileService = TestBed . inject ( FileService ) as jasmine . SpyObj < FileService > ;
5664 } ) ;
5765
5866 it ( 'should be created' , ( ) => {
@@ -610,16 +618,18 @@ describe('UtilityService', () => {
610618 describe ( 'webPathToBase64():' , ( ) => {
611619 it ( 'should convert webPath to base64 string' , async ( ) => {
612620 const mockBlob = new Blob ( [ 'test content' ] , { type : 'image/png' } ) ;
621+ const mockDataUrl = 'data:image/png;base64,dGVzdCBjb250ZW50' ;
613622 spyOn ( window , 'fetch' ) . and . resolveTo ( {
614623 blob : ( ) => Promise . resolve ( mockBlob ) ,
615624 ok : true ,
616625 } as Response ) ;
626+ fileService . getDataUrlFromBlob . and . resolveTo ( mockDataUrl ) ;
617627
618628 const result = await utilityService . webPathToBase64 ( 'https://example.com/image.png' ) ;
619629
620- expect ( result ) . toContain ( 'data:' ) ;
621- expect ( result ) . toContain ( 'base64,' ) ;
630+ expect ( result ) . toBe ( mockDataUrl ) ;
622631 expect ( window . fetch ) . toHaveBeenCalledWith ( 'https://example.com/image.png' ) ;
632+ expect ( fileService . getDataUrlFromBlob ) . toHaveBeenCalledWith ( mockBlob ) ;
623633 } ) ;
624634
625635 it ( 'should reject on fetch error' , async ( ) => {
@@ -629,34 +639,16 @@ describe('UtilityService', () => {
629639 await expectAsync ( utilityService . webPathToBase64 ( 'https://example.com/image.png' ) ) . toBeRejectedWith ( error ) ;
630640 } ) ;
631641
632- it ( 'should reject on FileReader error ' , async ( ) => {
642+ it ( 'should reject when getDataUrlFromBlob fails ' , async ( ) => {
633643 const mockBlob = new Blob ( [ 'test' ] , { type : 'image/png' } ) ;
644+ const readError = new Error ( 'Read error' ) ;
634645 spyOn ( window , 'fetch' ) . and . resolveTo ( {
635646 blob : ( ) => Promise . resolve ( mockBlob ) ,
636647 ok : true ,
637648 } as Response ) ;
649+ fileService . getDataUrlFromBlob . and . rejectWith ( readError ) ;
638650
639- // Mock FileReader to simulate error
640- const originalFileReader = window . FileReader ;
641- const mockFileReader = jasmine . createSpy ( 'FileReader' ) . and . returnValue ( {
642- readAsDataURL : function ( blob : Blob ) {
643- setTimeout ( ( ) => {
644- if ( this . onerror ) {
645- this . onerror ( new Error ( 'Read error' ) as unknown as ProgressEvent ) ;
646- }
647- } , 0 ) ;
648- } ,
649- } as unknown as FileReader ) ;
650- ( window as unknown as { FileReader : typeof FileReader } ) . FileReader = mockFileReader as unknown as typeof FileReader ;
651-
652- try {
653- await utilityService . webPathToBase64 ( 'https://example.com/image.png' ) ;
654- fail ( 'Should have thrown an error' ) ;
655- } catch ( error ) {
656- expect ( error ) . toBeDefined ( ) ;
657- } finally {
658- ( window as unknown as { FileReader : typeof FileReader } ) . FileReader = originalFileReader ;
659- }
651+ await expectAsync ( utilityService . webPathToBase64 ( 'https://example.com/image.png' ) ) . toBeRejectedWith ( readError ) ;
660652 } ) ;
661653 } ) ;
662654
0 commit comments