Skip to content

Commit

Permalink
test(image): add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
LeeJim committed Aug 31, 2022
1 parent 4a5f5a9 commit 716a777
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 39 deletions.
60 changes: 60 additions & 0 deletions src/image/__test__/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`image :base 1`] = `
<main>
<t-image>
<wx-view
class="t-class t-image t-image__mask t-image--faild t-image--shape-square"
>
<wx-view
class="t-class-load"
style="font-size: 40rpx; color: #999"
>
<t-icon>
<wx-label
class="t-class t-icon t-icon-close t-icon-base"
style=";;"
bind:tap="onTap"
/>
</t-icon>
</wx-view>
</wx-view>
<wx-image
class="t-class t-image t-image--shape-square"
hidden="{{true}}"
id="image"
lazyLoad="{{false}}"
mode="scaleToFill"
showMenuByLongpress="{{false}}"
src=""
style=""
webp="{{false}}"
bind:error="onLoadError"
bind:load="onLoaded"
/>
</t-image>
</main>
`;

exports[`image :success 1`] = `
<main>
<t-image
id="root"
bind:load="handleLoad"
>
<wx-image
class="t-class t-image t-image--shape-square"
hidden="{{false}}"
id="image"
lazyLoad="{{false}}"
mode="heightFix"
showMenuByLongpress="{{false}}"
src="https://www.tencent.com/img/index/menu_logo.png"
style="width: 0.00px;"
webp="{{false}}"
bind:error="onLoadError"
bind:load="onLoaded"
/>
</t-image>
</main>
`;
65 changes: 65 additions & 0 deletions src/image/__test__/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import simulate from 'miniprogram-simulate';
import path from 'path';

describe('image', () => {
const image = simulate.load(path.resolve(__dirname, `../image`), 't-image', {
less: true,
rootPath: path.resolve(__dirname, '../..'),
});

it(':base', () => {
const id = simulate.load({
template: `<t-image></t-image>`,
usingComponents: {
't-image': image,
},
});
const comp = simulate.render(id);
comp.attach(document.createElement('parent-wrapper'));

expect(comp.toJSON()).toMatchSnapshot();
});

it(':success', async () => {
const handleLoad = jest.fn();
const id = simulate.load({
template: `<t-image
id="root"
mode="{{mode}}"
src="https://www.tencent.com/img/index/menu_logo.png"
bind:load="handleLoad"
/>`,
data: {
mode: 'widthFix',
},
methods: {
handleLoad,
},
usingComponents: {
't-image': image,
},
});
const comp = simulate.render(id);
comp.attach(document.createElement('parent-wrapper'));

const $image = comp.querySelector('#root >>> #image');

$image.dispatchEvent('load', { detail: { width: 100, height: 100 } });

await simulate.sleep();

expect(handleLoad).toHaveBeenCalled();

// height fixed
const mock = jest.spyOn(wx, 'getSystemInfoSync');
mock.mockImplementation(() => ({ SDKVersion: '2.10.2' }));

comp.setData({ mode: 'heightFix' });
$image.dispatchEvent('load', { detail: { width: 100, height: 100 } });

await simulate.sleep();
expect(comp.toJSON()).toMatchSnapshot();

mock.mockRestore();
});
});
74 changes: 37 additions & 37 deletions src/image/image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,17 @@ export default class Image extends SuperComponent {
},
};

onLoaded(e: any) {
const sdkVersion = wx.getSystemInfoSync().SDKVersion;
const versionArray = sdkVersion.split('.').map((v) => parseInt(v, 10));
// 版本号低于2.10.3时组件内部实现heightFix模式
if (
versionArray[0] < 2 ||
(versionArray[0] === 2 && versionArray[1] < 10) ||
(versionArray[0] === 2 && versionArray[1] === 10 && versionArray[2] < 3)
) {
const mode = this.properties.mode as any as string;
if (mode === 'heightFix') {
methods = {
onLoaded(e: any) {
const sdkVersion = wx.getSystemInfoSync().SDKVersion;
const versionArray = sdkVersion.split('.').map((v) => parseInt(v, 10));
const { mode } = this.properties;
const isInCompatible =
versionArray[0] < 2 ||
(versionArray[0] === 2 && versionArray[1] < 10) ||
(versionArray[0] === 2 && versionArray[1] === 10 && versionArray[2] < 3);
// 版本号低于2.10.3时组件内部实现heightFix模式
if (mode === 'heightFix' && isInCompatible) {
// 实现heightFix模式,保持高度和宽高比,设置对应的宽度
const { height: picHeight, width: picWidth } = e.detail;
const query = this.createSelectorQuery();
Expand All @@ -60,33 +60,33 @@ export default class Image extends SuperComponent {
})
.exec();
}
}
this.setData({
isLoading: false,
isFailed: false,
});
this.triggerEvent('load', e.detail);
}

onLoadError(e: any) {
this.setData({
isLoading: false,
isFailed: true,
});
this.triggerEvent('error', e.detail);
}

update() {
const { src } = this.properties as any;
this.preSrc = src;
if (!src) {
// 链接为空时直接触发加载失败
this.onLoadError({ errMsg: '图片链接为空' });
} else {
this.setData({
isLoading: true,
isLoading: false,
isFailed: false,
});
}
}
this.triggerEvent('load', e.detail);
},

onLoadError(e: any) {
this.setData({
isLoading: false,
isFailed: true,
});
this.triggerEvent('error', e.detail);
},

update() {
const { src } = this.properties as any;
this.preSrc = src;
if (!src) {
// 链接为空时直接触发加载失败
this.onLoadError({ errMsg: '图片链接为空' });
} else {
this.setData({
isLoading: true,
isFailed: false,
});
}
},
};
}
4 changes: 2 additions & 2 deletions src/image/image.wxml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
mode="{{mode}}"
webp="{{webp}}"
lazy-load="{{lazy}}"
bindload="onLoaded"
binderror="onLoadError"
bind:load="onLoaded"
bind:error="onLoadError"
show-menu-by-longpress="{{showMenuByLongpress}}"
/>

0 comments on commit 716a777

Please sign in to comment.