chore: 版本 0.1.0 更新 默认 query-option 深拷贝导出与 schema 缺省回退
Node.js Build / build (push) Successful in 6s

This commit is contained in:
2026-08-07 16:42:01 +08:00
parent 23a7ebf668
commit e938118594
6 changed files with 212 additions and 6 deletions
+60
View File
@@ -0,0 +1,60 @@
import { afterEach, describe, expect, it } from 'vitest';
import { mount } from '@vue/test-utils';
import TransForm from '../src/TransForm.vue';
import { resetConfig, setConfig } from '../src/config.js';
// 每个用例后恢复全局配置,避免 setConfig 污染其它用例
afterEach(() => {
resetConfig();
});
describe('TransForm 操作符下拉(默认 query-option)', () => {
it('无 query-option 的字段渲染默认操作符列表(12 项,第一项 EQ)', () => {
const wrapper = mount(TransForm, {
props: { schema: [{ row: 12, field: 'name', 'field-name': '名称' }] },
});
const opts = wrapper.find('select').findAll('option');
expect(opts).toHaveLength(12);
expect(opts[0].text()).toBe('=');
expect(opts[0].attributes('value')).toBe('EQ');
expect(opts[11].text()).toBe('IS NOT NULL');
});
it('有 query-option 的字段仍渲染 schema 自己的选项(不被默认覆盖)', () => {
const wrapper = mount(TransForm, {
props: {
schema: [
{
row: 12,
field: 'status',
'field-name': '状态',
'query-option': [{ value: 'NE', label: '≠' }],
},
],
},
});
const opts = wrapper.find('select').findAll('option');
expect(opts).toHaveLength(1);
expect(opts[0].text()).toBe('≠');
expect(opts[0].attributes('value')).toBe('NE');
});
it('setConfig 自定义 queryOption 后,无 query-option 字段的下拉随之更新', () => {
setConfig({ queryOption: [{ value: 'GT', label: '>' }] });
const wrapper = mount(TransForm, {
props: { schema: [{ row: 12, field: 'age', 'field-name': '年龄' }] },
});
const opts = wrapper.find('select').findAll('option');
expect(opts).toHaveLength(1);
expect(opts[0].text()).toBe('>');
});
it('有 query-component 但无 query-option 的字段不套默认下拉(操作符由组件自己管理)', () => {
const wrapper = mount(TransForm, {
props: {
schema: [{ row: 12, field: 'custom', 'query-component': 'CustomQuery' }],
},
});
expect(wrapper.find('select').findAll('option')).toHaveLength(0);
});
});
+92 -1
View File
@@ -1,11 +1,18 @@
import { describe, expect, it } from 'vitest';
import { afterEach, describe, expect, it } from 'vitest';
import { getDefaultQueryOption, resetConfig, setConfig } from '../src/config.js';
import {
applyFilters,
buildFilters,
createFieldState,
groupRows,
resolveQueryOption,
} from '../src/composables/useFilters.js';
// 每个用例后恢复全局配置,避免 setConfig 污染其它用例
afterEach(() => {
resetConfig();
});
const schema = [
{
row: 6,
@@ -68,3 +75,87 @@ describe('groupRows', () => {
expect(groups[1]).toHaveLength(1);
});
});
describe('resolveQueryOption(默认 query-option 解析)', () => {
it('无 query-option 且无 query-component 时返回默认操作符列表', () => {
const opts = resolveQueryOption({ field: 'name' });
expect(opts).toHaveLength(12);
expect(opts[0]).toEqual({ value: 'EQ', label: '=' });
expect(opts.map((o) => o.value)).toEqual([
'EQ', 'NE', 'GT', 'GTE', 'LT', 'LTE',
'LIKE', 'IN', 'NOT_IN', 'BETWEEN', 'IS_NULL', 'IS_NOT_NULL',
]);
});
it('有 query-option 的字段仍用 schema 自己的(不被默认覆盖)', () => {
const fieldDef = { field: 'name', 'query-option': [{ value: 'NE', label: '≠' }] };
expect(resolveQueryOption(fieldDef)).toEqual([{ value: 'NE', label: '≠' }]);
});
it('有 query-component 但无 query-option 时返回空数组(组件自己管理)', () => {
const fieldDef = { field: 'name', 'query-component': 'CustomQuery' };
expect(resolveQueryOption(fieldDef)).toEqual([]);
});
it('getDefaultQueryOption() 返回深拷贝:修改返回值不影响后续调用', () => {
const first = getDefaultQueryOption();
first.push({ value: 'X', label: 'x' });
first[0].label = '被改坏了';
first.splice(1, 1);
const second = getDefaultQueryOption();
expect(second).toHaveLength(12);
expect(second[0]).toEqual({ value: 'EQ', label: '=' });
expect(second[1]).toEqual({ value: 'NE', label: '≠' });
});
it('setConfig({ queryOption }) 后 getDefaultQueryOption() 返回新列表', () => {
setConfig({
queryOption: [
{ value: 'EQ', label: '=' },
{ value: 'GT', label: '>' },
],
});
expect(getDefaultQueryOption()).toEqual([
{ value: 'EQ', label: '=' },
{ value: 'GT', label: '>' },
]);
});
it('resetConfig() 恢复默认操作符列表', () => {
setConfig({ queryOption: [{ value: 'ONLY_EQ', label: '=' }] });
resetConfig();
expect(getDefaultQueryOption()).toHaveLength(12);
expect(getDefaultQueryOption()[0]).toEqual({ value: 'EQ', label: '=' });
});
});
describe('默认 query fallback(无 query-option 时取默认列表第一项)', () => {
it('createFieldState:无 query-option 且无 query-component 默认取默认列表第一项(EQ)', () => {
const state = createFieldState([{ field: 'name' }]);
expect(state.name.query).toBe('EQ');
});
it('buildFilters:无 query-option 且无 query-component 默认取默认列表第一项(EQ)', () => {
const state = createFieldState([{ field: 'name' }]);
const filters = buildFilters([{ field: 'name' }], state);
expect(filters[0].query).toBe('EQ');
});
it('setConfig 自定义 queryOption 后,默认 query 取新列表第一项', () => {
setConfig({ queryOption: [{ value: 'GT', label: '>' }] });
const state = createFieldState([{ field: 'age' }]);
expect(state.age.query).toBe('GT');
expect(buildFilters([{ field: 'age' }], state)[0].query).toBe('GT');
});
it('有 query-component 但无 query-option 时,默认 query 仍为 EQ', () => {
const state = createFieldState([{ field: 'custom', 'query-component': 'CustomQuery' }]);
expect(state.custom.query).toBe('EQ');
});
it('显式 default-query 优先级最高,覆盖默认列表第一项', () => {
const state = createFieldState([{ field: 'name', 'default-query': 'LIKE' }]);
expect(state.name.query).toBe('LIKE');
});
});