Files
angular__angular/packages/common/test/image_loaders/image_loader_spec.ts
T
Kara Erickson 7ce497e5bc feat(common): add built-in Imgix loader (#47082)
This commit adds a built-in Imgix loader for the
NgOptimizedImage directive. If you provide the
desired Imgix hostname, an ImageLoader will be
generated with the correct options.

Usage looks like this:

```ts
providers: [
  provideImgixLoader('https://some.imgix.net')
]
```

It sets the "auto=format" flag by default, which
ensures that the smallest image format supported
by the browser is served.

This change also moves the IMAGE_LOADER, ImageLoader,
and ImageLoaderConfig into a new directory that will
be shared by all built-in image loaders.

PR Close #47082
2022-08-16 17:36:52 +00:00

149 lines
5.6 KiB
TypeScript

/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {CommonModule} from '@angular/common';
import {IMAGE_LOADER} from '@angular/common/src/directives/ng_optimized_image/image_loaders/image_loader';
import {provideImgixLoader} from '@angular/common/src/directives/ng_optimized_image/image_loaders/imgix_loader';
import {NgOptimizedImageModule} from '@angular/common/src/directives/ng_optimized_image/ng_optimized_image';
import {RuntimeErrorCode} from '@angular/common/src/errors';
import {Component} from '@angular/core';
import {ComponentFixture, TestBed} from '@angular/core/testing';
import {expect} from '@angular/platform-browser/testing/src/matchers';
describe('Built-in image directive loaders', () => {
describe('Imgix loader', () => {
describe('invalid paths', () => {
it('should throw if path is empty', () => {
expect(() => {
setupTestingModule([provideImgixLoader('')]);
})
.toThrowError(
`NG0${RuntimeErrorCode.INVALID_INPUT}: ImgixLoader has detected an invalid path: ` +
`expecting a path like https://somepath.imgix.net/` +
`but got: \`\``);
});
it('should throw if not a path', () => {
expect(() => {
setupTestingModule([provideImgixLoader('wellhellothere')]);
})
.toThrowError(
`NG0${RuntimeErrorCode.INVALID_INPUT}: ImgixLoader has detected an invalid path: ` +
`expecting a path like https://somepath.imgix.net/` +
`but got: \`wellhellothere\``);
});
it('should throw if path is missing a scheme', () => {
expect(() => {
setupTestingModule([provideImgixLoader('somepath.imgix.net')]);
})
.toThrowError(
`NG0${RuntimeErrorCode.INVALID_INPUT}: ImgixLoader has detected an invalid path: ` +
`expecting a path like https://somepath.imgix.net/` +
`but got: \`somepath.imgix.net\``);
});
it('should throw if path is malformed', () => {
expect(() => {
setupTestingModule([provideImgixLoader('somepa\th.imgix.net? few')]);
})
.toThrowError(
`NG0${RuntimeErrorCode.INVALID_INPUT}: ImgixLoader has detected an invalid path: ` +
`expecting a path like https://somepath.imgix.net/` +
`but got: \`somepa\th.imgix.net? few\``);
});
});
it('should construct an image loader with the given path', () => {
setupTestingModule([provideImgixLoader('https://somesite.imgix.net')]);
const template = `
<img rawSrc="img.png" width="150" height="50">
<img rawSrc="img-2.png" width="150" height="50">
`;
const fixture = createTestComponent(template);
fixture.detectChanges();
const nativeElement = fixture.nativeElement as HTMLElement;
const imgs = nativeElement.querySelectorAll('img')!;
expect(imgs[0].src).toBe('https://somesite.imgix.net/img.png?auto=format');
expect(imgs[1].src).toBe('https://somesite.imgix.net/img-2.png?auto=format');
});
it('should handle a trailing forward slash on the path', () => {
setupTestingModule([provideImgixLoader('https://somesite.imgix.net/')]);
const template = `
<img rawSrc="img.png" width="150" height="50">
`;
const fixture = createTestComponent(template);
fixture.detectChanges();
const nativeElement = fixture.nativeElement as HTMLElement;
const img = nativeElement.querySelector('img')!;
expect(img.src).toBe('https://somesite.imgix.net/img.png?auto=format');
});
it('should handle a leading forward slash on the src', () => {
setupTestingModule([provideImgixLoader('https://somesite.imgix.net/')]);
const template = `
<img rawSrc="/img.png" width="150" height="50">
`;
const fixture = createTestComponent(template);
fixture.detectChanges();
const nativeElement = fixture.nativeElement as HTMLElement;
const img = nativeElement.querySelector('img')!;
expect(img.src).toBe('https://somesite.imgix.net/img.png?auto=format');
});
it('should be compatible with rawSrcset', () => {
setupTestingModule([provideImgixLoader('https://somesite.imgix.net')]);
const template = `
<img rawSrc="img.png" rawSrcset="100w, 200w" width="100" height="50">
`;
const fixture = createTestComponent(template);
fixture.detectChanges();
const nativeElement = fixture.nativeElement as HTMLElement;
const img = nativeElement.querySelector('img')!;
expect(img.src).toBe('https://somesite.imgix.net/img.png?auto=format');
expect(img.srcset)
.toBe(
'https://somesite.imgix.net/img.png?auto=format&w=100 100w, https://somesite.imgix.net/img.png?auto=format&w=200 200w');
});
});
});
// Helpers
@Component({
selector: 'test-cmp',
template: '',
})
class TestComponent {
}
function setupTestingModule(providers: any[]) {
TestBed.configureTestingModule({
declarations: [TestComponent],
// Note: the `NgOptimizedImage` directive is experimental and is not a part of the
// `CommonModule` yet, so it's imported separately.
imports: [CommonModule, NgOptimizedImageModule],
providers,
});
}
function createTestComponent(template: string): ComponentFixture<TestComponent> {
return TestBed.overrideComponent(TestComponent, {set: {template: template}})
.createComponent(TestComponent);
}