/** * @fileoverview 单元测试 */ const path = require('path') const simulate = require('miniprogram-simulate') const html = require('./content') // 测试 html const dist = '../dev/mp-weixin/components/mp-html/index' // 组件目录 const mpHtml = simulate.load(path.resolve(__dirname, dist), 'mp-html') // 渲染测试 test('render', async () => { // 创建和渲染页面 const id = simulate.load({ data: { containerStyle: '', copyLink: true, lazyLoad: true, pauseVideo: true, previewImg: true, useAnchor: true }, template: ` 加载中... `, usingComponents: { 'mp-html': mpHtml } }) const page = simulate.render(id) // 设置数据 page.setData({ html }) await simulate.sleep(1000) const comp = page.querySelector('#article') expect(comp.dom.tagName).toBe('MP-HTML') await simulate.sleep(50) // api 测试 comp.instance.setContent( `
xxx
标题1 标题2
内容1
图片 图片
更多 1 < 2 &#aaa;&aaa;& 11
var i = 0

123
123

`, true) // 补充测试 expect(comp.instance.getText().includes('更多')).toBe(true) // 检查上方的实体是否被解码 await comp.instance.getRect() await comp.instance.navigateTo('anchor') // 基于页面跳转 comp.instance.in(page.instance) // 错误设置 comp.instance.in(page.instance, '#scroll', 'top') await comp.instance.navigateTo('anchor') // 基于 scroll-view 滚动 page.setData({ useAnchor: false }) await simulate.sleep(50) comp.instance.setContent('123', true) try { await comp.instance.navigateTo('anchor') // 禁用锚点的情况下跳转 } catch (e) { } page.setData({ containerStyle: 'white-space:pre-wrap' }) await simulate.sleep(50) comp.instance.setContent(' 空格\n换行') expect(comp.instance.getText().includes('\n')).toBe(true) // 检查换行是否被保留 // 无图测试 page.setData({ lazyLoad: false }) await simulate.sleep(50) comp.instance.setContent('
Hello world!
') simulate.sleep(50) // 长内容测试 let content = '
1
'.repeat(50) + '
' for (let i = 0; i < 50; i++) { content += '
' + i + '
' } content += 'xxx' for (let i = 0; i < 3; i++) { content += '
' + i + '
' } comp.instance.setContent(content) simulate.sleep(50) expect(comp.data.nodes.length).toBe(2) // 应该切分为 2 块 expect(comp.data.nodes[1].children.length).toBe(5) // 应该切分为 5 块 await simulate.sleep(50) // 等待异步 api 执行完毕 // 移除节点 comp.triggerLifeTime('detached') }) // 事件测试 test('event', async () => { // 模拟 api wx.createVideoContext = function () { // 模拟视频 context return { pause: function () { }, playbackRate: function () { } } } // 测试失败回调 wx.navigateTo = function (obj) { setTimeout(() => { if (typeof obj.fail === 'function') { obj.fail() } }, 0) } wx.switchTab = function (obj) { setTimeout(() => { if (typeof obj.fail === 'function') { obj.fail() } }, 0) } const comp = simulate.render(mpHtml) comp.setData({ selectable: 'force', loadingImg: 'xxx' }) await simulate.sleep(50) comp.instance.setContent( ` 链接2 链接3 `) await simulate.sleep(100) const node = comp.querySelector('#_root') node.triggerLifeTime('attached') comp.instance._add({ detail: node.instance }) // 模拟图片加载完毕 for (let i = 0; i <= 1; i++) { node.instance.imgLoad({ target: { dataset: { i: i.toString() } }, detail: { width: 100, height: 100 } }) // 模拟图片被点击 node.instance.imgTap({ target: { dataset: { i: i.toString() } } }) } comp.setData({ loadingImg: '' }) await simulate.sleep(350) node.instance.imgLoad({ target: { dataset: { i: '1' } } }) // 模拟图片链接被点击 node.instance.imgTap({ target: { dataset: { i: '2_0' } } }) node.instance.noop() // 模拟图片出错 const imgError = () => node.instance.mediaError({ target: { dataset: { i: '0' } }, detail: { errMsg: 'test' } }) imgError() comp.setData({ errorImg: 'xxx' }, imgError) // 模拟链接被点击 for (let i = 2; i <= 4; i++) { node.instance.linkTap({ currentTarget: { dataset: { i: i.toString() } } }) } // 模拟视频播放 for (let i = 0; i < 3; i++) { node.instance.play({ target: { id: 'v' + (i % 2) } }) } // 视频倍速播放 comp.instance.setPlaybackRate(1.5) node.instance.play({ target: { id: 'v2' } }) // 暂停视频播放 comp.instance.pauseMedia() // 模拟视频出错 node.instance.mediaError({ target: { dataset: { i: '6' } }, detail: { errMsg: 'test' } }) // 禁用一些功能 comp.setData({ copyLink: false, pauseVideo: false, previewImg: false }, () => { // 禁用自动拷贝后点击外部链接 node.instance.linkTap({ currentTarget: { dataset: { i: '3' } } }) // 禁用自动暂停后播放视频 node.instance.play({ target: { id: 'v0' } }) // 禁用预览后点击图片 node.instance.imgTap({ target: { dataset: { i: '0' } } }) }) await simulate.sleep(100) })