Skip to content
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
7 changes: 6 additions & 1 deletion src/graphic/helper/image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,12 @@ export function createOrUpdateImage<T>(

if (cachedImgObj) {
image = cachedImgObj.image;
!isImageReady(image) && cachedImgObj.pending.push(pendingWrap);
if (isImageReady(image)) {
onload && onload(image, cbPayload);
}
else {
cachedImgObj.pending.push(pendingWrap);
}
}
else {
image = platformApi.loadImage(
Expand Down
37 changes: 37 additions & 0 deletions test/ut/spec/graphic/Image.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,29 @@
import {createOrUpdateImage} from '../../../../src/graphic/helper/image';
import {Image as ZRImage} from '../zrender';

class HTMLImageElement {
width: number
height: number
src?: string

constructor() {}
}

class Image {
width?: number
height?: number
onload?: () => void

constructor(width?: number, height?: number, onload?: () => void) {
this.width = width || 1;
this.height = height || 1;
this.onload = onload;
setTimeout(() => {
this.onload?.();
}, 100);
}
}

describe('Image', function () {

it('Should get width and height from style by default', function () {
Expand Down Expand Up @@ -62,4 +79,24 @@ describe('Image', function () {
expect(rect.height).toBe(200);
});

it('Should trigger `onload` event even if hit cache', function (done) {
const imgSource = new HTMLImageElement();
imgSource.width = 100;
imgSource.height = 50;
imgSource.src = '#';
const mockOnload = jest.fn();
const fakeHostEl = {
dirty: () => {}
};

// 模拟测试Image加载
(global as any).Image = Image;
createOrUpdateImage(imgSource.src, imgSource as any, fakeHostEl, mockOnload);
createOrUpdateImage(imgSource.src, imgSource as any, fakeHostEl, mockOnload);

setTimeout(() => {
expect(mockOnload).toHaveBeenCalledTimes(2);
done();
}, 200);
});
});