Skip to content

Commit a7655c7

Browse files
committed
feat: support mock image loaded with size
1 parent 0be790e commit a7655c7

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

jest.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ module.exports = {
66
'^.+\\.tsx?$': 'ts-jest',
77
},
88
transformIgnorePatterns: ['/node_modules/'],
9+
coveragePathIgnorePatterns: ['/tests/unit/'],
910
moduleNameMapper: {
1011
'^@/(.*)$': '<rootDir>/src/$1',
1112
},

tests/unit/util.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,21 @@ export namespace mockImageOnload {
7979
const originalImgProtoDesc = Object.getOwnPropertyDescriptor(
8080
window.Image.prototype,
8181
'src'
82-
) as PropertyDecorator
82+
) as PropertyDescriptor
8383

8484
const mockImgProtoDesc = {
8585
set(this: HTMLImageElement, value: string) {
86+
if (originalImgProtoDesc.set) {
87+
originalImgProtoDesc.set.call(this, value)
88+
}
8689
loadedImgs.add(value)
8790
this.dispatchEvent(new CustomEvent('load'))
91+
const [, width = 0, height = 0] =
92+
value.match(/https:\/\/placeimg.com\/(\d+)\/(\d+)\/any/) || []
93+
this.width = +width
94+
this.height = +height
8895
},
89-
}
96+
} as PropertyDescriptor
9097

9198
const setImgProtoSrc = (enableMock: boolean) => {
9299
const desc = enableMock ? mockImgProtoDesc : originalImgProtoDesc

0 commit comments

Comments
 (0)