@@ -220,6 +220,52 @@ describe('CloudinaryImage', () => {
220220 } ) ;
221221 } ) ;
222222
223+ describe ( 'event emitters' , ( ) => {
224+ let onImageLoad ;
225+ let onImageError ;
226+ let des : DebugElement ; // the elements w/ the directive
227+
228+ @Component ( {
229+ template : `<cl-image id="image1" [public-id]="publicId"></cl-image>`
230+ } )
231+ class TestComponent {
232+ publicId : string = 'sample' ;
233+ }
234+
235+ let fixture : ComponentFixture < TestComponent > ;
236+
237+ beforeEach ( ( ) => {
238+ fixture = TestBed . configureTestingModule ( {
239+ declarations : [ CloudinaryTransformationDirective , CloudinaryImage , TestComponent ] ,
240+ providers : [ { provide : Cloudinary , useValue : localCloudinary } ]
241+ } ) . createComponent ( TestComponent ) ;
242+
243+ fixture . detectChanges ( ) ; // initial binding
244+ // all elements with an attached CloudinaryImage
245+ des = fixture . debugElement . query ( By . directive ( CloudinaryImage ) ) ;
246+ onImageLoad = jasmine . createSpy ( 'onImageLoad' ) ;
247+ onImageError = jasmine . createSpy ( 'onImageError' ) ;
248+ des . componentInstance . onLoad . subscribe ( onImageLoad ) ;
249+ des . componentInstance . onError . subscribe ( onImageError ) ;
250+ } ) ;
251+
252+ it ( 'calls the onLoad callback when image loads successfully' , ( ) => {
253+ // Simulate the load event
254+ const img = des . children [ 0 ] . nativeElement as HTMLImageElement ;
255+ img . dispatchEvent ( new Event ( 'load' ) ) ;
256+ expect ( onImageLoad ) . toHaveBeenCalled ( ) ;
257+ expect ( onImageError ) . not . toHaveBeenCalled ( ) ;
258+ } ) ;
259+
260+ it ( 'calls the onError callback when image fails to load' , ( ) => {
261+ // Simulate the error event
262+ const img = des . children [ 0 ] . nativeElement as HTMLImageElement ;
263+ img . dispatchEvent ( new CustomEvent ( 'error' , { detail : 'Eitan was here' } ) ) ;
264+ expect ( onImageLoad ) . not . toHaveBeenCalled ( ) ;
265+ expect ( onImageError ) . toHaveBeenCalled ( ) ;
266+ } ) ;
267+ } ) ;
268+
223269 describe ( 'responsive images with nested transformations using the cld-responsive attribute' , ( ) => {
224270 @Component ( {
225271 template : `<cl-image cld-responsive id="image1" public-id="responsive_sample.jpg">
@@ -254,5 +300,4 @@ describe('CloudinaryImage', () => {
254300 / c _ s c a l e , l _ t e x t : r o b o t o _ 2 5 _ b o l d : S D K , w _ 3 0 0 \/ e _ a r t : h o k u s a i \/ f _ a u t o \/ r e s p o n s i v e _ s a m p l e .j p g / ) ) ;
255301 } ) ;
256302 } ) ;
257-
258303} ) ;
0 commit comments