|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | + |
| 9 | +import {CommonModule} from '@angular/common'; |
| 10 | +import {IMAGE_LOADER} from '@angular/common/src/directives/ng_optimized_image/image_loaders/image_loader'; |
| 11 | +import {provideImgixLoader} from '@angular/common/src/directives/ng_optimized_image/image_loaders/imgix_loader'; |
| 12 | +import {NgOptimizedImageModule} from '@angular/common/src/directives/ng_optimized_image/ng_optimized_image'; |
| 13 | +import {RuntimeErrorCode} from '@angular/common/src/errors'; |
| 14 | +import {Component} from '@angular/core'; |
| 15 | +import {ComponentFixture, TestBed} from '@angular/core/testing'; |
| 16 | +import {expect} from '@angular/platform-browser/testing/src/matchers'; |
| 17 | + |
| 18 | +describe('Built-in image directive loaders', () => { |
| 19 | + describe('Imgix loader', () => { |
| 20 | + describe('invalid paths', () => { |
| 21 | + it('should throw if path is empty', () => { |
| 22 | + expect(() => { |
| 23 | + setupTestingModule([provideImgixLoader('')]); |
| 24 | + }) |
| 25 | + .toThrowError( |
| 26 | + `NG0${RuntimeErrorCode.INVALID_INPUT}: ImgixLoader has detected an invalid path: ` + |
| 27 | + `expecting a path like https://somepath.imgix.net/` + |
| 28 | + `but got: \`\``); |
| 29 | + }); |
| 30 | + |
| 31 | + it('should throw if not a path', () => { |
| 32 | + expect(() => { |
| 33 | + setupTestingModule([provideImgixLoader('wellhellothere')]); |
| 34 | + }) |
| 35 | + .toThrowError( |
| 36 | + `NG0${RuntimeErrorCode.INVALID_INPUT}: ImgixLoader has detected an invalid path: ` + |
| 37 | + `expecting a path like https://somepath.imgix.net/` + |
| 38 | + `but got: \`wellhellothere\``); |
| 39 | + }); |
| 40 | + |
| 41 | + it('should throw if path is missing a scheme', () => { |
| 42 | + expect(() => { |
| 43 | + setupTestingModule([provideImgixLoader('somepath.imgix.net')]); |
| 44 | + }) |
| 45 | + .toThrowError( |
| 46 | + `NG0${RuntimeErrorCode.INVALID_INPUT}: ImgixLoader has detected an invalid path: ` + |
| 47 | + `expecting a path like https://somepath.imgix.net/` + |
| 48 | + `but got: \`somepath.imgix.net\``); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should throw if path is malformed', () => { |
| 52 | + expect(() => { |
| 53 | + setupTestingModule([provideImgixLoader('somepa\th.imgix.net? few')]); |
| 54 | + }) |
| 55 | + .toThrowError( |
| 56 | + `NG0${RuntimeErrorCode.INVALID_INPUT}: ImgixLoader has detected an invalid path: ` + |
| 57 | + `expecting a path like https://somepath.imgix.net/` + |
| 58 | + `but got: \`somepa\th.imgix.net? few\``); |
| 59 | + }); |
| 60 | + }); |
| 61 | + |
| 62 | + it('should construct an image loader with the given path', () => { |
| 63 | + setupTestingModule([provideImgixLoader('https://somesite.imgix.net')]); |
| 64 | + |
| 65 | + const template = ` |
| 66 | + <img rawSrc="img.png" width="150" height="50"> |
| 67 | + <img rawSrc="img-2.png" width="150" height="50"> |
| 68 | + `; |
| 69 | + const fixture = createTestComponent(template); |
| 70 | + fixture.detectChanges(); |
| 71 | + |
| 72 | + const nativeElement = fixture.nativeElement as HTMLElement; |
| 73 | + const imgs = nativeElement.querySelectorAll('img')!; |
| 74 | + expect(imgs[0].src).toBe('https://somesite.imgix.net/img.png?auto=format'); |
| 75 | + expect(imgs[1].src).toBe('https://somesite.imgix.net/img-2.png?auto=format'); |
| 76 | + }); |
| 77 | + |
| 78 | + it('should handle a trailing forward slash on the path', () => { |
| 79 | + setupTestingModule([provideImgixLoader('https://somesite.imgix.net/')]); |
| 80 | + |
| 81 | + const template = ` |
| 82 | + <img rawSrc="img.png" width="150" height="50"> |
| 83 | + `; |
| 84 | + const fixture = createTestComponent(template); |
| 85 | + fixture.detectChanges(); |
| 86 | + |
| 87 | + const nativeElement = fixture.nativeElement as HTMLElement; |
| 88 | + const img = nativeElement.querySelector('img')!; |
| 89 | + expect(img.src).toBe('https://somesite.imgix.net/img.png?auto=format'); |
| 90 | + }); |
| 91 | + |
| 92 | + it('should handle a leading forward slash on the src', () => { |
| 93 | + setupTestingModule([provideImgixLoader('https://somesite.imgix.net/')]); |
| 94 | + |
| 95 | + const template = ` |
| 96 | + <img rawSrc="/img.png" width="150" height="50"> |
| 97 | + `; |
| 98 | + const fixture = createTestComponent(template); |
| 99 | + fixture.detectChanges(); |
| 100 | + |
| 101 | + const nativeElement = fixture.nativeElement as HTMLElement; |
| 102 | + const img = nativeElement.querySelector('img')!; |
| 103 | + expect(img.src).toBe('https://somesite.imgix.net/img.png?auto=format'); |
| 104 | + }); |
| 105 | + |
| 106 | + it('should be compatible with rawSrcset', () => { |
| 107 | + setupTestingModule([provideImgixLoader('https://somesite.imgix.net')]); |
| 108 | + |
| 109 | + const template = ` |
| 110 | + <img rawSrc="img.png" rawSrcset="100w, 200w" width="100" height="50"> |
| 111 | + `; |
| 112 | + const fixture = createTestComponent(template); |
| 113 | + fixture.detectChanges(); |
| 114 | + |
| 115 | + const nativeElement = fixture.nativeElement as HTMLElement; |
| 116 | + const img = nativeElement.querySelector('img')!; |
| 117 | + expect(img.src).toBe('https://somesite.imgix.net/img.png?auto=format'); |
| 118 | + expect(img.srcset) |
| 119 | + .toBe( |
| 120 | + 'https://somesite.imgix.net/img.png?auto=format&w=100 100w, https://somesite.imgix.net/img.png?auto=format&w=200 200w'); |
| 121 | + }); |
| 122 | + }); |
| 123 | +}); |
| 124 | + |
| 125 | + |
| 126 | +// Helpers |
| 127 | + |
| 128 | +@Component({ |
| 129 | + selector: 'test-cmp', |
| 130 | + template: '', |
| 131 | +}) |
| 132 | +class TestComponent { |
| 133 | +} |
| 134 | + |
| 135 | +function setupTestingModule(providers: any[]) { |
| 136 | + TestBed.configureTestingModule({ |
| 137 | + declarations: [TestComponent], |
| 138 | + // Note: the `NgOptimizedImage` directive is experimental and is not a part of the |
| 139 | + // `CommonModule` yet, so it's imported separately. |
| 140 | + imports: [CommonModule, NgOptimizedImageModule], |
| 141 | + providers, |
| 142 | + }); |
| 143 | +} |
| 144 | + |
| 145 | +function createTestComponent(template: string): ComponentFixture<TestComponent> { |
| 146 | + return TestBed.overrideComponent(TestComponent, {set: {template: template}}) |
| 147 | + .createComponent(TestComponent); |
| 148 | +} |
0 commit comments