|
| 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 | + |
| 22 | +const SRC_URLS = { |
| 23 | + 'xs': [ |
| 24 | + 'https://dummyimage.com/300x200/c7751e/fff.png', |
| 25 | + 'https://dummyimage.com/300x200/c7751e/000.png' |
| 26 | + ], |
| 27 | + 'gt-xs': [ |
| 28 | + 'https://dummyimage.com/400x250/c7c224/fff.png', |
| 29 | + 'https://dummyimage.com/400x250/c7c224/000.png' |
| 30 | + ], |
| 31 | + 'md': [ |
| 32 | + 'https://dummyimage.com/500x300/76c720/fff.png', |
| 33 | + 'https://dummyimage.com/500x300/76c720/000.png' |
| 34 | + ], |
| 35 | + 'lt-lg': [ |
| 36 | + 'https://dummyimage.com/600x350/25c794/fff.png', |
| 37 | + 'https://dummyimage.com/600x350/25c794/000.png' |
| 38 | + ], |
| 39 | + 'lg': [ |
| 40 | + 'https://dummyimage.com/700x400/258cc7/fff.png', |
| 41 | + 'https://dummyimage.com/700x400/258cc7/000.png' |
| 42 | + ], |
| 43 | + 'lt-xl': [ |
| 44 | + 'https://dummyimage.com/800x500/b925c7/ffffff.png', |
| 45 | + 'https://dummyimage.com/800x500/b925c7/000.png' |
| 46 | + ] |
| 47 | +}; |
| 48 | +const DEFAULT_SRC = 'https://dummyimage.com/300x300/c72538/ffffff.png'; |
| 49 | + |
| 50 | +describe('img-src directive', () => { |
| 51 | + let fixture: ComponentFixture<any>; |
| 52 | + let matchMedia: MockMatchMedia; |
| 53 | + let breakpoints: BreakPointRegistry; |
| 54 | + |
| 55 | + let componentWithTemplate = (template: string) => { |
| 56 | + fixture = makeCreateTestComponent(() => TestSrcsetComponent)(template); |
| 57 | + |
| 58 | + inject([MatchMedia, BreakPointRegistry], |
| 59 | + (_matchMedia: MockMatchMedia, _breakpoints: BreakPointRegistry) => { |
| 60 | + matchMedia = _matchMedia; |
| 61 | + breakpoints = _breakpoints; |
| 62 | + })(); |
| 63 | + }; |
| 64 | + |
| 65 | + beforeEach(() => { |
| 66 | + jasmine.addMatchers(customMatchers); |
| 67 | + |
| 68 | + // Configure testbed to prepare services |
| 69 | + TestBed.configureTestingModule({ |
| 70 | + imports: [CommonModule, FlexLayoutModule], |
| 71 | + declarations: [TestSrcsetComponent], |
| 72 | + providers: [ |
| 73 | + BreakPointRegistry, DEFAULT_BREAKPOINTS_PROVIDER, |
| 74 | + {provide: MatchMedia, useClass: MockMatchMedia} |
| 75 | + ] |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + describe('with static api', () => { |
| 80 | + it('should preserve the static src attribute', () => { |
| 81 | + componentWithTemplate(` |
| 82 | + <img src="https://dummyimage.com/300x300/c72538/ffffff.png"> |
| 83 | + `); |
| 84 | + const img = queryFor(fixture, 'img')[0].nativeElement; |
| 85 | + |
| 86 | + fixture.detectChanges(); |
| 87 | + expect(img).toHaveAttributes({ |
| 88 | + src: 'https://dummyimage.com/300x300/c72538/ffffff.png' |
| 89 | + }); |
| 90 | + }); |
| 91 | + |
| 92 | + it('should work standard input bindings', () => { |
| 93 | + componentWithTemplate(` |
| 94 | + <img [src]="defaultSrc" [src.xs]="xsSrc"> |
| 95 | + `); |
| 96 | + const img = queryFor(fixture, 'img')[0].nativeElement; |
| 97 | + |
| 98 | + fixture.detectChanges(); |
| 99 | + expect(img).toHaveAttributes({ |
| 100 | + src: 'https://dummyimage.com/300x300/c72538/ffffff.png' |
| 101 | + }); |
| 102 | + }); |
| 103 | + |
| 104 | + it('should work when no `src` value is defined', () => { |
| 105 | + componentWithTemplate(` |
| 106 | + <img src="" > |
| 107 | + `); |
| 108 | + |
| 109 | + const img = queryFor(fixture, 'img')[0].nativeElement; |
| 110 | + fixture.detectChanges(); |
| 111 | + expect(img).toHaveAttributes({ |
| 112 | + src: '' |
| 113 | + }); |
| 114 | + }); |
| 115 | + }); |
| 116 | + |
| 117 | + describe('with responsive api', () => { |
| 118 | + |
| 119 | + it('should work with a isolated image element and responsive srcs', () => { |
| 120 | + componentWithTemplate(` |
| 121 | + <img [src]="xsSrc" |
| 122 | + [src.md]="mdSrc"> |
| 123 | + `); |
| 124 | + fixture.detectChanges(); |
| 125 | + |
| 126 | + let img = queryFor(fixture, 'img')[0].nativeElement; |
| 127 | + |
| 128 | + matchMedia.activate('md'); |
| 129 | + fixture.detectChanges(); |
| 130 | + expect(img).toBeDefined(); |
| 131 | + expect(img).toHaveAttributes({ |
| 132 | + src: SRC_URLS['md'][0] |
| 133 | + }); |
| 134 | + |
| 135 | + // When activating an unused breakpoint, fallback to default [src] value |
| 136 | + matchMedia.activate('xl'); |
| 137 | + fixture.detectChanges(); |
| 138 | + expect(img).toHaveAttributes({ |
| 139 | + src: SRC_URLS['xs'][0] |
| 140 | + }); |
| 141 | + }); |
| 142 | + |
| 143 | + it('should work use [src] if default [src] is not defined', () => { |
| 144 | + componentWithTemplate(` |
| 145 | + <img [src.md]="mdSrc"> |
| 146 | + `); |
| 147 | + fixture.detectChanges(); |
| 148 | + matchMedia.activate('md'); |
| 149 | + fixture.detectChanges(); |
| 150 | + |
| 151 | + let img = queryFor(fixture, 'img')[0].nativeElement; |
| 152 | + expect(img).toBeDefined(); |
| 153 | + expect(img).toHaveAttributes({ |
| 154 | + src: SRC_URLS['md'][0] |
| 155 | + }); |
| 156 | + |
| 157 | + // When activating an unused breakpoint, fallback to default [src] value |
| 158 | + matchMedia.activate('xl'); |
| 159 | + fixture.detectChanges(); |
| 160 | + expect(img).toHaveAttributes({ |
| 161 | + src: '' |
| 162 | + }); |
| 163 | + }); |
| 164 | + |
| 165 | + }); |
| 166 | +}); |
| 167 | + |
| 168 | +// ***************************************************************** |
| 169 | +// Template Component |
| 170 | +// ***************************************************************** |
| 171 | + |
| 172 | +@Component({ |
| 173 | + selector: 'test-src-api', |
| 174 | + template: '' |
| 175 | +}) |
| 176 | +export class TestSrcsetComponent { |
| 177 | + defaultSrc = ''; |
| 178 | + xsSrc = ''; |
| 179 | + mdSrc = ''; |
| 180 | + lgSrc = ''; |
| 181 | + |
| 182 | + constructor() { |
| 183 | + this.defaultSrc = DEFAULT_SRC; |
| 184 | + this.xsSrc = SRC_URLS['xs'][0]; |
| 185 | + this.mdSrc = SRC_URLS['md'][0]; |
| 186 | + this.lgSrc = SRC_URLS['lg'][0]; |
| 187 | + |
| 188 | + } |
| 189 | +} |
| 190 | + |
| 191 | + |
0 commit comments