88
99import { CommonModule , DOCUMENT } from '@angular/common' ;
1010import { IMAGE_LOADER , ImageLoader , ImageLoaderConfig , NgOptimizedImageModule } from '@angular/common/src/directives/ng_optimized_image' ;
11+ import { RuntimeErrorCode } from '@angular/common/src/errors' ;
1112import { Component } from '@angular/core' ;
1213import { ComponentFixture , TestBed } from '@angular/core/testing' ;
1314import { expect } from '@angular/platform-browser/testing/src/matchers' ;
@@ -73,12 +74,33 @@ describe('Image directive', () => {
7374 fixture . detectChanges ( ) ;
7475 } )
7576 . toThrowError (
76- 'NG02950: The NgOptimizedImage directive (activated on an <img> element with the ' +
77+ `NG0${
78+ RuntimeErrorCode
79+ . UNEXPECTED_SRC_ATTR } : The NgOptimizedImage directive (activated on an <img> element with the ` +
7780 '`rawSrc="path/img.png"`) has detected that the `src` is also set (to `path/img2.png`). ' +
7881 'Please remove the `src` attribute from this image. The NgOptimizedImage directive will use ' +
7982 'the `rawSrc` to compute the final image URL and set the `src` itself.' ) ;
8083 } ) ;
8184
85+ it ( 'should throw if both `rawSrc` and `srcset` is present' , ( ) => {
86+ setupTestingModule ( ) ;
87+
88+ const template =
89+ '<img rawSrc="img-100.png" srcset="img-100.png 100w, img-200.png 200w" width="100" height="50">' ;
90+ expect ( ( ) => {
91+ const fixture = createTestComponent ( template ) ;
92+ fixture . detectChanges ( ) ;
93+ } )
94+ . toThrowError (
95+ `NG0${
96+ RuntimeErrorCode
97+ . UNEXPECTED_SRCSET_ATTR } : The NgOptimizedImage directive (activated on an <img> element with the ` +
98+ '`rawSrc="img-100.png"`) has detected that the `srcset` has been set. ' +
99+ 'Please replace the `srcset` attribute from this image with `rawSrcset`. ' +
100+ 'The NgOptimizedImage directive uses `rawSrcset` to set the `srcset` attribute' +
101+ 'at a time that does not disrupt lazy loading.' ) ;
102+ } ) ;
103+
82104 it ( 'should throw if `rawSrc` contains a Base64-encoded image (that starts with `data:`)' , ( ) => {
83105 setupTestingModule ( ) ;
84106
@@ -88,7 +110,9 @@ describe('Image directive', () => {
88110 fixture . detectChanges ( ) ;
89111 } )
90112 . toThrowError (
91- 'NG02951: The NgOptimizedImage directive has detected that the `rawSrc` was set ' +
113+ `NG0${
114+ RuntimeErrorCode
115+ . INVALID_INPUT } : The NgOptimizedImage directive has detected that the \`rawSrc\` was set ` +
92116 'to a Base64-encoded string (' + ANGULAR_LOGO_BASE64 . substring ( 0 , 50 ) + '...). ' +
93117 'Base64-encoded strings are not supported by the NgOptimizedImage directive. ' +
94118 'Use a regular `src` attribute (instead of `rawSrc`) to disable the NgOptimizedImage ' +
@@ -112,7 +136,7 @@ describe('Image directive', () => {
112136 // Note: use RegExp to partially match the error message, since the blob URL
113137 // is created dynamically, so it might be different for each invocation.
114138 const errorMessageRegExp =
115- / N G 0 2 9 5 1 : T h e N g O p t i m i z e d I m a g e d i r e c t i v e h a s d e t e c t e d t h a t t h e ` r a w S r c ` w a s s e t t o a b l o b U R L \( b l o b : / ;
139+ / N G 0 2 9 5 2 : T h e N g O p t i m i z e d I m a g e d i r e c t i v e h a s d e t e c t e d t h a t t h e ` r a w S r c ` w a s s e t t o a b l o b U R L \( b l o b : / ;
116140 expect ( ( ) => {
117141 const template = '<img rawSrc="' + blobURL + '" width="50" height="50">' ;
118142 const fixture = createTestComponent ( template ) ;
@@ -131,7 +155,9 @@ describe('Image directive', () => {
131155 fixture . detectChanges ( ) ;
132156 } )
133157 . toThrowError (
134- 'NG02953: The NgOptimizedImage directive (activated on an <img> ' +
158+ `NG0${
159+ RuntimeErrorCode
160+ . REQUIRED_INPUT_MISSING } : The NgOptimizedImage directive (activated on an <img> ` +
135161 'element with the `rawSrc="img.png"`) has detected that the required ' +
136162 '`width` attribute is missing. Please specify the `width` attribute ' +
137163 'on the mentioned element.' ) ;
@@ -146,7 +172,9 @@ describe('Image directive', () => {
146172 fixture . detectChanges ( ) ;
147173 } )
148174 . toThrowError (
149- 'NG02951: The NgOptimizedImage directive has detected that the `width` ' +
175+ `NG0${
176+ RuntimeErrorCode
177+ . INVALID_INPUT } : The NgOptimizedImage directive has detected that the \`width\` ` +
150178 'has an invalid value: expecting a number that represents the width ' +
151179 'in pixels, but got: `10px`.' ) ;
152180 } ) ;
@@ -160,7 +188,9 @@ describe('Image directive', () => {
160188 fixture . detectChanges ( ) ;
161189 } )
162190 . toThrowError (
163- 'NG02953: The NgOptimizedImage directive (activated on an <img> ' +
191+ `NG0${
192+ RuntimeErrorCode
193+ . REQUIRED_INPUT_MISSING } : The NgOptimizedImage directive (activated on an <img> ` +
164194 'element with the `rawSrc="img.png"`) has detected that the required ' +
165195 '`height` attribute is missing. Please specify the `height` attribute ' +
166196 'on the mentioned element.' ) ;
@@ -175,7 +205,9 @@ describe('Image directive', () => {
175205 fixture . detectChanges ( ) ;
176206 } )
177207 . toThrowError (
178- 'NG02951: The NgOptimizedImage directive has detected that the `height` ' +
208+ `NG0${
209+ RuntimeErrorCode
210+ . INVALID_INPUT } : The NgOptimizedImage directive has detected that the \`height\` ` +
179211 'has an invalid value: expecting a number that represents the height ' +
180212 'in pixels, but got: `10%`.' ) ;
181213 } ) ;
@@ -189,7 +221,9 @@ describe('Image directive', () => {
189221 fixture . detectChanges ( ) ;
190222 } )
191223 . toThrowError (
192- 'NG02951: The NgOptimizedImage directive has detected that the `rawSrc` ' +
224+ `NG0${
225+ RuntimeErrorCode
226+ . INVALID_INPUT } : The NgOptimizedImage directive has detected that the \`rawSrc\` ` +
193227 'has an invalid value: expecting a non-empty string, but got: `` (empty string).' ) ;
194228 } ) ;
195229
@@ -202,7 +236,9 @@ describe('Image directive', () => {
202236 fixture . detectChanges ( ) ;
203237 } )
204238 . toThrowError (
205- 'NG02951: The NgOptimizedImage directive has detected that the `rawSrc` ' +
239+ `NG0${
240+ RuntimeErrorCode
241+ . INVALID_INPUT } : The NgOptimizedImage directive has detected that the \`rawSrc\` ` +
206242 'has an invalid value: expecting a non-empty string, but got: ` ` (empty string).' ) ;
207243 } ) ;
208244
@@ -213,28 +249,29 @@ describe('Image directive', () => {
213249 [ 'priority' , true ]
214250 ] ;
215251 inputs . forEach ( ( [ inputName , value ] ) => {
216- it ( `should throw if inputs got changed after directive init (the \`${ inputName } \` input)` ,
217- ( ) => {
218- setupTestingModule ( ) ;
219-
220- const template =
221- '<img [rawSrc]="rawSrc" [width]="width" [height]="height" [priority]="priority">' ;
222- expect ( ( ) => {
223- // Initial render
224- const fixture = createTestComponent ( template ) ;
225- fixture . detectChanges ( ) ;
226-
227- // Update input (expect to throw)
228- ( fixture . componentInstance as unknown as
229- { [ key : string ] : unknown } ) [ inputName as string ] = value ;
230- fixture . detectChanges ( ) ;
231- } )
232- . toThrowError (
233- `NG02952: The NgOptimizedImage directive (activated on an <img> element ` +
234- `with the \`rawSrc="img.png"\`) has detected that the \`${ inputName } \` is ` +
235- `updated after the initialization. The NgOptimizedImage directive will not ` +
236- `react to this input change.` ) ;
237- } ) ;
252+ it ( `should throw if inputs got changed after directive init (the \`${ inputName } \` input)` , ( ) => {
253+ setupTestingModule ( ) ;
254+
255+ const template =
256+ '<img [rawSrc]="rawSrc" [width]="width" [height]="height" [priority]="priority">' ;
257+ expect ( ( ) => {
258+ // Initial render
259+ const fixture = createTestComponent ( template ) ;
260+ fixture . detectChanges ( ) ;
261+
262+ // Update input (expect to throw)
263+ ( fixture . componentInstance as unknown as { [ key : string ] : unknown } ) [ inputName as string ] =
264+ value ;
265+ fixture . detectChanges ( ) ;
266+ } )
267+ . toThrowError (
268+ `NG0${
269+ RuntimeErrorCode
270+ . UNEXPECTED_INPUT_CHANGE } : The NgOptimizedImage directive (activated on an <img> element ` +
271+ `with the \`rawSrc="img.png"\`) has detected that the \`${ inputName } \` is ` +
272+ `updated after the initialization. The NgOptimizedImage directive will not ` +
273+ `react to this input change.` ) ;
274+ } ) ;
238275 } ) ;
239276 } ) ;
240277
0 commit comments