import {expect,it,vi} from 'vitest' import {readFileSync} from 'node:fs' import {parse} from '@vue/compiler-sfc' import {baseParse} from '@vue/compiler-dom' import {defineComponent,nextTick} from 'vue' import {mount,flushPromises} from '@vue/test-utils' import ElementPlus from 'element-plus' import Sheet from '../../src/components/PurchaseSheet/index.vue' import {amountDisplay} from '../../src/utils/amount-display' import {queueMatches} from '../../src/utils/purchase-sheet-view' vi.mock('@/utils/request',()=>({default:vi.fn()})) vi.mock('@/store/modules/user',()=>({default:()=>({permissions:['*:*:*']})})) vi.mock('@/store/modules/app',()=>({default:()=>({size:'default'})})) it('real purchase columns, batch totals and footer keep actual amounts before quotes across display toggles',async()=>{ const template=parse(readFileSync('src/components/PurchaseSheet/index.vue','utf8')).descriptor.template.content let grid,footer;const totals=[] function visit(node){ if(node.tag==='template'&&node.props.some(p=>p.name==='for'&&p.exp?.content==='column in columns'))grid=node if(node.tag==='div'&&node.props.some(p=>p.name==='class'&&p.value?.content==='sheet-footer'))footer=node.children.find(c=>c.type===1) if(node.tag==='el-table-column'&&node.props.some(p=>p.name==='label'&&['实际总金额','报价合计'].includes(p.value?.content)))totals.push(node) for(const child of node.children||[])visit(child) } visit(baseParse(template));expect(grid).toBeTruthy();expect(footer).toBeTruthy();expect(totals).toHaveLength(2) const extract=node=>template.slice(node.loc.start.offset,node.loc.end.offset) const host=defineComponent({template:`
${extract(grid)}${totals.map(extract).join('')}
${extract(footer)}
`, data:()=>({canViewQuotes:true,compactAmounts:false,extras:false,batchProject:{mixed:false},sheet:{rows:[{_key:'r',quantity:2,unit:'个',actualUnitPrice:3,finalUnitPrice:5,actualTotalAmount:6,totalAmount:10}]},cellOwner:{},editCell:null}), computed:{columns(){return Sheet.computed.columns.call(this)},visibleRows(){return this.sheet.rows},actualTotal:()=>6,amount:()=>10,unpriced:()=>0}, methods:{selectColumn(){},originalIndex:()=>0,actualAmount:row=>row.quantity*row.actualUnitPrice,hasPrice:()=>true,money:value=>Number(value).toFixed(2)}}) const wrapper=mount(host,{global:{plugins:[ElementPlus],stubs:{PurchaseCell:true,BusinessIcon:true}}}) const headers=()=>wrapper.findAll('.detail thead th').map(h=>h.text()) const expected=['实际单价','实际金额','报价单价','报价金额'] await flushPromises();await nextTick() expect(headers().filter(h=>expected.includes(h))).toEqual(expected) for(const header of wrapper.findAll('.detail thead th').filter(h=>expected.includes(h.text())))expect(header.classes()).toContain('is-right') expect(wrapper.findAll('.batches thead th').map(h=>h.text())).toEqual(['实际总金额','报价合计']) expect(wrapper.get('.summary').text()).toMatch(/实际总金额 ¥ 6\.00.*报价总金额 ¥ 10\.00/) expect(wrapper.get('.detail tbody').text()).toContain('6.00') expect(wrapper.get('.detail tbody').text()).toContain('10.00') await wrapper.setData({compactAmounts:true});await flushPromises();await nextTick() expect(headers().filter(h=>expected.includes(h))).toEqual(['报价单价','报价金额']) expect(wrapper.findAll('.batches thead th').map(h=>h.text())).toEqual(['报价合计']) expect(wrapper.get('.summary').text()).not.toContain('实际总金额') await wrapper.setData({compactAmounts:false});await flushPromises();await nextTick() expect(headers().filter(h=>expected.includes(h))).toEqual(expected) await wrapper.setData({canViewQuotes:false});await flushPromises();await nextTick() expect(headers().filter(h=>expected.includes(h))).toEqual(['实际单价','实际金额']) expect(wrapper.findAll('.batches thead th').map(h=>h.text())).toEqual(['实际总金额']) expect(wrapper.get('.summary').text()).not.toContain('报价总金额') expect(wrapper.get('.summary').text()).toContain('实际总金额 ¥ 6.00') wrapper.unmount() }) it('employee purchase views keep actual amounts, omit quote sorts and preserve quotation workflow states',()=>{ const vm={canViewQuotes:false,extras:false,sheet:{unbatched:true},batchProject:{mixed:false}} expect(Sheet.computed.columns.call(vm).map(c=>c.field)).toContain('actualUnitPrice') expect(Sheet.computed.columns.call(vm).map(c=>c.field)).not.toContain('finalUnitPrice') expect(Sheet.computed.sortOptions.call(vm).map(c=>c.value)).not.toEqual(expect.arrayContaining(['priceAsc','priceDesc','amountDesc'])) expect(Sheet.methods.fieldLocked.call(vm,{},'finalUnitPrice')).toBe(true) const original=amountDisplay.enabled;amountDisplay.enabled=true try{expect(Sheet.computed.compactAmounts.call(vm)).toBe(false)}finally{amountDisplay.enabled=original} const row={id:1,quoteAvailable:true,finalUnitPrice:null,priceLocked:false,purchaseStatus:0} expect(Sheet.methods.hasPrice.call(vm,row)).toBe(true) expect(queueMatches(row,'confirm')).toBe(true);expect(queueMatches(row,'quote')).toBe(false) expect(Sheet.computed.canViewQuotes.call({$store:{getters:{userId:2,roles:['employee']}}})).toBe(false) expect(Sheet.computed.canViewQuotes.call({$store:{getters:{userId:2,roles:['purchase_manager']}}})).toBe(true) expect(Sheet.computed.canViewQuotes.call({$store:{getters:{userId:1,roles:[]}}})).toBe(true) })