Skip to content

Commit 22498d9

Browse files
committed
test(ImageLoader): fixed flaky integration tests
fix #9
1 parent ecef4aa commit 22498d9

File tree

5 files changed

+52
-42
lines changed

5 files changed

+52
-42
lines changed

e2e/app.e2e-spec.ts

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,27 @@ describe('ImageLoader Lib E2E Tests', function () {
66

77
const browserWaitTimeout = 10000;
88

9-
beforeEach(() => page = new AppPage());
10-
11-
beforeEach(() => page.navigateTo());
12-
13-
beforeEach(() => page.scrollTo());
9+
beforeEach(() => {
10+
page = new AppPage();
11+
page.navigateTo();
12+
page.scrollTo();
13+
});
1414

1515
afterEach(() => {
16-
browser.manage().logs().get('browser').then((browserLog: any[]) => {
17-
expect(browserLog).toEqual([]);
18-
});
16+
// ensure no errors appear in console
17+
browser.manage()
18+
.logs()
19+
.get('browser')
20+
.then((browserLog) => {
21+
expect(browserLog).toEqual([]);
22+
});
1923
});
2024

2125
describe('placeholder image', () => {
22-
beforeEach(() => page.setWindowSize(300, 580));
26+
beforeEach(() => page.setWindowSize(400, 580));
2327

2428
it('should load placeholder image', () => {
25-
expect(page.getImageBottomElement().getAttribute('src')).toEqual('http://via.placeholder.com/35x15?text=placeholder');
29+
expect(page.getImageBottomElement().getAttribute('src')).toEqual('http://via.placeholder.com/40x40?text=placeholder');
2630
});
2731

2832
it('should update placeholder loaded boolean on init', () => {
@@ -31,39 +35,37 @@ describe('ImageLoader Lib E2E Tests', function () {
3135
});
3236

3337
describe('inviewport image on load', () => {
34-
beforeEach(() => page.setWindowSize(300, 580));
38+
beforeEach(() => page.setWindowSize(400, 580));
3539

3640
it('should load full res image if image is in viewport on page load', () => {
37-
38-
page.setWindowSize(300, 580);
3941
browser.wait(() => page.getLoadedImageTopElement());
4042

4143
const imageLoaderCompClass = page.getImageTopLoaderComp().getAttribute('class');
4244
const imgSrc = page.getImageTopElement().getAttribute('srcset');
4345
expect(imageLoaderCompClass).toContain('sn-image-loaded');
44-
expect(imgSrc).toEqual('http://via.placeholder.com/150x350?text=xs+1x 1x, http://via.placeholder.com/300x700?text=xs+2x 2x');
46+
expect(imgSrc).toEqual('http://via.placeholder.com/400x400?text=xs+1x 1x, http://via.placeholder.com/800x800?text=xs+2x 2x');
4547

4648

4749
});
4850

4951
});
5052

5153
describe('lazy load image', () => {
52-
beforeEach(() => page.setWindowSize(300, 580));
54+
beforeEach(() => page.setWindowSize(400, 580));
5355

5456
it('should load image when scrolled into viewport', () => {
5557
let imageLoaderCompClass = page.getImageBottomLoaderComp().getAttribute('class');
5658
let imgSrc = page.getImageBottomElement().getAttribute('src');
5759
expect(imageLoaderCompClass).toContain('sn-image-not-loaded');
58-
expect(imgSrc).toEqual('http://via.placeholder.com/35x15?text=placeholder');
60+
expect(imgSrc).toEqual('http://via.placeholder.com/40x40?text=placeholder');
5961

6062
page.scrollTo(0, 580 * 2);
6163
browser.wait(() => page.getLoadedImageBottomElement());
6264

6365
imageLoaderCompClass = page.getImageBottomLoaderComp().getAttribute('class');
6466
imgSrc = page.getImageBottomElement().getAttribute('srcset');
6567
expect(imageLoaderCompClass).toContain('sn-image-loaded');
66-
expect(imgSrc).toEqual('http://via.placeholder.com/150x350?text=xs+1x 1x, http://via.placeholder.com/300x700?text=xs+2x 2x');
68+
expect(imgSrc).toEqual('http://via.placeholder.com/400x400?text=xs+1x 1x, http://via.placeholder.com/800x800?text=xs+2x 2x');
6769

6870
});
6971

@@ -80,37 +82,37 @@ describe('ImageLoader Lib E2E Tests', function () {
8082
describe('responsive image', () => {
8183

8284
it('should load correct image for "xs" device size', () => {
83-
page.setWindowSize(300, 580);
85+
page.setWindowSize(400, 580);
8486
page.scrollTo(0, 580 * 2);
8587
browser.wait(() => page.getLoadedImageBottomElement());
8688
const imageLoaderCompClass = page.getImageBottomLoaderComp().getAttribute('class');
8789
const imgSrc = page.getImageBottomElement().getAttribute('srcset');
8890
expect(imageLoaderCompClass).toContain('sn-image-loaded');
89-
expect(imgSrc).toEqual('http://via.placeholder.com/150x350?text=xs+1x 1x, http://via.placeholder.com/300x700?text=xs+2x 2x');
91+
expect(imgSrc).toEqual('http://via.placeholder.com/400x400?text=xs+1x 1x, http://via.placeholder.com/800x800?text=xs+2x 2x');
9092
});
9193

9294
it('should load correct image for "md" device size', () => {
9395
page.setWindowSize(768, 580);
94-
page.scrollTo(0, 580 * 2);
96+
page.scrollTo(0, 580 * 2.5);
9597
browser.wait(() => page.getLoadedImageBottomElement());
9698

9799
const imgSrc = page.getImageBottomElement().getAttribute('srcset');
98-
expect(imgSrc).toEqual('http://via.placeholder.com/350x250?text=md+1x 1x, http://via.placeholder.com/700x500?text=md+2x 2x');
100+
expect(imgSrc).toEqual('http://via.placeholder.com/768x400?text=md+1x 1x, http://via.placeholder.com/1536x800?text=md+2x 2x');
99101
});
100102

101103
it('should load correct image for "lg" device size', () => {
102104
page.setWindowSize(1024, 580);
103-
page.scrollTo(0, 580 * 2);
105+
page.scrollTo(0, 580 * 2.5);
104106
browser.wait(() => page.getLoadedImageBottomElement());
105107

106108
const imgSrc = page.getImageBottomElement().getAttribute('srcset');
107-
expect(imgSrc).toEqual('http://via.placeholder.com/700x400?text=lg+1x 1x, http://via.placeholder.com/1400x800?text=lg+2x 2x');
109+
expect(imgSrc).toEqual('http://via.placeholder.com/1024x400?text=lg+1x 1x, http://via.placeholder.com/2048x800?text=lg+2x 2x');
108110
});
109111

110112
it('should update image loaded event count on window resize when image in viewport', () => {
111113
expect(page.getBottomFullResCountElement().getText()).toEqual('0');
112114

113-
page.setWindowSize(320, 580);
115+
page.setWindowSize(400, 580);
114116
page.scrollTo(0, 580 * 3);
115117
browser.wait(() => page.getLoadedImageBottomElement());
116118
expect(page.getBottomFullResCountElement().getText()).toEqual('1');

e2e/app.po.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,25 @@ export class AppPage {
66
}
77

88
scrollTo(x: number = 0, y: number = 0) {
9-
return browser.executeScript(`window.scrollTo(${x}, ${y})`);
9+
browser.executeScript(`return window.scrollTo(${x}, ${y});`);
10+
return browser.wait(() =>
11+
this.getScrollYPosition()
12+
.then((posY) => posY === y));
1013
}
1114

1215
setWindowSize(x: number, y: number) {
13-
return browser.driver.manage().window().setSize(x, y);
16+
browser.driver.manage().window().setSize(x, y);
17+
return browser.wait(() =>
18+
this.getWindowSize()
19+
.then((size: any) => size.height === y && size.width === x));
20+
}
21+
22+
getWindowSize() {
23+
return browser.executeScript(`return { height: window.outerHeight, width: window.outerWidth };`);
24+
}
25+
26+
getScrollYPosition() {
27+
return browser.executeScript('return window.pageYOffset;');
1428
}
1529

1630
getImageTopLoaderComp() {

protractor.conf.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ exports.config = {
1919
framework: 'jasmine',
2020
jasmineNodeOpts: {
2121
showColors: true,
22-
defaultTimeoutInterval: 30000,
22+
defaultTimeoutInterval: 10000,
2323
print: function() {}
2424
},
2525
onPrepare() {

src/app/app.component.scss

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,7 @@
99

1010
:host ::ng-deep .img {
1111
transition: all .35s ease-in-out;
12-
width: 150px;
13-
}
14-
15-
@media (min-width: 768px) {
16-
:host ::ng-deep .img {
17-
width: 500px;
18-
}
12+
width: 100%;
1913
}
2014

2115
:host ::ng-deep .sn-image-not-loaded .img {

src/app/app.component.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,23 @@ export class AppComponent {
1515
];
1616

1717
image: ResponsiveImage = {
18-
placeholder: 'http://via.placeholder.com/35x15?text=placeholder',
19-
fallback: 'http://via.placeholder.com/350x150?text=fallback',
18+
placeholder: 'http://via.placeholder.com/40x40?text=placeholder',
19+
fallback: 'http://via.placeholder.com/400x400?text=fallback',
2020
images: [
2121
{
2222
size: 'xs',
23-
x1: 'http://via.placeholder.com/150x350?text=xs+1x',
24-
x2: 'http://via.placeholder.com/300x700?text=xs+2x'
23+
x1: 'http://via.placeholder.com/400x400?text=xs+1x',
24+
x2: 'http://via.placeholder.com/800x800?text=xs+2x'
2525
},
2626
{
2727
size: 'md',
28-
x1: 'http://via.placeholder.com/350x250?text=md+1x',
29-
x2: 'http://via.placeholder.com/700x500?text=md+2x'
28+
x1: 'http://via.placeholder.com/768x400?text=md+1x',
29+
x2: 'http://via.placeholder.com/1536x800?text=md+2x'
3030
},
3131
{
3232
size: 'lg',
33-
x1: 'http://via.placeholder.com/700x400?text=lg+1x',
34-
x2: 'http://via.placeholder.com/1400x800?text=lg+2x'
33+
x1: 'http://via.placeholder.com/1024x400?text=lg+1x',
34+
x2: 'http://via.placeholder.com/2048x800?text=lg+2x'
3535
}
3636
]
3737
};

0 commit comments

Comments
 (0)