|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google Inc. 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 | +import {Component} from '@angular/core'; |
| 9 | +import {CommonModule} from '@angular/common'; |
| 10 | +import {ComponentFixture, TestBed, inject} from '@angular/core/testing'; |
| 11 | + |
| 12 | +import {DEFAULT_BREAKPOINTS_PROVIDER} from '../../media-query/breakpoints/break-points-provider'; |
| 13 | +import {BreakPointRegistry} from '../../media-query/breakpoints/break-point-registry'; |
| 14 | +import {MockMatchMedia} from '../../media-query/mock/mock-match-media'; |
| 15 | +import {MatchMedia} from '../../media-query/match-media'; |
| 16 | +import {FlexLayoutModule} from '../../module'; |
| 17 | + |
| 18 | +import {customMatchers} from '../../utils/testing/custom-matchers'; |
| 19 | +import {makeCreateTestComponent, queryFor} from '../../utils/testing/helpers'; |
| 20 | +import {expect} from '../../utils/testing/custom-matchers'; |
| 21 | +import {_dom as _} from '../../utils/testing/dom-tools'; |
| 22 | + |
| 23 | +const SRC_URLS = { |
| 24 | + 'xs': [ |
| 25 | + 'https://dummyimage.com/300x200/c7751e/fff.png', |
| 26 | + 'https://dummyimage.com/300x200/c7751e/000.png' |
| 27 | + ], |
| 28 | + 'gt-xs': [ |
| 29 | + 'https://dummyimage.com/400x250/c7c224/fff.png', |
| 30 | + 'https://dummyimage.com/400x250/c7c224/000.png' |
| 31 | + ], |
| 32 | + 'md': [ |
| 33 | + 'https://dummyimage.com/500x300/76c720/fff.png', |
| 34 | + 'https://dummyimage.com/500x300/76c720/000.png' |
| 35 | + ], |
| 36 | + 'lt-lg': [ |
| 37 | + 'https://dummyimage.com/600x350/25c794/fff.png', |
| 38 | + 'https://dummyimage.com/600x350/25c794/000.png' |
| 39 | + ], |
| 40 | + 'lg': [ |
| 41 | + 'https://dummyimage.com/700x400/258cc7/fff.png', |
| 42 | + 'https://dummyimage.com/700x400/258cc7/000.png' |
| 43 | + ], |
| 44 | + 'lt-xl': [ |
| 45 | + 'https://dummyimage.com/800x500/b925c7/ffffff.png', |
| 46 | + 'https://dummyimage.com/800x500/b925c7/000.png' |
| 47 | + ] |
| 48 | +}; |
| 49 | +const DEFAULT_SRC = 'https://dummyimage.com/300x300/c72538/ffffff.png'; |
| 50 | + |
| 51 | +describe('img-src directive', () => { |
| 52 | + let fixture: ComponentFixture<any>; |
| 53 | + let matchMedia: MockMatchMedia; |
| 54 | + let breakpoints: BreakPointRegistry; |
| 55 | + |
| 56 | + let componentWithTemplate = (template: string) => { |
| 57 | + fixture = makeCreateTestComponent(() => TestSrcComponent)(template); |
| 58 | + |
| 59 | + inject([MatchMedia, BreakPointRegistry], |
| 60 | + (_matchMedia: MockMatchMedia, _breakpoints: BreakPointRegistry) => { |
| 61 | + matchMedia = _matchMedia; |
| 62 | + breakpoints = _breakpoints; |
| 63 | + })(); |
| 64 | + }; |
| 65 | + |
| 66 | + beforeEach(() => { |
| 67 | + jasmine.addMatchers(customMatchers); |
| 68 | + |
| 69 | + // Configure testbed to prepare services |
| 70 | + TestBed.configureTestingModule({ |
| 71 | + imports: [CommonModule, FlexLayoutModule], |
| 72 | + declarations: [TestSrcComponent], |
| 73 | + providers: [ |
| 74 | + BreakPointRegistry, DEFAULT_BREAKPOINTS_PROVIDER, |
| 75 | + {provide: MatchMedia, useClass: MockMatchMedia} |
| 76 | + ] |
| 77 | + }); |
| 78 | + }); |
| 79 | + |
| 80 | + describe('with static api', () => { |
| 81 | + it('should preserve the static src attribute', () => { |
| 82 | + let url = 'https://dummyimage.com/300x300/c72538/ffffff.png'; |
| 83 | + componentWithTemplate(` |
| 84 | + <img src="${url}"> |
| 85 | + `); |
| 86 | + const img = queryFor(fixture, 'img')[0].nativeElement; |
| 87 | + |
| 88 | + fixture.detectChanges(); |
| 89 | + expect(_.getAttribute( img, 'src')).toEqual(url); |
| 90 | + }); |
| 91 | + |
| 92 | + it('should work with empty src attributes', () => { |
| 93 | + componentWithTemplate(` |
| 94 | + <img src=""> |
| 95 | + `); |
| 96 | + const img = queryFor(fixture, 'img')[0].nativeElement; |
| 97 | + |
| 98 | + fixture.detectChanges(); |
| 99 | + expect(img).toHaveAttributes({ |
| 100 | + src: '' |
| 101 | + }); |
| 102 | + }); |
| 103 | + |
| 104 | + it('should work standard input bindings', () => { |
| 105 | + componentWithTemplate(` |
| 106 | + <img [src]="defaultSrc" [src.xs]="xsSrc"> |
| 107 | + `); |
| 108 | + const img = queryFor(fixture, 'img')[0].nativeElement; |
| 109 | + |
| 110 | + fixture.detectChanges(); |
| 111 | + expect(img).toHaveAttributes({ |
| 112 | + src: 'https://dummyimage.com/300x300/c72538/ffffff.png' |
| 113 | + }); |
| 114 | + |
| 115 | + let url = 'https://dummyimage.com/700x400/258cc7/fff.png'; |
| 116 | + fixture.componentInstance.defaultSrc = url; |
| 117 | + fixture.detectChanges(); |
| 118 | + expect(img).toHaveAttributes({ src: url }); |
| 119 | + |
| 120 | + }); |
| 121 | + |
| 122 | + it('should work when `src` value is not defined', () => { |
| 123 | + componentWithTemplate(` |
| 124 | + <img src > |
| 125 | + `); |
| 126 | + |
| 127 | + const img = queryFor(fixture, 'img')[0].nativeElement; |
| 128 | + fixture.detectChanges(); |
| 129 | + expect(img).toHaveAttributes({ |
| 130 | + src: '' |
| 131 | + }); |
| 132 | + }); |
| 133 | + |
| 134 | + it('should only work with "<img>" elements.', () => { |
| 135 | + componentWithTemplate(` |
| 136 | + <iframe src.xs="none.png" > |
| 137 | + `); |
| 138 | + |
| 139 | + const img = queryFor(fixture, 'iframe')[0].nativeElement; |
| 140 | + fixture.detectChanges(); |
| 141 | + expect(img).not.toHaveAttributes({ |
| 142 | + src: '' |
| 143 | + }); |
| 144 | + }); |
| 145 | + |
| 146 | + }); |
| 147 | + |
| 148 | + describe('with responsive api', () => { |
| 149 | + |
| 150 | + it('should work with a isolated image element and responsive srcs', () => { |
| 151 | + componentWithTemplate(` |
| 152 | + <img [src]="xsSrc" |
| 153 | + [src.md]="mdSrc"> |
| 154 | + `); |
| 155 | + fixture.detectChanges(); |
| 156 | + |
| 157 | + let img = queryFor(fixture, 'img')[0].nativeElement; |
| 158 | + |
| 159 | + matchMedia.activate('md'); |
| 160 | + fixture.detectChanges(); |
| 161 | + expect(img).toBeDefined(); |
| 162 | + expect(img).toHaveAttributes({ |
| 163 | + src: SRC_URLS['md'][0] |
| 164 | + }); |
| 165 | + |
| 166 | + // When activating an unused breakpoint, fallback to default [src] value |
| 167 | + matchMedia.activate('xl'); |
| 168 | + fixture.detectChanges(); |
| 169 | + expect(img).toHaveAttributes({ |
| 170 | + src: SRC_URLS['xs'][0] |
| 171 | + }); |
| 172 | + }); |
| 173 | + |
| 174 | + it('should work if default [src] is not defined', () => { |
| 175 | + componentWithTemplate(` |
| 176 | + <img [src.md]="mdSrc"> |
| 177 | + `); |
| 178 | + fixture.detectChanges(); |
| 179 | + matchMedia.activate('md'); |
| 180 | + fixture.detectChanges(); |
| 181 | + |
| 182 | + let img = queryFor(fixture, 'img')[0].nativeElement; |
| 183 | + expect(img).toBeDefined(); |
| 184 | + expect(img).toHaveAttributes({ |
| 185 | + src: SRC_URLS['md'][0] |
| 186 | + }); |
| 187 | + |
| 188 | + // When activating an unused breakpoint, fallback to default [src] value |
| 189 | + matchMedia.activate('xl'); |
| 190 | + fixture.detectChanges(); |
| 191 | + expect(img).toHaveAttributes({ |
| 192 | + src: '' |
| 193 | + }); |
| 194 | + }); |
| 195 | + |
| 196 | + }); |
| 197 | +}); |
| 198 | + |
| 199 | +// ***************************************************************** |
| 200 | +// Template Component |
| 201 | +// ***************************************************************** |
| 202 | + |
| 203 | +@Component({ |
| 204 | + selector: 'test-src-api', |
| 205 | + template: '' |
| 206 | +}) |
| 207 | +export class TestSrcComponent { |
| 208 | + defaultSrc = ''; |
| 209 | + xsSrc = ''; |
| 210 | + mdSrc = ''; |
| 211 | + lgSrc = ''; |
| 212 | + |
| 213 | + constructor() { |
| 214 | + this.defaultSrc = DEFAULT_SRC; |
| 215 | + this.xsSrc = SRC_URLS['xs'][0]; |
| 216 | + this.mdSrc = SRC_URLS['md'][0]; |
| 217 | + this.lgSrc = SRC_URLS['lg'][0]; |
| 218 | + |
| 219 | + } |
| 220 | +} |
| 221 | + |
| 222 | + |
0 commit comments