|
6 | 6 | * found in the LICENSE file at https://angular.io/license |
7 | 7 | */ |
8 | 8 |
|
9 | | -import {IMAGE_LOADER, ImageLoader} from '@angular/common/src/directives/ng_optimized_image'; |
| 9 | +import { |
| 10 | + IMAGE_LOADER, |
| 11 | + ImageLoader, |
| 12 | + provideNetlifyLoader, |
| 13 | +} from '@angular/common/src/directives/ng_optimized_image'; |
10 | 14 | import {provideCloudflareLoader} from '@angular/common/src/directives/ng_optimized_image/image_loaders/cloudflare_loader'; |
11 | 15 | import {provideCloudinaryLoader} from '@angular/common/src/directives/ng_optimized_image/image_loaders/cloudinary_loader'; |
12 | 16 | import {provideImageKitLoader} from '@angular/common/src/directives/ng_optimized_image/image_loaders/imagekit_loader'; |
13 | 17 | import {provideImgixLoader} from '@angular/common/src/directives/ng_optimized_image/image_loaders/imgix_loader'; |
14 | 18 | import {isValidPath} from '@angular/common/src/directives/ng_optimized_image/url'; |
| 19 | +import {RuntimeErrorCode} from '@angular/common/src/errors'; |
15 | 20 | import {createEnvironmentInjector, EnvironmentInjector} from '@angular/core'; |
16 | 21 | import {TestBed} from '@angular/core/testing'; |
17 | 22 |
|
@@ -207,6 +212,57 @@ describe('Built-in image directive loaders', () => { |
207 | 212 | }); |
208 | 213 | }); |
209 | 214 |
|
| 215 | + describe('Netlify loader', () => { |
| 216 | + function createNetlifyLoader(path?: string): ImageLoader { |
| 217 | + const injector = createEnvironmentInjector( |
| 218 | + [provideNetlifyLoader(path)], |
| 219 | + TestBed.inject(EnvironmentInjector), |
| 220 | + ); |
| 221 | + return injector.get(IMAGE_LOADER); |
| 222 | + } |
| 223 | + it('should construct an image loader with an empty path', () => { |
| 224 | + const loader = createNetlifyLoader(); |
| 225 | + let config = {src: 'img.png'}; |
| 226 | + expect(loader(config)).toBe('/.netlify/images?url=%2Fimg.png'); |
| 227 | + }); |
| 228 | + it('should construct an image loader with the given path', () => { |
| 229 | + const loader = createNetlifyLoader('https://mysite.com'); |
| 230 | + let config = {src: 'img.png'}; |
| 231 | + expect(loader(config)).toBe('https://mysite.com/.netlify/images?url=%2Fimg.png'); |
| 232 | + }); |
| 233 | + it('should construct an image loader with the given path', () => { |
| 234 | + const loader = createNetlifyLoader('https://mysite.com'); |
| 235 | + const config = {src: 'img.png', width: 100}; |
| 236 | + expect(loader(config)).toBe('https://mysite.com/.netlify/images?url=%2Fimg.png&w=100'); |
| 237 | + }); |
| 238 | + |
| 239 | + it('should construct an image URL with custom options', () => { |
| 240 | + const loader = createNetlifyLoader('https://mysite.com'); |
| 241 | + const config = {src: 'img.png', width: 100, loaderParams: {quality: 50}}; |
| 242 | + expect(loader(config)).toBe('https://mysite.com/.netlify/images?url=%2Fimg.png&w=100&q=50'); |
| 243 | + }); |
| 244 | + |
| 245 | + it('should construct an image with an absolute URL', () => { |
| 246 | + const path = 'https://mysite.com'; |
| 247 | + const src = 'https://angular.io/img.png'; |
| 248 | + const loader = createNetlifyLoader(path); |
| 249 | + expect(loader({src})).toBe( |
| 250 | + 'https://mysite.com/.netlify/images?url=https%3A%2F%2Fangular.io%2Fimg.png', |
| 251 | + ); |
| 252 | + }); |
| 253 | + |
| 254 | + it('should warn if an unknown loader parameter is provided', () => { |
| 255 | + const path = 'https://mysite.com'; |
| 256 | + const loader = createNetlifyLoader(path); |
| 257 | + const config = {src: 'img.png', loaderParams: {unknown: 'value'}}; |
| 258 | + spyOn(console, 'warn'); |
| 259 | + expect(loader(config)).toBe('https://mysite.com/.netlify/images?url=%2Fimg.png'); |
| 260 | + expect(console.warn).toHaveBeenCalledWith( |
| 261 | + `NG0${RuntimeErrorCode.INVALID_LOADER_ARGUMENTS}: The Netlify image loader has detected an \`<img>\` tag with the unsupported attribute "\`unknown\`".`, |
| 262 | + ); |
| 263 | + }); |
| 264 | + }); |
| 265 | + |
210 | 266 | describe('loader utils', () => { |
211 | 267 | it('should identify valid paths', () => { |
212 | 268 | expect(isValidPath('https://cdn.imageprovider.com/image-test')).toBe(true); |
|
0 commit comments