Files
eip4-web/e2e/linkage-probe.spec.ts
zhengwr b578d8d41b feat(主子表): 引用驱动联动 向导映射 UI + 真事件 e2e
- NewFeatureShell: 主子表步③加「引用联动」combo(引用列 → 目标子列 ← 源实体列),
  childLinkage 透传 compile;构建时分支(不依赖 amis 运行时 step 跳步),页面 data seed []
- e2e/master-detail: 引用驱动联动用例——真后端 schema(GET /api/page/schema) + 真 picker
  数据 + 真 Playwright 事件填充 + 提交落库验证(选物料→带出单价/规格,用户未手填仍随
  _ADD 级联落库;引用 FK 本身也落库)
- e2e/linkage-probe: amis input-table 行内联动机制隔离验证(钉死 selectedItems 是对象非
  数组;远程 source 选项携整行列;多行 ${index} 按行隔离防串台)
- NewFeatureShell.test: 修过时断言(c735f24 类型选择卡后需先选类型才出 amis 挂载点)

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-20 22:28:45 +08:00

272 lines
13 KiB
TypeScript
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.
/**
* 隔离验证 · amis input-table 行内联动机制(真 Playwright 事件)。
*
* 交接首要指令:上一会话「引用驱动联动」后端方案做了但手搓浏览器 fill 没验成功(疑 `.click()` 没触发
* React onEvent / `${index}` componentId 替换)→ 全部回退。本 probe 不碰后端,用 page.evaluate 直接
* amis.embed 注入「手写最小 schema」,再用真 Playwright 鼠标/键盘事件驱动,钉死正确 schema 形态:
* ① 对照组:input-number → setValue componentId=amount_${index}(amis 官方范式 input-table.md:2536)。
* ② select(物料带 PRICE/SPEC 整行)→ onEvent.change → setValue price_${index}/spec_${index}
* = ${event.data.selectedItems[0].PRICE/SPEC}。
* 验证兄弟列是否真被填充。结论决定后端编译器怎么发 schema(id 挂列上 vs quickEdit)。
*
* 纯前端:select 用 static options 不打后端,故任意 vite(默认 config 反代 8093 即可,不依赖 8094)。
*/
import { test, expect } from '@playwright/test';
test('amis input-table 行内联动:input-number 与 select 都应填充兄弟列', async ({ page }) => {
const consoleErrors: string[] = [];
page.on('console', (m) => { if (m.type() === 'error') consoleErrors.push(m.text()); });
await page.goto('/login');
await page.waitForFunction(() => typeof (window as Window & { amisRequire?: unknown }).amisRequire === 'function',
null, { timeout: 30_000 });
// 注入一个覆盖层 host(不抹掉 React root,避免 React reconciliation 报错),embed 手写 schema。
await page.evaluate(() => {
const w = window as unknown as { amisRequire: (p: string) => any };
const host = document.createElement('div');
host.id = 'probe-host';
host.style.cssText =
'position:fixed;inset:0;z-index:99999;background:#fff;padding:24px;overflow:auto';
document.body.appendChild(host);
const amis = w.amisRequire('amis/embed');
const schema = {
type: 'page',
data: { rows: [{}] }, // 预置一行,免去点「新增」
body: {
type: 'form',
title: '行内联动验证',
wrapWithPanel: false,
body: [
{
type: 'input-table',
name: 'rows',
id: 'eg-rows',
addable: true,
removable: true,
needConfirm: false,
columns: [
{
type: 'input-number',
name: 'qty',
label: '数量',
onEvent: {
change: {
actions: [
{ actionType: 'setValue', componentId: 'amount_${index}', args: { value: '${qty * 10}' } },
],
},
},
},
// 目标列带「真实 type」(经 quickEdit),且 id 挂在列上 —— 镜像后端编译器对普通子列的渲染形态,
// 验证 id-on-column + 显式 quickEdit type 仍能被 setValue componentId 命中。
{ name: 'amount', id: 'amount_${index}', label: '金额', quickEdit: { type: 'input-number', name: 'amount' } },
{
type: 'select',
name: 'material',
label: '物料',
// 整行 option(PRICE/SPEC 作为额外列),模拟 picker/list 的 items(responseData options:${items})
options: [
{ label: '水泥', value: 'C1', PRICE: '10', SPEC: '袋装' },
{ label: '钢筋', value: 'S1', PRICE: '20', SPEC: '12mm' },
],
onEvent: {
change: {
// 单选 select 的 change 事件 selectedItems = 被选 Option 对象本身(非数组),故 .PRICE 直取。
// (Select.tsx changeValue: dispatchEvent('change',{value,options,items,selectedItems:value}))
actions: [
{ actionType: 'setValue', componentId: 'price_${index}', args: { value: '${event.data.selectedItems.PRICE}' } },
{ actionType: 'setValue', componentId: 'spec_${index}', args: { value: '${event.data.selectedItems.SPEC}' } },
],
},
},
},
{ name: 'price', id: 'price_${index}', label: '单价' },
{ name: 'spec', id: 'spec_${index}', label: '规格' },
],
},
{
type: 'tpl',
id: 'probe-out',
tpl: 'PROBEOUT amount=[${rows[0].amount}] price=[${rows[0].price}] spec=[${rows[0].spec}]',
className: 'probe-out-marker',
},
],
},
};
amis.embed(host, schema, {}, {});
});
const host = page.locator('#probe-host');
await expect(host.locator('table')).toBeVisible({ timeout: 15_000 });
await expect(page.getByText(/PROBEOUT/)).toBeVisible({ timeout: 10_000 });
const probe = page.getByText(/PROBEOUT/);
// ---- ① 对照组:input-number qty → amount = qty*10 ----
// input-number 的输入框:取第一行第一列的数字输入。用 .cxd-Number 更稳。
const numberCell = host.locator('.cxd-Number input').first();
await numberCell.click();
await numberCell.fill('5');
await numberCell.press('Tab'); // 失焦触发 change
// tpl 重渲染有批处理延迟,用 toContainText 自动重试(非一次性读)。
await expect(probe, 'input-number 联动应把 amount 填成 50').toContainText('amount=[50]', { timeout: 8_000 });
console.log('[probe] after input-number qty=5 →', await probe.textContent());
// ---- ② select 物料 水泥 → price=10 spec=袋装 ----
const sel = host.locator('.cxd-Select').first();
await sel.click();
await page.waitForTimeout(300);
await page.getByText('水泥', { exact: true }).click();
await expect(probe, 'select 联动应把 price 填成 10').toContainText('price=[10]', { timeout: 8_000 });
await expect(probe, 'select 联动应把 spec 填成 袋装').toContainText('spec=[袋装]', { timeout: 8_000 });
console.log('[probe] after select 水泥 →', await probe.textContent());
console.log('[probe] console errors:', JSON.stringify(consoleErrors.slice(0, 20)));
});
test('amis select source(远程 picker) 选项仍携带整行额外列 → 联动填充', async ({ page }) => {
// 真实后端场景:子表引用列 select 用 source=/api/picker/{ref}/list(responseData options:${items}),
// items 是整行(含 PRICE/SPEC + _id/_label)。验证远程 source 选项被选中后 selectedItems 仍带额外列。
await page.goto('/login');
await page.waitForFunction(() => typeof (window as Window & { amisRequire?: unknown }).amisRequire === 'function',
null, { timeout: 30_000 });
await page.evaluate(() => {
const w = window as unknown as { amisRequire: (p: string) => any };
const host = document.createElement('div');
host.id = 'probe-host2';
host.style.cssText = 'position:fixed;inset:0;z-index:99999;background:#fff;padding:24px;overflow:auto';
document.body.appendChild(host);
const amis = w.amisRequire('amis/embed');
// mock fetcher:模拟 picker/list 返回整行 items(每行带 PRICE/SPEC/_id/_label/value/label)。
const fetcher = (opt: { url: string }) => {
if (opt.url.includes('/api/picker/')) {
const items = [
{ _id: 'C1', _label: '水泥', value: 'C1', label: '水泥', PRICE: '10', SPEC: '袋装' },
{ _id: 'S1', _label: '钢筋', value: 'S1', label: '钢筋', PRICE: '20', SPEC: '12mm' },
];
return Promise.resolve({ status: 200, headers: {}, data: { status: 0, msg: '', data: { items, options: items, total: 2 } } });
}
return Promise.resolve({ status: 200, headers: {}, data: { status: 0, msg: '', data: {} } });
};
const schema = {
type: 'page',
data: { rows: [{}] },
body: {
type: 'form',
title: '远程 source 联动验证',
wrapWithPanel: false,
body: [
{
type: 'input-table',
name: 'rows',
id: 'eg-rows2',
addable: true,
removable: true,
needConfirm: false,
columns: [
{
type: 'select',
name: 'material',
label: '物料',
source: { method: 'get', url: '/api/picker/MATERIAL/list', responseData: { options: '${items}' } },
labelField: '_label',
valueField: '_id',
onEvent: {
change: {
actions: [
{ actionType: 'setValue', componentId: 'price_${index}', args: { value: '${event.data.selectedItems.PRICE}' } },
{ actionType: 'setValue', componentId: 'spec_${index}', args: { value: '${event.data.selectedItems.SPEC}' } },
],
},
},
},
{ name: 'price', id: 'price_${index}', label: '单价' },
{ name: 'spec', id: 'spec_${index}', label: '规格' },
],
},
{ type: 'tpl', id: 'probe-out2', tpl: 'PROBEOUT2 price=[${rows[0].price}] spec=[${rows[0].spec}]' },
],
},
};
amis.embed(host, schema, {}, { fetcher });
});
const host = page.locator('#probe-host2');
await expect(host.locator('table')).toBeVisible({ timeout: 15_000 });
const probe = page.getByText(/PROBEOUT2/);
const sel = host.locator('.cxd-Select').first();
await sel.click();
await page.waitForTimeout(400); // 等远程 options 加载
await page.getByText('水泥', { exact: true }).click();
await expect(probe, '远程 source 选项联动应填 price=10').toContainText('price=[10]', { timeout: 8_000 });
await expect(probe, '远程 source 选项联动应填 spec=袋装').toContainText('spec=[袋装]', { timeout: 8_000 });
console.log('[probe2] after remote select 水泥 →', await probe.textContent());
});
test('amis 多行 input-table:componentId=col_${index} 按行隔离填充(行0选水泥/行1选钢筋, 互不串台)', async ({ page }) => {
// ★ 关键回归(对抗评审 HIGH 揪出):联动靠 ${index} 让 componentId 逐行展开。单行测覆盖不到「选第 2 行
// 是否误填第 0 行」的串台风险(属红线「绝不写错值」)。本测 seed 两行,分别选不同物料,断言各行各填各的。
await page.goto('/login');
await page.waitForFunction(() => typeof (window as Window & { amisRequire?: unknown }).amisRequire === 'function',
null, { timeout: 30_000 });
await page.evaluate(() => {
const w = window as unknown as { amisRequire: (p: string) => any };
const host = document.createElement('div');
host.id = 'probe-host3';
host.style.cssText = 'position:fixed;inset:0;z-index:99999;background:#fff;padding:24px;overflow:auto';
document.body.appendChild(host);
const amis = w.amisRequire('amis/embed');
const schema = {
type: 'page',
data: { rows: [{}, {}] }, // 两行
body: {
type: 'form', title: '多行联动隔离验证', wrapWithPanel: false,
body: [
{
type: 'input-table', name: 'rows', id: 'eg-rows3',
addable: true, removable: true, needConfirm: false,
columns: [
{
type: 'select', name: 'material', label: '物料',
options: [
{ label: '水泥', value: 'C1', PRICE: '10' },
{ label: '钢筋', value: 'S1', PRICE: '20' },
],
onEvent: { change: { actions: [
{ actionType: 'setValue', componentId: 'price_${index}', args: { value: '${event.data.selectedItems.PRICE}' } },
] } },
},
{ name: 'price', id: 'price_${index}', label: '单价' },
],
},
{ type: 'tpl', tpl: 'MULTI r0=[${rows[0].price}] r1=[${rows[1].price}]' },
],
},
};
amis.embed(host, schema, {}, {});
});
const host = page.locator('#probe-host3');
await expect(host.locator('table')).toBeVisible({ timeout: 15_000 });
const probe = page.getByText(/MULTI/);
// 行0 选水泥(10)
await host.locator('.cxd-Select').nth(0).click();
await page.waitForTimeout(200);
await page.getByText('水泥', { exact: true }).click();
await expect(probe, '行0选水泥应填 r0=10').toContainText('r0=[10]', { timeout: 8_000 });
// 行1 选钢筋(20):必须只填 r1,绝不串改 r0。
await host.locator('.cxd-Select').nth(1).click();
await page.waitForTimeout(200);
await page.getByText('钢筋', { exact: true }).click();
await expect(probe, '行1选钢筋应填 r1=20').toContainText('r1=[20]', { timeout: 8_000 });
// 串台守卫:r0 仍须是 10(行1的选择没污染行0)。
await expect(probe, '行0不应被行1串改').toContainText('r0=[10] r1=[20]', { timeout: 8_000 });
console.log('[probe3] multi-row →', await probe.textContent());
});