37 lines
2.4 KiB
JavaScript
37 lines
2.4 KiB
JavaScript
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import fs from 'node:fs';
|
|
import ts from 'typescript';
|
|
|
|
const source=fs.readFileSync(new URL('../src/StageSourceFilters.tsx',import.meta.url),'utf8');
|
|
const output=ts.transpileModule(source,{compilerOptions:{module:ts.ModuleKind.CommonJS,jsx:ts.JsxEmit.ReactJSX,target:ts.ScriptTarget.ES2022}}).outputText;
|
|
let lastSearch;
|
|
const jsx=(type,props)=>({type,props});const exports={};
|
|
new Function('require','exports',output)(name=>{
|
|
if(name==='react/jsx-runtime')return {jsx,jsxs:jsx};
|
|
if(name==='react')return {useState:initial=>[initial,value=>{lastSearch=value;} ]};
|
|
if(name==='antd')return {Select:'Select',Button:'Button',Alert:'Alert',Spin:'Spin'};
|
|
if(name==='@ant-design/icons')return {CloseOutlined:'CloseOutlined'};
|
|
if(name==='./readableSelectPopup')return {readableSelectPopup:{}};
|
|
return {};
|
|
},exports);
|
|
const nodes=value=>!value||typeof value!=='object'?[]:[value,...[value.props?.children].flat(Infinity).flatMap(nodes)];
|
|
const field={key:'customer',label:'客户名称',type:'source_value',stages:['bright'],columns:{bright:'客户名称'}};
|
|
|
|
test('contains is selectable and accepts bounded free keywords without a value-candidate lookup',()=>{
|
|
let changed;
|
|
const rule={field:'customer',stages:['bright'],operator:'contains',values:['日立']};
|
|
const root=exports.StageSourceFilters({stage:'bright',fields:[field],query:{year:2026},value:[rule],onChange:value=>{changed=value;},onUnauthorized:()=>false});
|
|
const all=nodes(root);const operation=all.find(x=>x.type==='Select'&&x.props['aria-label']==='客户名称筛选方式');
|
|
assert.ok(operation.props.options.some(x=>x.value==='contains'&&x.label==='包含'));
|
|
const free=all.find(x=>typeof x.type==='function'&&x.type.name==='ContainsValues');
|
|
const keywords=free.type(free.props);
|
|
assert.equal(keywords.props.mode,'tags');assert.equal(keywords.props.maxCount,50);assert.deepEqual(keywords.props.options,[]);
|
|
keywords.props.onSearch('x'.repeat(257));assert.equal(lastSearch.length,256);
|
|
keywords.props.onSearch('x'.repeat(255)+'😀');assert.equal(lastSearch,'x'.repeat(255));
|
|
keywords.props.onSearch('日立');keywords.props.onBlur();assert.equal(lastSearch,'');
|
|
keywords.props.onChange(['日立','%_',' ']);
|
|
assert.deepEqual(changed,[{...rule,values:['日立','%_']}]);
|
|
operation.props.onChange('in');assert.deepEqual(changed,[{...rule,operator:'in',values:[]}]);
|
|
});
|