Skip to content

Commit

Permalink
Merge pull request #25 from imagekit-developer/feature/lazy-load-tests
Browse files Browse the repository at this point in the history
Code Coverage for lazyload and LQIP features
  • Loading branch information
imagekitio authored Nov 2, 2020
2 parents 77845df + b4f8a8c commit 0fd2fee
Show file tree
Hide file tree
Showing 5 changed files with 365 additions and 32 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "imagekitio-react",
"version": "1.0.8",
"version": "1.0.9",
"description": "React SDK for ImageKit.io which implements client-side upload and URL generation for use inside a react application.",
"scripts": {
"build": "rm -rf dist*;rollup -c",
Expand All @@ -25,6 +25,9 @@
"verbose": true,
"setupFilesAfterEnv": [
"<rootDir>/src/test/setupTests.js"
],
"coveragePathIgnorePatterns": [
"<rootDir>/src/test/mocks"
]
},
"author": "ImageKit",
Expand Down
21 changes: 12 additions & 9 deletions src/components/IKImage/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,9 @@ class IKImage extends ImageKitComponent {
triggerOriginalImageLoad() {
var img = new Image();
img.onload = () => {
this.setState({ originalSrcLoaded: true });
this.updateImageUrl();
this.setState({ originalSrcLoaded: true }, () => {
this.updateImageUrl();
});
}
img.src = this.state.originalSrc;
}
Expand All @@ -136,10 +137,11 @@ class IKImage extends ImageKitComponent {
const imageObserver = new IntersectionObserver(entries => {
const el = entries[0];
if (el && el.isIntersecting) {
this.setState({ intersected: true });
if (lqip && lqip.active) this.triggerOriginalImageLoad();
imageObserver.disconnect();
this.updateImageUrl();
this.setState({ intersected: true }, () => {
if (lqip && lqip.active) this.triggerOriginalImageLoad();
imageObserver.disconnect();
this.updateImageUrl();
});
}
}, {
rootMargin: `${rootMargin} 0px ${rootMargin} 0px`
Expand All @@ -150,9 +152,10 @@ class IKImage extends ImageKitComponent {
})
} else {
// Load original image right away
this.setState({ intersected: true });
if (lqip && lqip.active) this.triggerOriginalImageLoad();
this.updateImageUrl();
this.setState({ intersected: true }, () => {
if (lqip && lqip.active) this.triggerOriginalImageLoad();
this.updateImageUrl();
});
}
}

Expand Down
Loading

0 comments on commit 0fd2fee

Please sign in to comment.