tmagic-editor/packages/editor/tests/unit/fields/DataSourceFields.spec.ts
roymondchen d068b7c1c4 feat(editor): 数据源字段选择支持数组下标与迭代器元素字段
迭代器子组件可同时选元素字段和下标,其余场景只保留固定下标;已选路径缺节点时补回选项,避免级联框空白。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-12-01 16:03:06 +08:00

464 lines
18 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.

/*
* Tencent is pleased to support the open source community by making TMagicEditor available.
*
* Copyright (C) 2025 Tencent.
*/
import { beforeEach, describe, expect, test, vi } from 'vitest';
import { defineComponent, h, ref } from 'vue';
import { mount } from '@vue/test-utils';
import DataSourceFields from '@editor/fields/DataSourceFields.vue';
const { messageBoxConfirm, messageError, fieldFormSubmitPayload, fieldFormChangeRecords, capturedFieldFormValues } =
vi.hoisted(() => {
const current: Record<string, any> = { index: -1, name: 'a', title: 't', type: 'string' };
const changeRecords: { propPath?: string; value: any }[] = [];
return {
messageBoxConfirm: vi.fn(async () => true),
messageError: vi.fn(),
fieldFormSubmitPayload: { current },
fieldFormChangeRecords: { current: changeRecords },
capturedFieldFormValues: { current: null as any },
};
});
const uiService = { get: vi.fn() };
vi.mock('@editor/hooks/use-services', () => ({
useServices: () => ({ uiService }),
}));
vi.mock('@editor/hooks', () => ({
useEditorContentHeight: () => ({ height: ref(600) }),
}));
vi.mock('@editor/hooks/use-next-float-box-position', () => ({
useNextFloatBoxPosition: () => ({ boxPosition: ref({ x: 100, y: 100 }), calcBoxPosition: vi.fn() }),
}));
vi.mock('@editor/utils/logger', () => ({ error: vi.fn() }));
vi.mock('@editor/components/FloatingBox.vue', () => ({
default: defineComponent({
name: 'FloatingBox',
props: ['visible', 'width', 'height', 'title', 'position'],
setup(props, { slots }) {
return () =>
h(
'div',
{
class: ['fake-floating', `fb-${props.title}`],
'data-visible': String(props.visible),
},
slots.body?.(),
);
},
}),
}));
let capturedColumns: any[] = [];
let capturedConfigs: any[] = [];
vi.mock('@tmagic/table', () => ({
MagicTable: defineComponent({
name: 'MagicTable',
props: ['data', 'columns', 'border'],
setup(props) {
capturedColumns = props.columns;
return () => h('div', { class: 'fake-table' });
},
}),
}));
vi.mock('@tmagic/form', async () => {
const actual = await vi.importActual<any>('@tmagic/form');
return {
...actual,
MFormBox: defineComponent({
name: 'MFormBox',
props: ['config', 'values', 'parentValues', 'disabled', 'title', 'labelWidth'],
emits: ['submit'],
setup(props, { emit }) {
capturedConfigs.push(props.config);
const isJson =
Array.isArray(props.config) && props.config.some((c: any) => c.type === 'vs-code' && c.language === 'json');
return () => {
if (!isJson) capturedFieldFormValues.current = props.values;
return h('div', {
class: ['fake-form-box', isJson ? 'json-form' : 'field-form'],
onClick: () => {
if (isJson) {
emit('submit', { data: '{"foo":1}' });
} else {
emit('submit', fieldFormSubmitPayload.current, { changeRecords: fieldFormChangeRecords.current });
}
},
});
};
},
}),
};
});
vi.mock('@tmagic/utils', async () => {
const actual = await vi.importActual<any>('@tmagic/utils');
return {
...actual,
getDefaultValueFromFields: vi.fn(() => ({ a: 1 })),
};
});
vi.mock('@tmagic/design', () => ({
TMagicButton: defineComponent({
name: 'TMagicButton',
inheritAttrs: false,
setup(_p, { slots, attrs }) {
return () =>
h(
'button',
{
...attrs,
class: ['fake-btn', (attrs as any).class].filter(Boolean).join(' '),
},
slots.default?.(),
);
},
}),
tMagicMessage: { error: messageError },
tMagicMessageBox: { confirm: messageBoxConfirm },
}));
beforeEach(() => {
vi.clearAllMocks();
capturedColumns = [];
capturedConfigs = [];
capturedFieldFormValues.current = null;
fieldFormSubmitPayload.current = { index: -1, name: 'a', title: 't', type: 'string' };
fieldFormChangeRecords.current = [];
});
describe('DataSourceFields', () => {
test('渲染 MagicTable 和按钮', () => {
const wrapper = mount(DataSourceFields, {
props: { config: {}, model: { fields: [] }, name: 'fields', prop: 'fields' } as any,
});
expect(wrapper.find('.fake-table').exists()).toBe(true);
expect(wrapper.findAll('.fake-btn').length).toBeGreaterThanOrEqual(2);
});
test('点击新增字段添加', async () => {
const model: any = { fields: [] };
const wrapper = mount(DataSourceFields, {
props: { config: {}, model, name: 'fields', prop: 'fields' } as any,
});
const buttons = wrapper.findAll('.fake-btn');
await buttons[1].trigger('click');
await wrapper.find('.field-form').trigger('click');
expect(wrapper.emitted('change')).toBeTruthy();
const lastCall = (wrapper.emitted('change') as any[]).pop();
expect(lastCall[1]).toMatchObject({ modifyKey: 0 });
});
test('修改已有字段 (index > -1)', async () => {
capturedConfigs = [];
const model: any = { fields: [{ name: 'a', title: 't1', type: 'string' }] };
const wrapper = mount(DataSourceFields, {
props: { config: {}, model, name: 'fields', prop: 'fields' } as any,
});
const editAction = capturedColumns[capturedColumns.length - 1].actions[0];
editAction.handler({ name: 'a', title: 't1', type: 'string' }, 0);
// 重新触发 form submit 模拟为 index 0
capturedConfigs = [];
void wrapper;
});
test('删除 action 弹出确认并删除', async () => {
const model: any = { fields: [{ name: 'a', title: 't1' }] };
const wrapper = mount(DataSourceFields, {
props: { config: {}, model, name: 'fields', prop: 'fields' } as any,
});
const removeAction = capturedColumns[capturedColumns.length - 1].actions[1];
await removeAction.handler({ name: 'a', title: 't1' }, 0);
expect(messageBoxConfirm).toHaveBeenCalled();
expect(model.fields).toHaveLength(0);
expect(wrapper.emitted('change')).toBeTruthy();
});
test('快速添加 JSON 解析后 emit change', async () => {
const wrapper = mount(DataSourceFields, {
props: { config: {}, model: { fields: [] }, name: 'fields', prop: 'fields' } as any,
});
const buttons = wrapper.findAll('.fake-btn');
await buttons[0].trigger('click');
await wrapper.find('.json-form').trigger('click');
const events = wrapper.emitted('change');
expect(events).toBeTruthy();
const lastCall = events![events!.length - 1];
expect(lastCall[0]).toEqual([expect.objectContaining({ name: 'foo', type: 'number', defaultValue: 1 })]);
});
test('快速添加 JSON 解析失败 message.error', async () => {
capturedConfigs = [];
const wrapper = mount(DataSourceFields, {
props: { config: {}, model: { fields: [] }, name: 'fields', prop: 'fields' } as any,
});
const fbConfig = capturedConfigs[1];
void fbConfig;
void wrapper;
expect(true).toBe(true);
});
test('数据类型 onChange 重置 fields', () => {
mount(DataSourceFields, {
props: { config: {}, model: { fields: [] }, name: 'fields', prop: 'fields' } as any,
});
const typeItem = capturedConfigs[0].find((c: any) => c.name === 'type');
const setModel = vi.fn();
typeItem.onChange(undefined, 'string', { setModel });
expect(setModel).toHaveBeenCalledWith('fields', []);
expect(setModel).toHaveBeenCalledTimes(1);
setModel.mockClear();
typeItem.onChange(undefined, 'object', { setModel });
expect(setModel).toHaveBeenCalledWith('defaultValue', {});
expect(setModel).not.toHaveBeenCalledWith('fields', []);
});
test('切到数组且默认值为空时写成 [],切走时空数组恢复成空字符串', () => {
mount(DataSourceFields, {
props: { config: {}, model: { fields: [] }, name: 'fields', prop: 'fields' } as any,
});
const typeItem = capturedConfigs[0].find((c: any) => c.name === 'type');
const setModel = vi.fn();
typeItem.onChange(undefined, 'array', { model: { defaultValue: '' }, setModel });
expect(setModel).toHaveBeenCalledWith('defaultValue', []);
setModel.mockClear();
typeItem.onChange(undefined, 'array', { model: { type: 'array' }, setModel });
expect(setModel).toHaveBeenCalledWith('defaultValue', []);
setModel.mockClear();
typeItem.onChange(undefined, 'array', { model: { defaultValue: [{ id: 1 }] }, setModel });
expect(setModel).not.toHaveBeenCalled();
setModel.mockClear();
typeItem.onChange(undefined, 'string', { model: { defaultValue: [] }, setModel });
expect(setModel).toHaveBeenCalledWith('fields', []);
expect(setModel).toHaveBeenCalledWith('defaultValue', '');
setModel.mockClear();
typeItem.onChange(undefined, 'string', { model: { defaultValue: 'kept' }, setModel });
expect(setModel).not.toHaveBeenCalledWith('defaultValue', '');
});
test('提交数组字段时未填默认值收成空数组', async () => {
fieldFormSubmitPayload.current = { index: -1, name: 'list', title: '列表', type: 'array', defaultValue: '' };
const wrapper = mount(DataSourceFields, {
props: { config: {}, model: { fields: [] }, name: 'fields', prop: 'fields' } as any,
});
await wrapper.findAll('.fake-btn')[1].trigger('click');
await wrapper.find('.field-form').trigger('click');
const lastCall = (wrapper.emitted('change') as any[]).pop();
expect(lastCall[0]).toMatchObject({ name: 'list', type: 'array', defaultValue: [] });
expect(lastCall[1].changeRecords[0].value.defaultValue).toEqual([]);
});
test('提交数组字段时已填默认值保持不变', async () => {
fieldFormSubmitPayload.current = {
index: 0,
name: 'list',
title: '列表',
type: 'array',
defaultValue: [{ id: 1 }],
};
const wrapper = mount(DataSourceFields, {
props: {
config: {},
model: { fields: [{ name: 'list', title: '列表', type: 'array' }] },
name: 'fields',
prop: 'fields',
} as any,
});
await wrapper.find('.field-form').trigger('click');
const lastCall = (wrapper.emitted('change') as any[]).pop();
expect(lastCall[0].defaultValue).toEqual([{ id: 1 }]);
expect(lastCall[1].modifyKey).toBe(0);
expect(lastCall[1].changeRecords).toEqual([]);
});
test('编辑数组字段时未填默认值会写入空数组', async () => {
fieldFormSubmitPayload.current = { index: 0, name: 'list', title: '列表', type: 'array', defaultValue: '' };
fieldFormChangeRecords.current = [{ propPath: 'title', value: '列表' }];
const wrapper = mount(DataSourceFields, {
props: {
config: {},
model: { fields: [{ name: 'list', title: '列表', type: 'array', defaultValue: '' }] },
name: 'fields',
prop: 'fields',
} as any,
});
await wrapper.find('.field-form').trigger('click');
const lastCall = (wrapper.emitted('change') as any[]).pop();
expect(lastCall[0].defaultValue).toEqual([]);
expect(lastCall[1].changeRecords).toEqual([
{ propPath: 'fields.0.title', value: '列表' },
{ propPath: 'fields.0.defaultValue', value: [] },
]);
fieldFormSubmitPayload.current = { index: 0, name: 'list', title: '列表', type: 'array' };
fieldFormChangeRecords.current = [{ propPath: 'defaultValue', value: '' }];
await wrapper.find('.field-form').trigger('click');
const replaced = (wrapper.emitted('change') as any[]).pop();
expect(replaced[0].defaultValue).toEqual([]);
expect(replaced[1].changeRecords).toEqual([{ propPath: 'fields.0.defaultValue', value: [] }]);
});
test('切到对象且默认值为空时写成 {},切走时空对象恢复成空字符串', () => {
mount(DataSourceFields, {
props: { config: {}, model: { fields: [] }, name: 'fields', prop: 'fields' } as any,
});
const typeItem = capturedConfigs[0].find((c: any) => c.name === 'type');
const setModel = vi.fn();
typeItem.onChange(undefined, 'object', { model: { defaultValue: '' }, setModel });
expect(setModel).toHaveBeenCalledWith('defaultValue', {});
setModel.mockClear();
typeItem.onChange(undefined, 'object', { model: { defaultValue: [] }, setModel });
expect(setModel).toHaveBeenCalledWith('defaultValue', {});
setModel.mockClear();
typeItem.onChange(undefined, 'object', { model: { defaultValue: { a: 1 } }, setModel });
expect(setModel).not.toHaveBeenCalled();
setModel.mockClear();
typeItem.onChange(undefined, 'array', { model: { defaultValue: {} }, setModel });
expect(setModel).toHaveBeenCalledWith('defaultValue', []);
setModel.mockClear();
typeItem.onChange(undefined, 'string', { model: { defaultValue: {} }, setModel });
expect(setModel).toHaveBeenCalledWith('fields', []);
expect(setModel).toHaveBeenCalledWith('defaultValue', '');
});
test('提交对象字段时未填默认值收成空对象', async () => {
fieldFormSubmitPayload.current = { index: -1, name: 'info', title: '信息', type: 'object', defaultValue: '' };
const wrapper = mount(DataSourceFields, {
props: { config: {}, model: { fields: [] }, name: 'fields', prop: 'fields' } as any,
});
await wrapper.findAll('.fake-btn')[1].trigger('click');
await wrapper.find('.field-form').trigger('click');
const lastCall = (wrapper.emitted('change') as any[]).pop();
expect(lastCall[0]).toMatchObject({ name: 'info', type: 'object', defaultValue: {} });
expect(lastCall[1].changeRecords[0].value.defaultValue).toEqual({});
});
test('提交对象字段时已填默认值保持不变', async () => {
fieldFormSubmitPayload.current = {
index: 0,
name: 'info',
title: '信息',
type: 'object',
defaultValue: { a: 1 },
};
const wrapper = mount(DataSourceFields, {
props: {
config: {},
model: { fields: [{ name: 'info', title: '信息', type: 'object' }] },
name: 'fields',
prop: 'fields',
} as any,
});
await wrapper.find('.field-form').trigger('click');
const lastCall = (wrapper.emitted('change') as any[]).pop();
expect(lastCall[0].defaultValue).toEqual({ a: 1 });
expect(lastCall[1].changeRecords).toEqual([]);
});
test('编辑对象字段时未填默认值会写入空对象', async () => {
fieldFormSubmitPayload.current = { index: 0, name: 'info', title: '信息', type: 'object' };
fieldFormChangeRecords.current = [{ propPath: 'defaultValue', value: '' }];
const wrapper = mount(DataSourceFields, {
props: {
config: {},
model: { fields: [{ name: 'info', title: '信息', type: 'object', defaultValue: '' }] },
name: 'fields',
prop: 'fields',
} as any,
});
await wrapper.find('.field-form').trigger('click');
const lastCall = (wrapper.emitted('change') as any[]).pop();
expect(lastCall[0].defaultValue).toEqual({});
expect(lastCall[1].changeRecords).toEqual([{ propPath: 'fields.0.defaultValue', value: {} }]);
});
test('编辑对象字段时空默认值显示为空对象', async () => {
const row = { name: 'info', title: '信息', type: 'object', defaultValue: '' };
const wrapper = mount(DataSourceFields, {
props: { config: {}, model: { fields: [row] }, name: 'fields', prop: 'fields' } as any,
});
const editAction = capturedColumns[capturedColumns.length - 1].actions[0];
editAction.handler(row, 0);
await wrapper.vm.$nextTick();
expect(capturedFieldFormValues.current).toMatchObject({ type: 'object', defaultValue: {}, index: 0 });
});
test('编辑数组字段时空默认值显示为空数组', async () => {
const row = { name: 'list', title: '列表', type: 'array', defaultValue: '' };
const wrapper = mount(DataSourceFields, {
props: { config: {}, model: { fields: [row] }, name: 'fields', prop: 'fields' } as any,
});
const editAction = capturedColumns[capturedColumns.length - 1].actions[0];
editAction.handler(row, 0);
await wrapper.vm.$nextTick();
expect(capturedFieldFormValues.current).toMatchObject({ type: 'array', defaultValue: [], index: 0 });
});
test('name 字段 validator 重复提示', () => {
mount(DataSourceFields, {
props: { config: {}, model: { fields: [] }, name: 'fields', prop: 'fields' } as any,
});
const nameItem = capturedConfigs[0].find((c: any) => c.name === 'name');
const { validator } = nameItem.rules[1];
const callback = vi.fn();
validator({ value: 'a', callback }, { model: { index: -1 }, parent: [{ name: 'a' }] });
expect(callback).toHaveBeenCalledWith('属性key(a)已存在');
const callback2 = vi.fn();
validator({ value: 'b', callback: callback2 }, { model: { index: -1 }, parent: [{ name: 'a' }] });
expect(callback2).toHaveBeenCalledWith();
});
test('defaultValue type 函数动态返回', () => {
mount(DataSourceFields, {
props: { config: {}, model: { fields: [] }, name: 'fields', prop: 'fields' } as any,
});
const dvItem = capturedConfigs[0].find((c: any) => c.name === 'defaultValue');
expect(dvItem.type(undefined, { model: { type: 'number' } })).toBe('number');
expect(dvItem.type(undefined, { model: { type: 'boolean' } })).toBe('select');
expect(dvItem.type(undefined, { model: { type: 'string' } })).toBe('text');
expect(dvItem.type(undefined, { model: { type: 'object' } })).toBe('vs-code');
});
test('fields display 函数', () => {
mount(DataSourceFields, {
props: { config: {}, model: { fields: [] }, name: 'fields', prop: 'fields' } as any,
});
const fieldsItem = capturedConfigs[0].find((c: any) => c.name === 'fields');
expect(fieldsItem.display(undefined, { model: { type: 'object' } })).toBe(true);
expect(fieldsItem.display(undefined, { model: { type: 'array' } })).toBe(true);
expect(fieldsItem.display(undefined, { model: { type: 'string' } })).toBe(false);
});
test('defaultValue formatter 异常时返回原值', () => {
mount(DataSourceFields, {
props: { config: {}, model: { fields: [] }, name: 'fields', prop: 'fields' } as any,
});
const dvCol = capturedColumns.find((c: any) => c.prop === 'defaultValue');
const circular: any = {};
circular.self = circular;
expect(dvCol.formatter(undefined, { defaultValue: circular })).toBe(circular);
});
});