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' 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:()=>({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) wrapper.unmount() })