From 716a777aa5b07181c8406c374f8c6649b6b8e8c7 Mon Sep 17 00:00:00 2001 From: leejimqiu Date: Wed, 31 Aug 2022 15:13:51 +0800 Subject: [PATCH] test(image): add unit test --- .../__test__/__snapshots__/index.test.js.snap | 60 +++++++++++++++ src/image/__test__/index.test.js | 65 ++++++++++++++++ src/image/image.ts | 74 +++++++++---------- src/image/image.wxml | 4 +- 4 files changed, 164 insertions(+), 39 deletions(-) create mode 100644 src/image/__test__/__snapshots__/index.test.js.snap create mode 100644 src/image/__test__/index.test.js diff --git a/src/image/__test__/__snapshots__/index.test.js.snap b/src/image/__test__/__snapshots__/index.test.js.snap new file mode 100644 index 000000000..c7c44bc84 --- /dev/null +++ b/src/image/__test__/__snapshots__/index.test.js.snap @@ -0,0 +1,60 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`image :base 1`] = ` +
+ + + + + + + + + +
+`; + +exports[`image :success 1`] = ` +
+ + +
+`; diff --git a/src/image/__test__/index.test.js b/src/image/__test__/index.test.js new file mode 100644 index 000000000..893213e8f --- /dev/null +++ b/src/image/__test__/index.test.js @@ -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: ``, + 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: ``, + 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(); + }); +}); diff --git a/src/image/image.ts b/src/image/image.ts index f09460fd1..66551ccd3 100644 --- a/src/image/image.ts +++ b/src/image/image.ts @@ -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(); @@ -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, + }); + } + }, + }; } diff --git a/src/image/image.wxml b/src/image/image.wxml index 6282b7af7..075cc22bd 100644 --- a/src/image/image.wxml +++ b/src/image/image.wxml @@ -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}}" />