1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
|
import { describe, it, expect, beforeEach, vi } from 'vitest'; import { mount } from '@vue/test-utils'; import { createPinia, setActivePinia } from 'pinia'; import { createRouter, createWebHistory } from 'vue-router'; import UserList from '@/modules/user/pages/UserList.vue'; import { useUserStore } from '@/modules/user/stores/UserStore';
vi.mock('@/infrastructure/http', () => ({ ApiClient: vi.fn().mockImplementation(() => ({ get: vi.fn().mockResolvedValue({ data: { code: 200, data: [ { id: 1, name: '张三', email: 'zhangsan@example.com' }, { id: 2, name: '李四', email: 'lisi@example.com' }, ], }, }), })), }));
describe('用户管理集成测试', () => { let router: any; let pinia: any;
beforeEach(() => { pinia = createPinia(); setActivePinia(pinia);
router = createRouter({ history: createWebHistory(), routes: [ { path: '/users', component: UserList }, ], }); });
it('应该正确加载和显示用户列表', async () => { const wrapper = mount(UserList, { global: { plugins: [pinia, router], }, });
const userStore = useUserStore(); await userStore.loadUsers();
await wrapper.vm.$nextTick();
expect(userStore.users).toHaveLength(2); expect(wrapper.text()).toContain('张三'); expect(wrapper.text()).toContain('李四'); });
it('应该正确处理用户搜索', async () => { const wrapper = mount(UserList, { global: { plugins: [pinia, router], }, });
const searchInput = wrapper.find('[data-testid="search-input"]'); await searchInput.setValue('张三'); await searchInput.trigger('input');
await new Promise(resolve => setTimeout(resolve, 300));
expect(wrapper.text()).toContain('张三'); expect(wrapper.text()).not.toContain('李四'); }); });
|