debug.test.tsx 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import { describe, it, expect, vi } from 'vitest';
  2. import { render, screen } from '@testing-library/react';
  3. import '@testing-library/jest-dom';
  4. import { TestWrapper } from '~/utils/client/test-render';
  5. import { UsersPage } from '@/client/admin/pages/Users';
  6. import { userClient } from '@/client/api';
  7. // Mock the API client
  8. vi.mock('@/client/api', () => ({
  9. userClient: {
  10. $get: vi.fn(),
  11. $post: vi.fn(),
  12. ':id': {
  13. $put: vi.fn(),
  14. $delete: vi.fn()
  15. }
  16. }
  17. }));
  18. describe('Debug Test', () => {
  19. it('should mock the API client', async () => {
  20. // Mock the API call
  21. (userClient.$get as any).mockResolvedValue({
  22. status: 200,
  23. ok: true,
  24. json: async () => ({
  25. data: [
  26. {
  27. id: 1,
  28. username: 'testuser',
  29. nickname: '测试用户',
  30. email: 'test@example.com',
  31. phone: '13800138000',
  32. name: '测试用户',
  33. isDisabled: 0,
  34. createdAt: '2024-01-01T00:00:00.000Z',
  35. roles: [{ id: 1, name: 'admin' }]
  36. }
  37. ],
  38. pagination: {
  39. total: 1,
  40. current: 1,
  41. pageSize: 10
  42. }
  43. })
  44. });
  45. render(
  46. <TestWrapper>
  47. <UsersPage />
  48. </TestWrapper>
  49. );
  50. // Check if the mock was called
  51. expect(userClient.$get).toHaveBeenCalled();
  52. // Wait for data to load
  53. await screen.findByText('testuser');
  54. expect(screen.getByText('testuser')).toBeInTheDocument();
  55. });
  56. });