Skip to content

Commit

Permalink
[ImageLoader] Call failure callback from getSize when image decoding …
Browse files Browse the repository at this point in the history
…fails
  • Loading branch information
lkinasiewicz committed Jan 14, 2025
1 parent a46e085 commit f8ddd30
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ describe('ImageLoader', () => {
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 {
Expand Down Expand Up @@ -67,3 +77,9 @@ class NotLoadingMockImage extends MockImage {
window.setTimeout(this.onerror, 0);
}
}

class NotDecodingMockImage extends MockImage {
decode() {
return Promise.reject();
}
}
2 changes: 2 additions & 0 deletions packages/react-native-web/src/modules/ImageLoader/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ const ImageLoader = {
const { naturalHeight, naturalWidth } = image;
if (naturalHeight && naturalWidth) {
success(naturalWidth, naturalHeight);
} else {
errorCallback();
}
}
}
Expand Down

0 comments on commit f8ddd30

Please sign in to comment.