Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ImageLoader] Simplify getSize implementation, call failure callback when decoding fails #2750

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import ImageLoader from '../index';

const testImage =
'data:image/webp;base64,UklGRkYAAABXRUJQVlA4IDoAAADwAgCdASoXABMAPi0QhkKhoQ36AAwBYllAHYAAajokAAD+/SFF//G83mta3//9QZ/5Bn/kGfp4AAAA';
const testImageWidth = 23;
const testImageHeight = 19;

const DefaultImage = window.Image;

describe('ImageLoader', () => {
afterEach(() => {
window.Image = DefaultImage;
});

test('Success callback is called when image loads', async () => {
window.Image = MockImage;
const successCallback = jest.fn();
const failureCallback = jest.fn();
ImageLoader.getSize(testImage, successCallback, failureCallback);
await jest.runAllTimers();
expect(failureCallback).toHaveBeenCalledTimes(0);
expect(successCallback).toHaveBeenCalledTimes(1);
expect(successCallback).toHaveBeenCalledWith(
testImageWidth,
testImageHeight
);
});

test('Failure callback is called when image fails to load', async () => {
window.Image = NotLoadingMockImage;
const successCallback = jest.fn();
const failureCallback = jest.fn();
ImageLoader.getSize(testImage, successCallback, failureCallback);
await jest.runAllTimers();
expect(failureCallback).toHaveBeenCalledTimes(1);
expect(successCallback).toHaveBeenCalledTimes(0);
});

test('Failure callback is called when image fails to decode', async () => {
window.Image = NotDecodingMockImage;
const successCallback = jest.fn();
const failureCallback = jest.fn();
ImageLoader.getSize(testImage, successCallback, failureCallback);
await jest.runAllTimers();
expect(failureCallback).toHaveBeenCalledTimes(1);
expect(successCallback).toHaveBeenCalledTimes(0);
});
});

class MockImage {
constructor(width = 0, height = 0) {
this.width = width;
this.height = height;
this.naturalWidth = 0;
this.naturalHeight = 0;
this._src = '';
}
get src() {
return this._src;
}
set src(uri) {
this._src = uri;
window.setTimeout(this.onload, 0);
}
decode() {
this.naturalWidth = testImageWidth;
this.naturalHeight = testImageHeight;
return Promise.resolve();
}
onerror() {}
onload() {}
}

class NotLoadingMockImage extends MockImage {
set src(uri) {
this._src = uri;
window.setTimeout(this.onerror, 0);
}
}

class NotDecodingMockImage extends MockImage {
decode() {
return Promise.reject();
}
}
12 changes: 2 additions & 10 deletions packages/react-native-web/src/modules/ImageLoader/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,31 +88,23 @@ const ImageLoader = {
success: (width: number, height: number) => void,
failure: () => void
) {
let complete = false;
const interval = setInterval(callback, 16);
const requestId = ImageLoader.load(uri, callback, errorCallback);

function callback() {
const image = requests[`${requestId}`];
if (image) {
const { naturalHeight, naturalWidth } = image;
if (naturalHeight && naturalWidth) {
success(naturalWidth, naturalHeight);
complete = true;
} else {
errorCallback();
}
}
if (complete) {
ImageLoader.abort(requestId);
clearInterval(interval);
}
}

function errorCallback() {
if (typeof failure === 'function') {
failure();
}
ImageLoader.abort(requestId);
clearInterval(interval);
}
},
has(uri: string): boolean {
Expand Down