Files
image-workbench-desktop/tests/result-img-doubleclick-test.js
T
2026-12-02 17:47:59 +08:00

66 lines
2.9 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
'use strict'
// 结果图手势契约(2026-11-26):单击只选中、双击才预览。
// 用假 React 记录 createElement 的树,直接调用组件函数——不需要 jsdom/React 运行时,
// 断言的是「实际渲染出来的 props」,不是源码文本。
const assert = require('assert')
const fs = require('fs')
const path = require('path')
const vm = require('vm')
const root = path.resolve(__dirname, '..')
const React = {
createElement(type, props) {
const children = Array.prototype.slice.call(arguments, 2)
return { type, props: Object.assign({}, props || {}, { children }) }
},
useState(init) { return [init, function () {}] },
// 2026-12-02:ResultImg 用 useEffect 订阅落地状态,假 React 必须提供(否则 TypeError)
useEffect(fn) { try { const c = fn && fn(); if (typeof c === 'function') c() } catch (e) { /* ignore */ } },
}
const sandbox = { React }
vm.runInNewContext(fs.readFileSync(path.join(root, 'src', 'ui-components.js'), 'utf8'), sandbox)
const findImg = (tree) => {
if (!tree || typeof tree !== 'object') return null
if (tree.type === 'img') return tree
for (const c of (tree.props && tree.props.children) || []) {
const hit = findImg(c)
if (hit) return hit
}
return null
}
// 1) ResultImg:单击不预览,双击预览
let previewed = ''
const img = findImg(sandbox.IWBResultImg({ item: { url: 'https://x/a.png' }, onPreview: (u) => { previewed = u } }))
assert.ok(img, 'ResultImg 必须渲染出 img')
assert.strictEqual(typeof img.props.onDoubleClick, 'function', '双击必须绑定预览')
assert.strictEqual(typeof img.props.onClick, 'function', '单击仍需有处理器(阻止冒泡)')
img.props.onClick({ stopPropagation() {} })
assert.strictEqual(previewed, '', '单击不得触发预览')
img.props.onDoubleClick({ stopPropagation() {} })
assert.strictEqual(previewed, 'https://x/a.png', '双击必须带上图片 URL')
// 2) 共享下载浮层:按钮存在、点击回调正确、pointerdown 不冒泡到节点拖动
let downloaded = 0
let copied = 0
let stopped = 0
const ov = sandbox.IWBResultDownloadOverlay({
onDownload: () => { downloaded++ },
onCopyRef: () => { copied++ },
children: { type: 'img', props: {} },
})
// 2026-12-02:浮层由「底部横条」改为「右下角圆形图标容器」(iwb-dl-corner)
const bar = ov.props.children.find((c) => c && c.props && c.props.className === 'iwb-dl-corner')
assert.ok(bar, '浮层必须渲染右下角圆钮容器')
const btns = bar.props.children.filter(Boolean)
assert.strictEqual(btns.length, 2, '浮层应有复制 + 下载两个按钮')
btns[0].props.onClick({ stopPropagation() {} })
btns[1].props.onClick({ stopPropagation() {} })
btns[1].props.onPointerDown({ stopPropagation() { stopped++ } })
assert.strictEqual(copied, 1)
assert.strictEqual(downloaded, 1)
assert.strictEqual(stopped, 1, 'pointerdown 必须阻止冒泡(否则会拖动节点)')
console.log('result-img gestures: 全部通过')