cmvr-wms/ruoyi-ui-vue3/tests/runtime/assistant-pagination.spec.js

36 lines
2.7 KiB
JavaScript
Raw Normal View History

import { expect, it, vi } from 'vitest'
import { defineComponent, effectScope, nextTick, reactive } from 'vue'
import { mount, shallowMount, flushPromises } from '@vue/test-utils'
import ElementPlus from 'element-plus'
import { useAssistantPagination } from '@/utils/assistant-pagination'
import Preview from '@/components/BusinessAssistant/AssistantReferencePricePreview.vue'
import Draft from '@/components/BusinessAssistant/AssistantDraft.vue'
vi.mock('@/components/BusinessAssistant/AssistantField.vue',()=>({default:{template:'<span/>'}}))
it('defaults to 50 rows, supports 20/50/100, resets on resizing, and clamps after exclusions',()=>{
const scope=effectScope(),rows=reactive(Array.from({length:121},(_,id)=>({id})))
try{
const pager=scope.run(()=>useAssistantPagination(()=>rows))
expect(pager.pageRows.value).toHaveLength(50);expect(pager.pageSizes).toEqual([20,50,100])
pager.page.value=3;expect(pager.pageRows.value).toHaveLength(21)
pager.pageSize.value=100;expect(pager.page.value).toBe(1);expect(pager.pageRows.value).toHaveLength(100)
pager.page.value=2;rows.splice(10);expect(pager.page.value).toBe(1);expect(pager.pageRows.value).toHaveLength(10)
}finally{scope.stop()}
})
it('a real AI price preview displays all 34 quote items on its first page',async()=>{
const wrapper=mount(Preview,{props:{rows:Array.from({length:34},(_,id)=>({name:`报价物品${id+1}`,quantity:1,newActualPrice:id,newActualAmount:id}))},global:{plugins:[ElementPlus]}})
try{await flushPromises();expect(wrapper.findAll('.el-table__body tbody tr')).toHaveLength(34);expect(wrapper.find('.el-pagination').text()).toContain('34')}
finally{wrapper.unmount()}
})
it('the AI confirmation table renders 50 rows and changing to 100 resets the page without losing rows',async()=>{
const table=defineComponent({props:['data'],template:'<div><slot/></div>'}),pager=defineComponent({emits:['update:currentPage','update:pageSize'],template:'<div/>'})
const rows=Array.from({length:103},(_,id)=>({_rowId:String(id),userName:`测试用户${id}`}))
const wrapper=shallowMount(Draft,{props:{draft:{id:'test-pagination',version:1,status:'DRAFT',action:'user.create',fields:[{key:'userName',label:'账号'}]},rows},global:{stubs:{ElTable:table,ElTableColumn:true,ElPagination:pager}}})
try{
expect(wrapper.findComponent(table).props('data')).toHaveLength(50)
wrapper.findComponent(pager).vm.$emit('update:currentPage',3);await nextTick();expect(wrapper.findComponent(table).props('data')).toHaveLength(3)
wrapper.findComponent(pager).vm.$emit('update:pageSize',100);await nextTick();expect(wrapper.findComponent(table).props('data')).toHaveLength(100)
expect(rows).toHaveLength(103);expect(wrapper.emitted('change')).toBeUndefined()
}finally{wrapper.unmount()}
})