Skip to content

Commit c36c79b

Browse files
author
Jack Matthews
authored
Merge pull request #5 from thisissoon/hotfix/load-correct-img-when-inviewport
Hotfix/load correct img when inviewport
2 parents 36f9928 + 13d4e78 commit c36c79b

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

src/app/image-loader/image-loader.component.spec.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,16 @@ import { TestBed, async, ComponentFixture } from '@angular/core/testing';
22
import { NO_ERRORS_SCHEMA, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
33
import { By } from '@angular/platform-browser';
44

5+
import { WindowRef } from '@thisissoon/angular-inviewport';
6+
57
import { ImageLoaderComponent } from './image-loader.component';
68
import { Breakpoint, ResponsiveImage } from './shared/image.model';
79
import { ImageLoadedEvent } from './index';
810

11+
class MockWindowRef {
12+
public innerWidth = 800;
13+
}
14+
915
describe('ImageLoaderComponent', () => {
1016
let fixture: ComponentFixture<ImageLoaderComponent>;
1117
let component: ImageLoaderComponent;
@@ -40,7 +46,10 @@ describe('ImageLoaderComponent', () => {
4046
],
4147
declarations: [
4248
ImageLoaderComponent
43-
]
49+
],
50+
providers: [
51+
{ provide: WindowRef, useClass: MockWindowRef }
52+
],
4453
}).compileComponents();
4554
}));
4655

@@ -52,13 +61,17 @@ describe('ImageLoaderComponent', () => {
5261
fixture.detectChanges();
5362
});
5463

64+
it('should update size based on window ref object on init', () => {
65+
expect(component.size).toEqual('md');
66+
});
67+
5568
it('should set placeholder on init', () => {
5669
const spy = spyOn(component, 'setPlaceholder');
5770
component.ngOnInit();
5871
expect(spy).toHaveBeenCalled();
5972
});
6073

61-
it('should set fire placeholder loaded event on image load when loaded is false', () => {
74+
it('should fire placeholder loaded event on image load when loaded is false', () => {
6275
const spy = spyOn(component.imagePlaceholderLoaded, 'emit');
6376
component.loaded = false;
6477
const imageElement = fixture.debugElement.query(By.css('img'));
@@ -168,4 +181,5 @@ describe('ImageLoaderComponent', () => {
168181
component.ngOnDestroy();
169182
expect(spy).toHaveBeenCalled();
170183
});
184+
171185
});

src/app/image-loader/image-loader.component.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import 'rxjs/add/operator/debounceTime';
1818
import * as classes from './shared/classes';
1919
import * as events from './shared/events';
2020
import { ImageLoadedEvent, ResponsiveImage, RetinaImage, Size, Breakpoint, Retina } from './shared';
21+
import { WindowRef } from '@thisissoon/angular-inviewport';
2122

2223
/**
2324
* A component that renders a `img` element with the correct image url
@@ -185,6 +186,15 @@ export class ImageLoaderComponent implements OnInit, AfterViewInit, OnDestroy {
185186
public get notLoaded(): boolean {
186187
return !this.loaded;
187188
}
189+
/**
190+
* Creates instance of component. Updates this.size
191+
* based on window width.
192+
*
193+
* @memberof ImageLoaderComponent
194+
*/
195+
constructor(private windowRef: WindowRef) {
196+
this.onWidthChange(this.windowRef.innerWidth);
197+
}
188198
/**
189199
* Set placeholder image as image on component init
190200
*
@@ -207,7 +217,7 @@ export class ImageLoaderComponent implements OnInit, AfterViewInit, OnDestroy {
207217
.takeUntil(this.ngUnsubscribe$)
208218
.debounceTime(10)
209219
.subscribe((width) => this.onWidthChange(width));
210-
this.width$.next(dummyImg.offsetWidth);
220+
this.width$.next(this.windowRef.innerWidth);
211221
}
212222
/**
213223
* If element is in viewport preload image by setting the src

0 commit comments

Comments
 (0)