import { describe, expect, it, vi, beforeEach } from 'vitest' import { flushPromises, mount } from '@vue/test-utils' import type { Customer, Product } from '../api' import ElementPlus from 'element-plus' import { defineComponent } from 'vue' const SelectStub = defineComponent({ name:'ElSelect', props:['modelValue'], emits:['update:modelValue'], template:'' }) const calls = vi.hoisted(() => ({ listOrders:vi.fn(),getOrder:vi.fn(),createOrder:vi.fn(),confirmPayment:vi.fn(),issueOrder:vi.fn() })) vi.mock('../orders-api', () => calls) import OrdersPanel from './OrdersPanel.vue' const order = {id:42,order_no:'test-order',customer_id:1,customer_name:'测试客户',product_id:2,product_name:'测试产品',amount_cents:100,currency:'CNY',status:'paid',payment_reference:'BANK-001',license_id:null} const props: {customers: Customer[]; products: Product[]} = {customers:[{id:1,name:'测试客户',contact_email:'a@example.test',status:'active',created_at:'',updated_at:''}],products:[{id:2,code:'test',name:'测试产品',description:'',active:true,created_at:'',updated_at:''}]} function button(wrapper:ReturnType,text:string) { return wrapper.findAll('button').find(item=>item.text()===text)! } beforeEach(()=>{vi.resetAllMocks();calls.listOrders.mockResolvedValue({data:{data:[order],meta:{total:1}}});calls.getOrder.mockResolvedValue({data:{data:order}})}) describe('OrdersPanel',()=>{ it('loads persisted orders and retrieves detail',async()=>{ const wrapper=mount(OrdersPanel,{props,global:{plugins:[ElementPlus]}});await flushPromises() expect(wrapper.text()).toContain('test-order');expect(calls.listOrders).toHaveBeenCalledWith(1) await button(wrapper,'详情').trigger('click');await flushPromises();expect(calls.getOrder).toHaveBeenCalledWith(42) wrapper.unmount() }) it('retains an unchanged create key after failure',async()=>{ calls.createOrder.mockRejectedValueOnce(new Error('network')).mockResolvedValueOnce({}) const wrapper=mount(OrdersPanel,{props,global:{plugins:[ElementPlus],stubs:{teleport:true,ElSelect:SelectStub}}});await flushPromises() await button(wrapper,'录入订单').trigger('click');await flushPromises() const selects=wrapper.findAllComponents({name:'ElSelect'}) selects[0]!.vm.$emit('update:modelValue',1);selects[1]!.vm.$emit('update:modelValue',2);await flushPromises() await button(wrapper,'创建订单').trigger('click');await flushPromises() await button(wrapper,'创建订单').trigger('click');await flushPromises() expect(calls.createOrder).toHaveBeenCalledTimes(2) expect(calls.createOrder.mock.calls[0]![1]).toBe(calls.createOrder.mock.calls[1]![1]) expect(calls.createOrder.mock.calls[0]![0]).toEqual({customer_id:1,product_id:2,amount_cents:100,currency:'CNY'}) wrapper.unmount() }) it('displays loading failures',async()=>{ calls.listOrders.mockRejectedValue(new Error('订单接口失败')) const wrapper=mount(OrdersPanel,{props,global:{plugins:[ElementPlus]}});await flushPromises() expect(wrapper.get('[role="alert"]').text()).toBe('订单接口失败');wrapper.unmount() }) })