- 为对话框添加防止意外关闭的安全措施 - 实现表单提交时的数据有效性验证 - 集成远程选择器组件提升用户体验 - 更新表格组件样式适配新设计规范 - 优化列表页面加载和错误处理机制 - 统一组件尺寸设置和存储管理
47 lines
3.3 KiB
JavaScript
47 lines
3.3 KiB
JavaScript
import test from 'node:test'
|
|
import assert from 'node:assert/strict'
|
|
import { parseClipboardTable, pasteRectangle, collectRowErrors, validateRows } from '../../src/utils/purchase-sheet.js'
|
|
|
|
test('purchase drafts reject unmaintained dictionaries but preserve existing catalog references', () => {
|
|
const row={materialName:'电机',unit:'临时单位',category:'临时分类',projectId:1,expenseCategoryId:1,quantity:1}
|
|
assert.deepEqual(collectRowErrors([row],[],[],['个'],['机械件']).map(e=>e.field),['category','unit'])
|
|
assert.deepEqual(collectRowErrors([{...row,unit:'个',category:'机械件'}],[],[],['个'],['机械件']),[])
|
|
const item={id:1,productName:'电机',unit:'临时单位',category:'临时分类'}
|
|
assert.deepEqual(collectRowErrors([{...row,productId:1}],[item],[],['个'],['机械件']),[])
|
|
})
|
|
|
|
test('Excel quoted TSV keeps newlines, tabs, escaped quotes and empty cells in their cells', () => {
|
|
assert.deepEqual(parseClipboardTable('名称\t备注\r\n电机\t"第一行\n第二行\t\"\"尺寸\"\""\r\n'), [['名称','备注'],['电机','第一行\n第二行\t"尺寸"']])
|
|
assert.deepEqual(parseClipboardTable('a\t\t\n'), [['a','','']])
|
|
assert.throws(()=>parseClipboardTable('"unterminated'), /未闭合/)
|
|
assert.throws(()=>parseClipboardTable('"closed"bad'), /格式不正确/)
|
|
})
|
|
|
|
test('multiline Excel cell never creates unintended purchase rows; failed paste is atomic', () => {
|
|
const rows=[{_key:'a',materialName:'电机',unit:'个',quantity:1}]
|
|
const context={emptyRow:()=>({_key:'b',quantity:1,unit:'个'}),readonly:()=>false,items:[],units:['个']}
|
|
const pasted=pasteRectangle(rows,'"第一行\n第二行"',0,0,['remark'],context)
|
|
assert.equal(pasted.length,1)
|
|
assert.equal(pasted[0].remark,'第一行\n第二行')
|
|
assert.equal(rows[0].remark,undefined)
|
|
assert.throws(()=>pasteRectangle(rows,'"未闭合',0,0,['remark'],context))
|
|
assert.equal(rows[0].remark,undefined)
|
|
})
|
|
|
|
test('all row and field errors are collected with stable keys and original line numbers', () => {
|
|
const rows=[{_key:'first',materialName:'电机',quantity:0,unit:'个',category:'材料'}, {_key:'second',materialName:'控制器',projectId:1,expenseCategoryId:1,quantity:1,unit:'个',category:'材料',finalUnitPrice:-1,purchasePurpose:'REPAY'}]
|
|
const errors=collectRowErrors(rows,[],[11,25],['个'])
|
|
assert.deepEqual(errors.map(e=>[e.rowKey,e.line,e.field]),[['first',11,'projectId'],['first',11,'expenseCategoryId'],['first',11,'quantity'],['second',25,'finalUnitPrice'],['second',25,'loanId']])
|
|
assert.equal(rows[0].productId,undefined)
|
|
assert.throws(()=>validateRows(rows,[],[11,25]),error=>error.fields.length===5&&error.message.includes('第 11 行'))
|
|
})
|
|
|
|
test('resolved item categories and zero price remain valid, conflicting units mark both rows', () => {
|
|
const item={id:1,productName:'电机',brand:'A',spec:'10',unit:'个',category:'设备'}
|
|
const row={_key:'a',materialName:'电机',materialBrand:'A',materialParams:'10',unit:'个',category:'',projectId:1,expenseCategoryId:1,quantity:1,finalUnitPrice:0}
|
|
assert.deepEqual(collectRowErrors([row],[item]),[])
|
|
validateRows([row],[item]);assert.equal(row.productId,1)
|
|
const conflict=collectRowErrors([row,{...row,_key:'b',unit:'件'}],[])
|
|
assert.deepEqual(new Set(conflict.filter(e=>e.field==='unit').map(e=>e.rowKey)),new Set(['a','b']))
|
|
})
|