'use strict' // src/ui-groups-sidebar.js 的行为测试(此前该组件完全无测试)。 // // 用 vm + 极简 React/ReactDOM 假体驱动组件:右键卡片 → groupMenu 置位 → 重渲染 → ReactDOM.createPortal // 渲染「移动到分组」菜单 → 点「未分组」→ onSavedGroupsChange/onFlash 被调用。 // 注:真实环境里 ReactDOM 由 vendor 脚本提供为浏览器全局;本测试以假体注入,只验证组件自身接线。 const assert = require('assert') const fs = require('fs') const path = require('path') const vm = require('vm') const root = path.resolve(__dirname, '..') const loadComp = () => { const states = [] const refs = [] let cursor = 0 let rerender = null const fakeReact = { createElement: (type, props, ...children) => ({ __el: true, type, props: props || {}, children: children.flat() }), useState: (init) => { const i = cursor++ if (!(i in states)) states[i] = (typeof init === 'function') ? init() : init return [states[i], (v) => { states[i] = (typeof v === 'function') ? v(states[i]) : v; if (rerender) rerender() }] }, useEffect: () => {}, // 不执行副作用(本测试不需要 document 监听) useRef: (init) => { const i = cursor++; if (!(i in refs)) refs[i] = { current: init === undefined ? null : init }; return refs[i] }, } const portals = [] const fakeReactDOM = { createPortal: (child, container) => { portals.push({ child, container }); return { __el: true, type: '__portal__', props: {}, children: [child] } } } const sandbox = { React: fakeReact, ReactDOM: fakeReactDOM, document: { body: { __body: true }, addEventListener: () => {}, removeEventListener: () => {} }, console, confirm: () => true, } // 组件里用的是 bare `window.confirm`(删除分组前确认)⇒ 沙箱必须提供 window 自引用, // 否则一走到 removeGroup 就 ReferenceError(此前没有用例触发过「删除分组」)。 sandbox.window = sandbox vm.runInNewContext(fs.readFileSync(path.join(root, 'src', 'ui-groups-sidebar.js'), 'utf8'), sandbox) const Comp = sandbox.IWBUIGroupsSidebar let tree = null const render = (props) => { cursor = 0; tree = Comp(props) } rerender = () => render(render.props) return { render: (props) => { render.props = props; render(props) }, portals, getTree: () => tree } } const walk = (node, fn) => { if (!node || typeof node !== 'object') return if (Array.isArray(node)) { node.forEach((n) => walk(n, fn)); return } if (node.__el) { fn(node); (node.children || []).forEach((c) => walk(c, fn)) } } assert.strictEqual(typeof vm.runInNewContext, 'function') const created = [] const flashed = [] const app = loadComp() const props = { embedded: true, savedGroups: [{ id: 'g1', name: '收藏组1', group: '' }], nodeGroupGroups: [{ id: 'x', name: '分组X' }], api: () => Promise.resolve(), I: () => null, onSavedGroupsChange: (list) => created.push(list), onFlash: (m) => flashed.push(m), } // 初次渲染:groupMenu 为 null ⇒ 不应有 portal app.render(props) assert.strictEqual(app.portals.length, 0, '未右键时不应渲染「移动到分组」菜单') assert.ok(app.getTree(), '组件应返回元素树') // 右键卡片 → 打开分组菜单(此路径此前因 ReactDOM 未绑定而抛 ReferenceError) let card = null walk(app.getTree(), (n) => { if (!card && typeof n.props.className === 'string' && n.props.className.indexOf('iwb-pcard') === 0) card = n }) assert.ok(card, '应能定位到节点组卡片') card.props.onContextMenu({ preventDefault() {}, stopPropagation() {}, clientX: 30, clientY: 40 }) assert.strictEqual(app.portals.length, 1, '右键后应经 ReactDOM.createPortal 渲染菜单') assert.ok(app.portals[0].container && app.portals[0].container.__body === true, 'portal 容器应为 document.body') // 菜单里应含「移动到分组」标题与「未分组」按钮(只在 portal 子树里找,避免撞上分类芯片) let menuBtn = null walk(app.portals[0].child, (n) => { if (!menuBtn && n.type === 'button' && n.children[0] === '未分组') menuBtn = n }) assert.ok(menuBtn, '菜单应含「未分组」按钮') // 点「未分组」→ 与分组解绑并回调 menuBtn.props.onClick() assert.strictEqual(created.length, 1, 'assignNodeGroup 应触发 onSavedGroupsChange') assert.strictEqual(created[0][0].group, '', '应把该组移到未分组') assert.strictEqual(flashed.join(','), '已移动到未分组') // ── 失败必须可见(2026-11-12)── // 两条保存路径都要在失败时给出 **err 级** 提示: // ① saveList:组件把失败回调交给 app.js(那边判 r.ok===false 后调用),文案必须是 err; // ② saveGroups:本组件自己发 api('groups'),失败是 resolve({ok:false}) 而不是 reject。 const tick = () => new Promise((r) => setTimeout(r, 0)) ;(async () => { // ① saveList 的失败回调 → onFlash(..., 'err') { const flashes = [] let onErr = null const a = loadComp() a.render({ embedded: true, savedGroups: [{ id: 'g1', name: '收藏组1', group: '分组X' }], nodeGroupGroups: [{ id: 'x', name: '分组X' }], api: () => Promise.resolve(), I: () => null, onSavedGroupsChange: (list, cb) => { onErr = cb }, onFlash: (m, ty) => flashes.push([m, ty]), }) let card = null walk(a.getTree(), (n) => { if (!card && typeof n.props.className === 'string' && n.props.className.indexOf('iwb-pcard') === 0) card = n }) card.props.onContextMenu({ preventDefault() {}, stopPropagation() {}, clientX: 10, clientY: 10 }) let btn = null for (const p of a.portals) walk(p.child, (n) => { if (!btn && n.type === 'button' && n.children[0] === '未分组') btn = n }) btn.props.onClick() assert.ok(typeof onErr === 'function', 'saveList 应把失败回调交给 app.js(否则失败永远显示不出来)') onErr() assert.ok(flashes.some((f) => f[1] === 'err' && /保存失败/.test(f[0])), '节点组保存失败必须是 err 级提示(实际:' + JSON.stringify(flashes) + ')') } // ② saveGroups:api 返回 {ok:false} → onFlash(..., 'err') { const flashes = [] const a = loadComp() a.render({ embedded: true, savedGroups: [{ id: 'g1', name: '收藏组1', group: '' }], nodeGroupGroups: [{ id: 'x', name: '分组X' }], api: () => Promise.resolve({ ok: false, error: '节点分组保存失败:浏览器本地存储空间不足。' }), I: () => null, onSavedGroupsChange: () => {}, onNodeGroupGroupsChange: () => {}, onFlash: (m, ty) => flashes.push([m, ty]), }) let mgr = null // 注意:同排还有「+ 新建分组」也是 iwb-chip-add ⇒ 必须按 title 精确区分 walk(a.getTree(), (n) => { if (!mgr && typeof n.props.title === 'string' && n.props.title.indexOf('管理分组') === 0) mgr = n }) assert.ok(mgr, '应能找到「管理分组」入口') mgr.props.onClick() let del = null // 管理分组弹窗是**内联**渲染(不经 portal),所以要在整棵树里找 walk(a.getTree(), (n) => { if (!del && n.type === 'button' && typeof n.props.title === 'string' && n.props.title.indexOf('删除分组') === 0) del = n }) assert.ok(del, '管理分组弹窗应含「删除分组」按钮') del.props.onClick() await tick() assert.ok(flashes.some((f) => f[1] === 'err' && /保存失败/.test(f[0])), '删除分组后保存失败必须给 err 提示(实际:' + JSON.stringify(flashes) + ')') } console.log('ui-groups-sidebar checks passed') })().catch((e) => { console.error('❌ ' + ((e && e.stack) || e)); process.exit(1) })