99import { CommonModule , DOCUMENT , IMAGE_CONFIG , ImageConfig } from '@angular/common' ;
1010import { RuntimeErrorCode } from '@angular/common/src/errors' ;
1111import { PLATFORM_SERVER_ID } from '@angular/common/src/platform_id' ;
12- import { Component , PLATFORM_ID , Provider , Type } from '@angular/core' ;
12+ import { ChangeDetectionStrategy , Component , PLATFORM_ID , Provider , Type } from '@angular/core' ;
1313import { ComponentFixture , TestBed } from '@angular/core/testing' ;
1414import { DomSanitizer , SafeResourceUrl } from '@angular/platform-browser' ;
1515import { expect } from '@angular/platform-browser/testing/src/matchers' ;
@@ -1078,7 +1078,6 @@ describe('Image directive', () => {
10781078 fixture . detectChanges ( ) ;
10791079 const nativeElement = fixture . nativeElement as HTMLElement ;
10801080 const img = nativeElement . querySelector ( 'img' ) ! ;
1081- const styles = parseInlineStyles ( img ) ;
10821081 // Double quotes removed to account for different browser behavior.
10831082 expect ( img . getAttribute ( 'style' ) ?. replace ( / " / g, '' ) . replace ( / \s / g, '' ) ) . toBe (
10841083 `background-size:cover;background-position:50%50%;background-repeat:no-repeat;background-image:url(data:image/png;base64,iVBORw0KGgoAAAANSUhEU);filter:blur(${ PLACEHOLDER_BLUR_AMOUNT } px);` ,
@@ -1100,6 +1099,19 @@ describe('Image directive', () => {
11001099 ) ;
11011100 } ) ;
11021101
1102+ it ( 'should replace the placeholder with the actual image on load' , ( ) => {
1103+ setupTestingModule ( ) ;
1104+ const template = '<img ngSrc="path/img.png" width="400" height="300" placeholder="true" />' ;
1105+ const fixture = createTestComponent ( template , ChangeDetectionStrategy . OnPush ) ;
1106+ fixture . detectChanges ( ) ;
1107+ const nativeElement = fixture . nativeElement as HTMLElement ;
1108+ const img = nativeElement . querySelector ( 'img' ) ! ;
1109+ expect ( parseInlineStyles ( img ) . has ( 'background-image' ) ) . toBe ( true ) ;
1110+ img . dispatchEvent ( new Event ( 'load' ) ) ;
1111+ fixture . detectChanges ( ) ;
1112+ expect ( parseInlineStyles ( img ) . has ( 'background-image' ) ) . toBe ( false ) ;
1113+ } ) ;
1114+
11031115 it ( 'should use the placeholderResolution set in imageConfig' , ( ) => {
11041116 const imageConfig = {
11051117 placeholderResolution : 30 ,
@@ -2177,10 +2189,13 @@ function setUpModuleNoLoader() {
21772189 } ) ;
21782190}
21792191
2180- function createTestComponent ( template : string ) : ComponentFixture < TestComponent > {
2181- return TestBed . overrideComponent ( TestComponent , { set : { template : template } } ) . createComponent (
2182- TestComponent ,
2183- ) ;
2192+ function createTestComponent (
2193+ template : string ,
2194+ changeDetection = ChangeDetectionStrategy . OnPush ,
2195+ ) : ComponentFixture < TestComponent > {
2196+ return TestBed . overrideComponent ( TestComponent , {
2197+ set : { template, changeDetection} ,
2198+ } ) . createComponent ( TestComponent ) ;
21842199}
21852200
21862201function parseInlineStyles ( img : HTMLImageElement ) : Map < string , string > {
0 commit comments