同步:补齐已发布的查询面板与不良构成源码
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import fs from 'node:fs';
|
||||
import ts from 'typescript';
|
||||
import React from 'react';
|
||||
function fixture() {
|
||||
const source=fs.readFileSync(process.env.QUERY_CHART_SOURCE || new URL('../src/QueryPage.tsx',import.meta.url),'utf8');
|
||||
const end=source.includes('export function TrendTable(')?source.indexOf('export function TrendTable('):source.indexOf('function TrendTable(');
|
||||
const code=source.slice(source.indexOf('export function TrendChart('),end).replace('export function','function');
|
||||
const slots=new Map(),effects=new Map(),observers=[],charts=[];let cursor=0,dirty=false,queued=[],active=true;
|
||||
const node={clientWidth:900,clientHeight:380};
|
||||
const hooks={useRef(initial){const i=cursor++;if(!slots.has(i))slots.set(i,{current:initial});return slots.get(i);},useState(initial){const i=cursor++;if(!slots.has(i))slots.set(i,initial);return [slots.get(i),v=>{const next=typeof v==='function'?v(slots.get(i)):v;if(!Object.is(next,slots.get(i))){slots.set(i,next);dirty=true;}}];},useCallback(fn,deps){const i=cursor++;const old=slots.get(i);if(!old||deps.some((d,j)=>!Object.is(d,old.deps[j])))slots.set(i,{fn,deps});return slots.get(i).fn;},useEffect(fn,deps){const i=cursor++,old=effects.get(i);if(!old||deps.some((d,j)=>!Object.is(d,old.deps[j])))queued.push(()=>{old?.cleanup?.();effects.set(i,{deps,cleanup:fn()});});}};
|
||||
hooks.useLayoutEffect=hooks.useEffect;
|
||||
const ec={init(_node,_theme,opts){const c={opts,options:[],resizes:[],disposed:false,getWidth:()=>c.opts.width??100,getHeight:()=>c.opts.height??380,on(){},resize(o){c.resizes.push(o);},setOption(o){c.options.push(o);},convertToPixel:()=>0,dispose(){c.disposed=true;}};charts.push(c);return c;}};
|
||||
const prelude=`const {useRef,useState,useEffect,useLayoutEffect,useCallback}=hooks;
|
||||
const usePageCache=()=>({active:getActive()});
|
||||
const Empty=Object.assign('Empty',{PRESENTED_IMAGE_SIMPLE:'simple'});
|
||||
const MutationObserver=class{observe(){}disconnect(){}},ResizeObserver=class{constructor(fn){this.fn=fn;observers.push(this)}observe(){}disconnect(){this.disconnected=true}};
|
||||
const document={documentElement:{dataset:{}}},window={matchMedia:()=>({matches:false,addEventListener(){},removeEventListener(){}})};
|
||||
const queryChartTypography=()=>({fontFamily:'sans',numericFamily:'sans',fontSize:12,tooltipFontSize:12});
|
||||
const getComputedStyle=()=>({getPropertyValue:()=>''}),getWorkspaceTheme=()=>({id:'light'}),trendBarPalette=()=>({qualified:'#607ae1',scrap:'#d58c35'});
|
||||
const trendBarWidth=()=>32,hasStageRows=()=>true,stageValue=(r,s,f)=>r[s][f],visibleCompositeRate=()=>.95,visibleStageRate=()=>.97,rateNumber=v=>v*100,numberText=String,escapeTooltipText=String,defectWeightRate=()=>0;
|
||||
`;
|
||||
const compiled=ts.transpileModule(prelude+code+'\nreturn TrendChart;',{fileName:'motion.tsx',compilerOptions:{target:ts.ScriptTarget.ES2022,jsx:ts.JsxEmit.React}}).outputText;
|
||||
const Component=new Function('React','hooks','echarts','getActive','observers',compiled)(React,hooks,ec,()=>active,observers);
|
||||
const stage={qualifiedWeight:90,scrapWeight:10,totalWeight:100,defects:[]};const props={rows:[{month:1,black:stage,bright:stage}],mode:'production',stage:'bright',rateBasis:'original',targetRate:null,onMonth(){}};
|
||||
function attach(t){if(!t||typeof t!=='object')return;if(t.props?.ref)t.props.ref.current=node;React.Children.forEach(t.props?.children,attach);}
|
||||
function render(){let n=0;do{cursor=0;dirty=false;queued=[];attach(Component(props));queued.forEach(f=>f());assert.ok(++n<10);}while(dirty);}
|
||||
return {charts,node,render,setActive(v){active=v;render();},resize(){for(const o of observers)if(!o.disconnected)o.fn();},unmount(){effects.forEach(e=>e.cleanup?.());}};
|
||||
}
|
||||
test('visible entry uses the actual chart size and enables native vertical bar growth',()=>{
|
||||
const f=fixture();f.render();const c=f.charts[0];assert.equal(c.opts.width,900);assert.equal(c.opts.height,380);
|
||||
const o=c.options.find(o=>o.series);assert.ok(o.animationDuration>0);assert.equal(o.xAxis.type,'category');assert.equal(o.yAxis[0].type,'value');assert.ok(o.series.filter(s=>s.type==='bar').every(s=>s.yAxisIndex===0));f.unmount();
|
||||
});
|
||||
test('cached page reentry creates a fresh vertical reveal and never resizes a hidden chart to zero',()=>{
|
||||
const f=fixture();f.render();f.node.clientWidth=0;f.resize();assert.equal(f.charts[0].resizes.length,0);
|
||||
f.setActive(false);assert.equal(f.charts[0].disposed,true);assert.equal(f.charts.length,1);
|
||||
f.node.clientWidth=900;f.setActive(true);assert.equal(f.charts.length,2);assert.equal(f.charts[1].opts.width,900);assert.ok(f.charts[1].options.find(o=>o.series).animationDuration>0);f.unmount();
|
||||
});
|
||||
Reference in New Issue
Block a user