cmvr-wms/mobile-app/tests/arrival-list.test.mjs

44 lines
3.7 KiB
JavaScript
Raw Normal View History

import test from 'node:test';
import assert from 'node:assert/strict';
import {readFileSync} from 'node:fs';
import {createRequire} from 'node:module';
import vm from 'node:vm';
import {arrivalListQuery} from '../src/core/arrival-list.ts';
import {arrivalLayout} from '../src/core/arrival-layout.ts';
test('registration queue asks the server for confirmed eligible purchases, not all not-yet-accepted purchases',()=>{
assert.deepEqual(arrivalListQuery('arrival'),{pendingRegistration:true,arrivalReady:true,priceLocked:true});
assert.deepEqual(arrivalListQuery('accept'),{pendingAcceptance:true,priceLocked:true});
});
const require=createRequire(import.meta.url),React=require('react'),ts=require('typescript');
const source=readFileSync(new URL('../src/screens/ArrivalListParts.tsx',import.meta.url),'utf8');
const compiled=ts.transpileModule(source,{compilerOptions:{module:ts.ModuleKind.CommonJS,jsx:ts.JsxEmit.ReactJSX,esModuleInterop:true}}).outputText;
const exports={};
vm.runInNewContext(compiled,{exports,require:name=>({react:React,'react/jsx-runtime':require('react/jsx-runtime'),
'react-native':{ActivityIndicator:'Loading',Pressable:'Pressable',Text:'Text',TextInput:'TextInput',View:'View',useWindowDimensions:()=>({width:393,fontScale:1})},
'../core/arrival-layout':{arrivalLayout},'../ui':{Card:'Card',Icon:'Icon',useTheme:()=>({})},'../core/modules':{display:v=>v==null?'—':String(v)}}[name]||require(name))});
function nodes(tree){const all=[];function walk(n){if(!n||typeof n!=='object')return;all.push(n);React.Children.toArray(n.props?.children).forEach(walk);}walk(tree);return all;}
test('compact queue tabs have equal width, correct state, accessible search and explicit clear',()=>{
const calls=[];const tree=exports.ArrivalListTools({query:'水冷',setQuery:v=>calls.push(v),queue:'accept',setQueue:v=>calls.push(v),busy:false,search:()=>calls.push('search'),reset:()=>calls.push('reset'),total:25,page:2});
const all=nodes(tree),tabs=all.filter(n=>n.props.accessibilityRole==='tab');
assert.equal(tabs.length,2);assert.deepEqual(tabs.map(n=>n.props.style.flex),[1,1]);
assert.deepEqual(tabs.map(n=>n.props.accessibilityState.selected),[false,true]);tabs[0].props.onPress();
all.find(n=>n.type==='TextInput').props.onSubmitEditing();all.find(n=>n.props.accessibilityLabel==='清除搜索').props.onPress();
assert.deepEqual(calls,['arrival','search','reset']);
});
test('acceptance card keeps quantity and unit together without a tall fixed-size badge',()=>{
const all=nodes(exports.ArrivalCard({item:{materialName:'很长的物品名称',materialParams:'规格',remainingQuantity:0,pendingArrivalQuantity:18,unit:'很长的测试单位'},queue:'accept',children:null}));
assert.ok(all.some(n=>n.type==='Text'&&n.props.children==='18'));
assert.ok(all.some(n=>n.type==='Text'&&n.props.children==='很长的测试单位'&&n.props.style.fontSize===14));
assert.ok(all.some(n=>n.type==='Text'&&n.props.children==='待验收'));
assert.ok(all.some(n=>n.type==='View'&&n.props.style.flexDirection==='row'&&React.Children.toArray(n.props.children).some(c=>c.props?.children==='18')&&React.Children.toArray(n.props.children).some(c=>c.props?.children==='很长的测试单位')));
assert.ok(all.filter(n=>n.type==='Text').every(n=>n.props.style.fontSize>=13));
assert.ok(all.filter(n=>n.type==='View').every(n=>n.props.style.width!==94));
});
test('query state survives queue changes and pagination uses applied search rather than unsent text',()=>{
const app=readFileSync(new URL('../src/App.tsx',import.meta.url),'utf8');
assert.match(app,/arrivalListQuery\(arrivalQueue\)/);
assert.match(app,/load\(1,appliedQuery\.current\)/);
assert.match(app,/load\(page\+1,appliedQuery\.current\)/);
assert.doesNotMatch(app,/notArrived:arrivalQueue/);
});