tmagic-editor/packages/editor/tests/unit/fields/CondOpSelect.spec.ts
roymondchen 1c67b5e77b feat(editor): 注册编辑器字段内置 typeMatch 校验规则并补充文档
- 新增 typeMatchRules.ts,为编辑器自定义字段注册内置 typeMatch 校验规则
- 抽取 event.ts 事件工具函数
- form typeMatch 适配 TDesign 校验器签名,错误消息补充实际值
- rules.md 新增「Editor 字段内置规则」章节,16 个字段文档补充校验说明
2026-07-14 17:15:28 +08:00

104 lines
3.2 KiB
TypeScript

/*
* Tencent is pleased to support the open source community by making TMagicEditor available.
*
* Copyright (C) 2025 Tencent.
*/
import { describe, expect, test, vi } from 'vitest';
import { defineComponent, h } from 'vue';
import { mount } from '@vue/test-utils';
import CondOpSelect from '@editor/fields/CondOpSelect.vue';
import { getFieldType } from '@editor/utils';
const dataSourceService = {
getDataSourceById: vi.fn(),
};
vi.mock('@editor/hooks/use-services', () => ({
useServices: () => ({ dataSourceService }),
}));
vi.mock('@editor/utils', async () => {
const actual = await vi.importActual<any>('@editor/utils');
return {
...actual,
getFieldType: vi.fn(),
};
});
vi.mock('@tmagic/design', () => ({
TMagicSelect: defineComponent({
name: 'TMagicSelect',
props: ['modelValue', 'clearable', 'filterable', 'size', 'disabled'],
emits: ['change'],
setup(_p, { emit, slots }) {
return () =>
h(
'select',
{
onChange: (e: Event) => emit('change', (e.target as HTMLSelectElement).value),
},
slots.default?.(),
);
},
}),
getDesignConfig: vi.fn(() => ({})),
}));
const baseProps = (extra: any = {}) => ({
config: { type: 'cond-op-select', parentFields: [] },
name: 'op',
model: { field: ['ds1', 'a'], op: '' },
disabled: false,
size: 'default',
...extra,
});
describe('CondOpSelect', () => {
test('array 类型展示 arrayOptions', () => {
(getFieldType as any).mockReturnValue('array');
const wrapper = mount(CondOpSelect, { props: baseProps() as any });
const html = wrapper.html();
expect(html).toContain('label="包含"');
expect(html).toContain('label="不包含"');
});
test('boolean/null 类型展示 是/不是', () => {
(getFieldType as any).mockReturnValue('boolean');
const wrapper = mount(CondOpSelect, { props: baseProps() as any });
expect(wrapper.html()).toContain('label="是"');
});
test('number 类型 options 包含 eq+number', () => {
(getFieldType as any).mockReturnValue('number');
const wrapper = mount(CondOpSelect, { props: baseProps() as any });
const html = wrapper.html();
expect(html).toContain('label="等于"');
expect(html).toContain('label="大于"');
});
test('string 类型 options 包含 array+eq', () => {
(getFieldType as any).mockReturnValue('string');
const wrapper = mount(CondOpSelect, { props: baseProps() as any });
const html = wrapper.html();
expect(html).toContain('label="包含"');
expect(html).toContain('label="等于"');
});
test('其他类型展示 array+eq+number', () => {
(getFieldType as any).mockReturnValue('any');
const wrapper = mount(CondOpSelect, { props: baseProps() as any });
const html = wrapper.html();
expect(html).toContain('label="包含"');
expect(html).toContain('label="等于"');
expect(html).toContain('label="大于"');
});
test('change 事件 emit', async () => {
(getFieldType as any).mockReturnValue('string');
const wrapper = mount(CondOpSelect, { props: baseProps() as any });
await wrapper.findComponent({ name: 'TMagicSelect' }).vm.$emit('change', '=');
expect(wrapper.emitted('change')?.[0]?.[0]).toBe('=');
});
});