2026-10-20 10:04:17 +08:00
import { expect , it , vi } from 'vitest'
import { reactive , nextTick , computed , watch } from 'vue'
import { createRouter , createMemoryHistory } from 'vue-router'
import Sheet from '../../src/components/PurchaseSheet/index.vue'
import { createSheetEditor } from '../../src/utils/purchase-sheet-editor'
import { writeClipboardText } from '../../src/utils/purchase-grid-clipboard'
vi . mock ( '@/utils/request' , ( ) => ( { default : vi . fn ( ) } ) )
vi . mock ( '@/store/modules/user' , ( ) => ( { default : ( ) => ( { permissions : [ '*:*:*' ] } ) } ) )
vi . mock ( '@/store/modules/app' , ( ) => ( { default : ( ) => ( { size : 'default' } ) } ) )
2026-10-27 21:17:27 +08:00
it . each ( [ 'number' , 'text' ] ) ( 'typing into a %s grid input does not invoke unsupported native text selection' , async type => {
const root = document . createElement ( 'div' ) , cell = document . createElement ( 'div' ) , input = document . createElement ( 'input' )
cell . className = 'sheet-cell' ; cell . dataset . row = '0' ; cell . dataset . field = 'quantity' ; input . type = type ; cell . append ( input ) ; root . append ( cell )
const entered = vi . fn ( ) ; input . addEventListener ( 'input' , entered )
const vm = { $el : root , editableCell : ( ) => true , activateCell : vi . fn ( ) , originalIndex : ( ) => 0 }
await expect ( Sheet . methods . typeGridCell . call ( vm , { } , 'quantity' , '12' ) ) . resolves . toBeUndefined ( )
expect ( input . value ) . toBe ( '12' ) ; expect ( entered ) . toHaveBeenCalledOnce ( ) ; expect ( vm . _typingGrid ) . toBe ( false )
if ( type === 'text' ) expect ( input . selectionStart ) . toBe ( 2 )
} )
2026-10-20 10:04:17 +08:00
it ( 'compact display filters rows without removing them from complete purchase saves' , ( ) => {
const rows = [ { _key : 'zero' , id : 1 , finalUnitPrice : 0 , purchaseStatus : 0 } , { _key : 'priced' , id : 2 , finalUnitPrice : 3 , purchaseStatus : 0 } ]
const vm = { sheet : { rows } , rowFilter : '' , rowQueue : 'all' , rowSort : 'original' , onlyInvalid : false , compactAmounts : true , isEditing : false , viewReference : r => r , projectName : ( ) => '' }
vm . amountRowVisible = Sheet . methods . amountRowVisible . bind ( vm )
expect ( Sheet . computed . visibleRows . call ( vm ) . map ( r => r . id ) ) . toEqual ( [ 2 ] )
expect ( Sheet . methods . activeRows . call ( vm ) . map ( r => r . id ) ) . toEqual ( [ 1 , 2 ] )
vm . isEditing = true ; expect ( vm . amountRowVisible ( { _key : 'new' , finalUnitPrice : 0 } ) ) . toBe ( true )
} )
it ( 'viewing a moved target navigates to the full procurement route, not an embedded sheet replacement' , async ( ) => {
const push = vi . fn ( ) , openBatch = vi . fn ( ) , vm = { moveOutcome : 72 , saving : false , embedded : true , $route : { path : '/project/detail/1501' , query : { } } , $router : { push } , openBatch }
await Sheet . methods . viewMoveTarget . call ( vm )
expect ( push ) . toHaveBeenCalledWith ( { path : '/procurement/details' , query : { purchaseBatchId : '72' } } )
expect ( openBatch ) . not . toHaveBeenCalled ( ) ; expect ( vm . moveOutcome ) . toBeNull ( )
} )
it ( 'returning from a deep-linked batch removes its route parameter so reload and reopen work' , async ( ) => {
const replace = vi . fn ( ) , loadBatches = vi . fn ( ) , vm = { saving : false , dirty : false , sheet : { batch : { id : 72 } } , embedded : false , $route : { path : '/procurement/details' , query : { purchaseBatchId : '72' , projectId : '9' } } , $router : { replace } , loadBatches }
await Sheet . methods . back . call ( vm )
expect ( replace ) . toHaveBeenCalledWith ( { path : '/procurement/details' , query : { projectId : '9' } } )
expect ( vm . sheet ) . toBeNull ( ) ; expect ( loadBatches ) . not . toHaveBeenCalled ( )
} )
it ( 'target route navigation and back cooperate with the actual route watcher and query restoration' , async ( ) => {
const router = createRouter ( { history : createMemoryHistory ( ) , routes : [ { path : '/procurement/details' , component : { template : '<div/>' } } ] } )
await router . push ( '/procurement/details?purchaseBatchId=11' ) ; await router . isReady ( )
const vm = reactive ( { moveOutcome : 72 , saving : false , dirty : false , embedded : false , sheet : { batch : { id : 11 } } , query : { } , projectId : null , $router : router , _routeStamp : undefined , loadBatches : vi . fn ( ) , openBatch : vi . fn ( async batch => { vm . sheet = { batch } } ) } )
Object . defineProperty ( vm , '$route' , { get : ( ) => router . currentRoute . value } )
const stop = watch ( ( ) => router . currentRoute . value . fullPath , ( ) => Sheet . methods . applyRoute . call ( vm ) )
try { await Sheet . methods . viewMoveTarget . call ( vm ) ; await nextTick ( ) ; expect ( router . currentRoute . value . query . purchaseBatchId ) . toBe ( '72' ) ; expect ( vm . sheet . batch . id ) . toBe ( 72 )
await Sheet . methods . back . call ( vm ) ; await nextTick ( ) ; expect ( router . currentRoute . value . query . purchaseBatchId ) . toBeUndefined ( ) ; expect ( vm . sheet ) . toBeNull ( ) ; expect ( vm . loadBatches ) . toHaveBeenCalled ( )
} finally { stop ( ) }
} )
it ( 'the actual undo/redo handlers commit buffered inputs and retain editor focus for repeated shortcuts' , async ( ) => {
const root = document . createElement ( 'div' ) , input = document . createElement ( 'input' ) ; root . tabIndex = - 1 ; root . append ( input ) ; document . body . append ( root )
const vm = reactive ( { sheet : null , baseline : '' , editing : true , isEditing : true , saving : false , history : [ ] , redoHistory : [ ] , selected : [ ] , changedKeys : new Set ( ) , persistDraft ( ) { } , $el : root , $nextTick : nextTick , focusSelectedCell : vi . fn ( ) } )
const editor = createSheetEditor ( vm ) ; vm . _editor = editor ; vm . finishCellEdit = Sheet . methods . finishCellEdit . bind ( vm )
vm . sheet = { batch : { } , rows : [ { _key : 'r' , quantity : 1 } ] , deletedIds : [ ] } ; vm . baseline = JSON . stringify ( vm . sheet )
const canUndo = computed ( ( ) => Sheet . computed . canUndo . call ( vm ) )
try { editor . beginCell ( vm . sheet . rows [ 0 ] , 'quantity' ) ; input . focus ( ) ; input . addEventListener ( 'blur' , ( ) => { vm . sheet . rows [ 0 ] . quantity = 4 } , { once : true } )
await Sheet . methods . undo . call ( vm ) ; expect ( vm . sheet . rows [ 0 ] . quantity ) . toBe ( 1 ) ; expect ( document . activeElement ) . toBe ( root )
await Sheet . methods . redo . call ( vm ) ; expect ( vm . sheet . rows [ 0 ] . quantity ) . toBe ( 4 ) ; expect ( canUndo . value ) . toBe ( true )
await Sheet . methods . undo . call ( vm ) ; expect ( canUndo . value ) . toBe ( false )
} finally { editor . stop ( ) ; root . remove ( ) }
} )
it ( 'spreadsheet copy uses the modern clipboard in secure contexts and falls back on internal HTTP' , async ( ) => {
const copy = vi . fn ( ) , exec = vi . fn ( ( ) => true )
vi . stubGlobal ( 'isSecureContext' , true ) ; Object . defineProperty ( navigator , 'clipboard' , { configurable : true , value : { writeText : copy } } )
try { await writeClipboardText ( 'A\tB' ) ; expect ( copy ) . toHaveBeenCalledWith ( 'A\tB' )
vi . stubGlobal ( 'isSecureContext' , false ) ; Object . defineProperty ( document , 'execCommand' , { configurable : true , value : exec } ) ; await writeClipboardText ( 'C\tD' ) ; expect ( exec ) . toHaveBeenCalledWith ( 'copy' ) ; expect ( document . querySelector ( 'textarea' ) ) . toBeNull ( )
} finally { vi . unstubAllGlobals ( ) ; delete navigator . clipboard ; delete document . execCommand }
} )
it ( 'search input retains native shortcuts while grid shortcuts are intercepted exactly once' , ( ) => {
const root = document . createElement ( 'div' ) , field = document . createElement ( 'input' ) ; field . className = 'search' ; root . append ( field )
const undo = vi . fn ( ) , redo = vi . fn ( ) , vm = { isEditing : true , saving : false , $el : root , undo , redo }
const event = { target : field , key : 'z' , ctrlKey : true , preventDefault : vi . fn ( ) , stopPropagation : vi . fn ( ) }
Sheet . methods . editorShortcut . call ( vm , event ) ; expect ( undo ) . not . toHaveBeenCalled ( ) ; expect ( event . preventDefault ) . not . toHaveBeenCalled ( )
event . target = root ; Sheet . methods . editorShortcut . call ( vm , event ) ; expect ( undo ) . toHaveBeenCalledOnce ( )
event . shiftKey = true ; Sheet . methods . editorShortcut . call ( vm , event ) ; expect ( redo ) . toHaveBeenCalledOnce ( )
} )
2026-10-27 21:17:27 +08:00
it ( 'a virtual window at the end retains the last row when density changes its capacity' , ( ) => {
const vm = { sheet : { } , virtualized : true , visibleRows : Array . from ( { length : 500 } ) , virtualStart : 476 }
Sheet . watch . virtualCount . call ( vm , 23 , 24 )
expect ( vm . virtualStart ) . toBe ( 477 ) ; expect ( vm . virtualStart + 23 ) . toBe ( 500 )
vm . virtualStart = 200 ; Sheet . watch . virtualCount . call ( vm , 25 , 23 ) ; expect ( vm . virtualStart ) . toBe ( 200 )
vm . virtualStart = 477 ; Sheet . watch . virtualCount . call ( vm , 28 , 23 ) ; expect ( vm . virtualStart ) . toBe ( 472 )
} )
it ( 'closing a sheet leaves an empty virtual view without dereferencing its old rows' , ( ) => { expect ( Sheet . computed . visibleRows . call ( { sheet : null , rowFilter : '' , rowSort : 'original' } ) ) . toEqual ( [ ] ) ; expect ( ( ) => Sheet . watch . virtualCount . call ( { sheet : null } , 24 , 23 ) ) . not . toThrow ( ) } )