31 lines
2.4 KiB
JavaScript
31 lines
2.4 KiB
JavaScript
|
|
import test from 'node:test'
|
||
|
|
import assert from 'node:assert/strict'
|
||
|
|
import {purchaseGridRange,selectedGridText,clipboardTableText} from '../../src/utils/purchase-grid-clipboard.js'
|
||
|
|
import {parseClipboardTable,pasteRectangle} from '../../src/utils/purchase-sheet.js'
|
||
|
|
const rows=[{_key:'a',materialName:'物品A',materialParams:'型号\tA',quantity:2},{_key:'b',materialName:'物品B',materialParams:'两行\n型号',quantity:0}],columns=['materialName','materialParams','quantity'].map(field=>({field}))
|
||
|
|
test('rectangle selection supports reversed corners and visible row ordering',()=>{
|
||
|
|
const selection={anchor:{key:'b',field:'quantity'},focus:{key:'a',field:'materialParams'}}
|
||
|
|
assert.deepEqual(purchaseGridRange(selection,rows,columns),{top:0,bottom:1,left:1,right:2})
|
||
|
|
assert.equal(purchaseGridRange(selection,[rows[0]],columns),null)
|
||
|
|
assert.deepEqual(purchaseGridRange(selection,[rows[1],rows[0]],columns),{top:0,bottom:1,left:1,right:2})
|
||
|
|
})
|
||
|
|
test('copy exports the chosen columns only, with Excel-compatible quoted multiline TSV and zero values',()=>{
|
||
|
|
const text=selectedGridText({top:0,bottom:1,left:1,right:2},rows,columns,(row,key)=>row[key])
|
||
|
|
assert.deepEqual(parseClipboardTable(text),[['型号\tA','2'],['两行\n型号','0']])
|
||
|
|
assert.deepEqual(parseClipboardTable(clipboardTableText([['a"b','c\nd','e\tf',''] ])),[['a"b','c\nd','e\tf','']])
|
||
|
|
})
|
||
|
|
test('paste respects visible sorted rows and never partially applies invalid data',()=>{
|
||
|
|
const source=rows.map(r=>({...r,unit:'个'})),context={readonly:()=>false,items:[],units:['个'],rowIndices:[1,0]}
|
||
|
|
const result=pasteRectangle(source,'3\n4',1,0,['quantity'],context)
|
||
|
|
assert.deepEqual(result.map(r=>r.quantity),[4,3]);assert.deepEqual(source.map(r=>r.quantity),[2,0])
|
||
|
|
assert.throws(()=>pasteRectangle(source,'3\n非法',1,0,['quantity'],context),/格式不正确/)
|
||
|
|
assert.deepEqual(source.map(r=>r.quantity),[2,0])
|
||
|
|
})
|
||
|
|
test('paste rejects ragged rectangles, missing anchors and permission-protected fields',()=>{
|
||
|
|
const source=[{_key:'a',quantity:1,unit:'个'}],context={readonly:()=>false,items:[],units:['个']}
|
||
|
|
assert.throws(()=>pasteRectangle(source,'1\t2\n3',0,0,['quantity','finalUnitPrice'],context),/矩形/)
|
||
|
|
assert.throws(()=>pasteRectangle(source,'1',0,-1,['quantity'],context),/起始/)
|
||
|
|
assert.throws(()=>pasteRectangle(source,'3',0,0,['quantity'],{...context,canEditField:()=>false}),/不可通过粘贴/)
|
||
|
|
assert.equal(source[0].quantity,1)
|
||
|
|
})
|