Files
image-workbench-desktop/tests/plugin-grouplib-render-test.js
T

107 lines
5.5 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'
// 节点组库插件渲染回归(grouplib 1.0.0)
//
// 守三件事:① 列表/分组胶囊/卡片渲染;② 改名·删除·移动分组都经 ctx.grouplib 落盘(插件不留副本);
// ③ 卡片拖拽必须带 application/x-iwb-group(画布 drop 靠它实例化)。
const assert = require('assert')
const fs = require('fs')
const path = require('path')
const vm = require('vm')
const { JSDOM } = require('jsdom')
const src = fs.readFileSync(path.resolve(__dirname, '..', '..', 'iwb-plugins-desktop', 'grouplib', 'src', 'index.js'), 'utf8')
const wait = (ms) => new Promise((r) => setTimeout(r, ms))
;(async () => {
const dom = new JSDOM('<!doctype html><html><body><div id="host"></div></body></html>', { pretendToBeVisual: true })
const { window } = dom
window.confirm = () => true
let registered = null
window.IWBPluginHost = { register (manifest, factory) { registered = { manifest, factory } } }
const sandbox = { window, document: window.document, console, setTimeout, clearTimeout, Promise, Object, Array, JSON, Math, Date, String, Number }
vm.runInNewContext(src, sandbox)
assert.ok(registered, '插件应注册')
assert.strictEqual(registered.manifest.id, 'grouplib')
assert.ok(registered.manifest.capabilities.indexOf('grouplib') >= 0, '应声明 grouplib 能力')
let groups = [
{ id: 'g1', name: '海报组', group: '电商', nodes: [] },
{ id: 'g2', name: '白底组', group: '', nodes: [] },
]
let groupGroups = [{ id: '电商', name: '电商' }]
const calls = { save: 0, remove: [], instantiate: [], notify: [] }
let mounted = null
const ctx = {
ui: { registerPanel (p) { mounted = p.mount } },
grouplib: {
list: async () => ({ groups: groups.slice(), groupGroups: groupGroups.slice() }),
save: async (payload) => { calls.save++; groups = payload.groups.slice(); groupGroups = payload.groupGroups.slice(); return { ok: true } },
remove: async (id) => { calls.remove.push(id); groups = groups.filter((g) => g.id !== id); return { ok: true } },
instantiate: async (id) => { calls.instantiate.push(id); return { ok: true } },
notify: (t, ty) => { calls.notify.push([t, ty]) },
onChanged: () => () => {},
},
}
registered.factory(ctx)
assert.ok(mounted, '应注册面板 mount')
const host = window.document.getElementById('host')
mounted(host, ctx)
await wait(50)
const cards = () => Array.from(host.querySelectorAll('.iwb-pcard'))
assert.strictEqual(cards().length, 2, '应渲染两个节点组卡片,实际 ' + cards().length)
const chips = Array.from(host.querySelectorAll('.iwb-chip')).map((c) => c.textContent)
assert.ok(chips.some((t) => t.indexOf('全部') === 0 && /2$/.test(t)), '应有「全部 2」胶囊:' + chips.join('|'))
assert.ok(chips.some((t) => t.indexOf('未分组') === 0 && /1$/.test(t)), '应有「未分组 1」胶囊:' + chips.join('|'))
// 分组筛选:点「未分组」只剩白底组
const noneChip = Array.from(host.querySelectorAll('.iwb-chip')).find((c) => c.textContent.indexOf('未分组') === 0)
noneChip.dispatchEvent(new window.MouseEvent('click', { bubbles: true }))
await wait(20)
assert.strictEqual(cards().length, 1, '筛选未分组后应只剩 1 张')
assert.ok(cards()[0].textContent.indexOf('白底组') >= 0, '剩下的应是白底组')
// 拖拽 payload:画布 drop 依赖 application/x-iwb-group(JSON 字符串)
const card = cards()[0]
const store = {}
const dragEvt = new window.Event('dragstart', { bubbles: true })
dragEvt.dataTransfer = { setData: (k, v) => { store[k] = v }, effectAllowed: '' }
card.dispatchEvent(dragEvt)
assert.strictEqual(store['application/x-iwb-groupid'], 'g2', '拖拽应带组 id')
assert.strictEqual(JSON.parse(store['application/x-iwb-group']), 'g2', '拖拽应带实例化 payload')
// 双击改名 → ctx.grouplib.save 落盘
card.dispatchEvent(new window.MouseEvent('dblclick', { bubbles: true }))
await wait(20)
const input = cards()[0] && cards()[0].querySelector('.iwb-pname-input')
assert.ok(input, '双击后应出现改名输入框')
input.value = '白底组-新'
input.dispatchEvent(new window.KeyboardEvent('keydown', { key: 'Enter', bubbles: true }))
await wait(50)
assert.ok(calls.save >= 1, '改名应经 save 落盘')
assert.strictEqual(groups.find((g) => g.id === 'g2').name, '白底组-新', '宿主侧名字应更新')
// 删除 → 走 remove(id)(不是本地删完就算)
const del = Array.from(host.querySelectorAll('.iwb-pbtns .iwb-btn-danger'))[0]
assert.ok(del, '卡片应有删除按钮')
del.dispatchEvent(new window.MouseEvent('click', { bubbles: true }))
await wait(60)
assert.deepStrictEqual(calls.remove, ['g2'], '删除应调用宿主 remove,实际 ' + JSON.stringify(calls.remove))
// 删掉最后一张 → 空态
Array.from(host.querySelectorAll('.iwb-chip')).forEach((c) => { if (c.textContent.indexOf('全部') === 0) c.dispatchEvent(new window.MouseEvent('click', { bubbles: true })) })
await wait(20)
const last = cards()[0]
assert.ok(last, '应还剩一张卡片')
last.querySelector('.iwb-pbtns .iwb-btn-danger').dispatchEvent(new window.MouseEvent('click', { bubbles: true }))
await wait(60)
assert.deepStrictEqual(calls.remove, ['g2', 'g1'], '两次删除都应走宿主 remove')
assert.ok(host.querySelector('.iwb-hint'), '无节点组时应给空态提示')
console.log('grouplib render checks passed(列表/筛选/拖拽 payload/改名落盘/删除走宿主)')
})().catch((e) => { console.error(e); process.exit(1) })