@@ -642,9 +642,8 @@ describe('Image directive', () => {
642642 } ) ;
643643
644644 const inputs = [
645- [ 'ngSrc' , 'new-img.png' ] , [ 'width' , 10 ] , [ 'height' , 20 ] , [ 'priority' , true ] , [ 'fill' , true ] ,
646- [ 'loading' , true ] , [ 'sizes' , '90vw' ] , [ 'disableOptimizedSrcset' , true ] ,
647- [ 'loaderParams' , '{foo: "test1"}' ]
645+ [ 'width' , 10 ] , [ 'height' , 20 ] , [ 'priority' , true ] , [ 'fill' , true ] , [ 'loading' , true ] ,
646+ [ 'sizes' , '90vw' ] , [ 'disableOptimizedSrcset' , true ] , [ 'loaderParams' , '{foo: "test1"}' ]
648647 ] ;
649648 inputs . forEach ( ( [ inputName , value ] ) => {
650649 it ( `should throw if the \`${ inputName } \` input changed after directive initialized the input` ,
@@ -692,6 +691,35 @@ describe('Image directive', () => {
692691 } ) . toThrowError ( new RegExp ( expectedErrorMessage ) ) ;
693692 } ) ;
694693 } ) ;
694+ it ( `should not throw if ngSrc changed after directive is initialized` , ( ) => {
695+ @Component ( {
696+ selector : 'test-cmp' ,
697+ template : `<img
698+ [ngSrc]="ngSrc"
699+ [width]="width"
700+ [height]="height"
701+ [loading]="loading"
702+ [sizes]="sizes"
703+ >`
704+ } )
705+ class TestComponent {
706+ width = 100 ;
707+ height = 50 ;
708+ ngSrc = 'img.png' ;
709+ loading = false ;
710+ sizes = '100vw' ;
711+ }
712+
713+ setupTestingModule ( { component : TestComponent } ) ;
714+
715+ // Initial render
716+ const fixture = TestBed . createComponent ( TestComponent ) ;
717+ fixture . detectChanges ( ) ;
718+ expect ( ( ) => {
719+ fixture . componentInstance . ngSrc = 'newImg.png' ;
720+ fixture . detectChanges ( ) ;
721+ } ) . not . toThrowError ( new RegExp ( 'was updated after initialization' ) ) ;
722+ } ) ;
695723 } ) ;
696724
697725 describe ( 'lazy loading' , ( ) => {
@@ -1259,6 +1287,75 @@ describe('Image directive', () => {
12591287 expect ( imgs [ 1 ] . src . trim ( ) ) . toBe ( `${ IMG_BASE_URL } /img-2.png` ) ;
12601288 } ) ;
12611289
1290+ it ( 'should use the image loader to update `src` if `ngSrc` updated' , ( ) => {
1291+ @Component ( {
1292+ selector : 'test-cmp' ,
1293+ template : `<img
1294+ [ngSrc]="ngSrc"
1295+ width="300"
1296+ height="300"
1297+ >`
1298+ } )
1299+ class TestComponent {
1300+ ngSrc = `img.png` ;
1301+ }
1302+ const imageLoader = ( config : ImageLoaderConfig ) => `${ IMG_BASE_URL } /${ config . src } ` ;
1303+ setupTestingModule ( { imageLoader, component : TestComponent } ) ;
1304+ const fixture = TestBed . createComponent ( TestComponent ) ;
1305+ fixture . detectChanges ( ) ;
1306+
1307+ let nativeElement = fixture . nativeElement as HTMLElement ;
1308+ let imgs = nativeElement . querySelectorAll ( 'img' ) ! ;
1309+ expect ( imgs [ 0 ] . src ) . toBe ( `${ IMG_BASE_URL } /img.png` ) ;
1310+
1311+ fixture . componentInstance . ngSrc = 'updatedImg.png' ;
1312+ fixture . detectChanges ( ) ;
1313+ expect ( imgs [ 0 ] . src ) . toBe ( `${ IMG_BASE_URL } /updatedImg.png` ) ;
1314+ } ) ;
1315+
1316+ it ( 'should use the image loader to update `srcset` if `ngSrc` updated' , ( ) => {
1317+ @Component ( {
1318+ selector : 'test-cmp' ,
1319+ template : `<img
1320+ [ngSrc]="ngSrc"
1321+ width="300"
1322+ height="300"
1323+ sizes="100vw"
1324+ >`
1325+ } )
1326+ class TestComponent {
1327+ ngSrc = `img.png` ;
1328+ }
1329+ const imageLoader = ( config : ImageLoaderConfig ) => {
1330+ const width = config . width ? `?w=${ config . width } ` : `` ;
1331+ return `${ IMG_BASE_URL } /${ config . src } ${ width } ` ;
1332+ } ;
1333+ setupTestingModule ( { imageLoader, component : TestComponent } ) ;
1334+ const fixture = TestBed . createComponent ( TestComponent ) ;
1335+ fixture . detectChanges ( ) ;
1336+
1337+ let nativeElement = fixture . nativeElement as HTMLElement ;
1338+ let imgs = nativeElement . querySelectorAll ( 'img' ) ! ;
1339+ expect ( imgs [ 0 ] . getAttribute ( 'srcset' ) )
1340+ . toBe ( `${ IMG_BASE_URL } /img.png?w=640 640w, ${ IMG_BASE_URL } /img.png?w=750 750w, ${
1341+ IMG_BASE_URL } /img.png?w=828 828w, ${ IMG_BASE_URL } /img.png?w=1080 1080w, ${
1342+ IMG_BASE_URL } /img.png?w=1200 1200w, ${ IMG_BASE_URL } /img.png?w=1920 1920w, ${
1343+ IMG_BASE_URL } /img.png?w=2048 2048w, ${ IMG_BASE_URL } /img.png?w=3840 3840w`) ;
1344+
1345+ fixture . componentInstance . ngSrc = 'updatedImg.png' ;
1346+ nativeElement = fixture . nativeElement as HTMLElement ;
1347+ imgs = nativeElement . querySelectorAll ( 'img' ) ! ;
1348+ fixture . detectChanges ( ) ;
1349+ expect ( imgs [ 0 ] . getAttribute ( 'srcset' ) )
1350+ . toBe ( `${ IMG_BASE_URL } /updatedImg.png?w=640 640w, ${
1351+ IMG_BASE_URL } /updatedImg.png?w=750 750w, ${ IMG_BASE_URL } /updatedImg.png?w=828 828w, ${
1352+ IMG_BASE_URL } /updatedImg.png?w=1080 1080w, ${
1353+ IMG_BASE_URL } /updatedImg.png?w=1200 1200w, ${
1354+ IMG_BASE_URL } /updatedImg.png?w=1920 1920w, ${
1355+ IMG_BASE_URL } /updatedImg.png?w=2048 2048w, ${
1356+ IMG_BASE_URL } /updatedImg.png?w=3840 3840w`) ;
1357+ } ) ;
1358+
12621359 it ( 'should pass absolute URLs defined in the `ngSrc` to custom image loaders provided via the `IMAGE_LOADER` token' ,
12631360 ( ) => {
12641361 const imageLoader = ( config : ImageLoaderConfig ) => `${ config . src } ?rewritten=true` ;
0 commit comments