|
| 1 | +/** |
| 2 | + * System Admin Pages Integration Tests |
| 3 | + * |
| 4 | + * Tests that system pages (User, Org, Role, AuditLog) fetch data |
| 5 | + * via useAdapter() and render records from the API. |
| 6 | + */ |
| 7 | + |
| 8 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 9 | +import { render, screen, waitFor, fireEvent } from '@testing-library/react'; |
| 10 | +import '@testing-library/jest-dom'; |
| 11 | +import { MemoryRouter } from 'react-router-dom'; |
| 12 | + |
| 13 | +// --- Shared mock adapter --- |
| 14 | +const mockFind = vi.fn().mockResolvedValue({ data: [], total: 0 }); |
| 15 | +const mockCreate = vi.fn().mockResolvedValue({ id: 'new-1' }); |
| 16 | +const mockDelete = vi.fn().mockResolvedValue({}); |
| 17 | + |
| 18 | +vi.mock('../context/AdapterProvider', () => ({ |
| 19 | + useAdapter: () => ({ |
| 20 | + find: mockFind, |
| 21 | + create: mockCreate, |
| 22 | + delete: mockDelete, |
| 23 | + update: vi.fn(), |
| 24 | + findOne: vi.fn(), |
| 25 | + }), |
| 26 | +})); |
| 27 | + |
| 28 | +vi.mock('@object-ui/auth', () => ({ |
| 29 | + useAuth: () => ({ user: { id: 'u1', name: 'Admin', role: 'admin' } }), |
| 30 | +})); |
| 31 | + |
| 32 | +vi.mock('sonner', () => ({ |
| 33 | + toast: { success: vi.fn(), error: vi.fn() }, |
| 34 | +})); |
| 35 | + |
| 36 | +// Import after mocks |
| 37 | +import { UserManagementPage } from '../pages/system/UserManagementPage'; |
| 38 | +import { OrgManagementPage } from '../pages/system/OrgManagementPage'; |
| 39 | +import { RoleManagementPage } from '../pages/system/RoleManagementPage'; |
| 40 | +import { AuditLogPage } from '../pages/system/AuditLogPage'; |
| 41 | + |
| 42 | +function wrap(ui: React.ReactElement) { |
| 43 | + return render(<MemoryRouter>{ui}</MemoryRouter>); |
| 44 | +} |
| 45 | + |
| 46 | +beforeEach(() => { |
| 47 | + vi.clearAllMocks(); |
| 48 | +}); |
| 49 | + |
| 50 | +describe('UserManagementPage', () => { |
| 51 | + it('should call dataSource.find("sys_user") on mount', async () => { |
| 52 | + mockFind.mockResolvedValueOnce({ |
| 53 | + data: [{ id: '1', name: 'Alice', email: 'alice@test.com', role: 'admin', status: 'active', lastLoginAt: '' }], |
| 54 | + }); |
| 55 | + wrap(<UserManagementPage />); |
| 56 | + await waitFor(() => { |
| 57 | + expect(mockFind).toHaveBeenCalledWith('sys_user'); |
| 58 | + }); |
| 59 | + expect(screen.getByText('Alice')).toBeInTheDocument(); |
| 60 | + }); |
| 61 | + |
| 62 | + it('should show empty state when no users', async () => { |
| 63 | + mockFind.mockResolvedValueOnce({ data: [] }); |
| 64 | + wrap(<UserManagementPage />); |
| 65 | + await waitFor(() => { |
| 66 | + expect(screen.getByText('No users found.')).toBeInTheDocument(); |
| 67 | + }); |
| 68 | + }); |
| 69 | + |
| 70 | + it('should call create when Add User is clicked', async () => { |
| 71 | + mockFind.mockResolvedValue({ data: [] }); |
| 72 | + mockCreate.mockResolvedValueOnce({ id: 'new-user' }); |
| 73 | + wrap(<UserManagementPage />); |
| 74 | + await waitFor(() => { |
| 75 | + expect(screen.getByText('No users found.')).toBeInTheDocument(); |
| 76 | + }); |
| 77 | + fireEvent.click(screen.getByText('Add User')); |
| 78 | + await waitFor(() => { |
| 79 | + expect(mockCreate).toHaveBeenCalledWith('sys_user', expect.objectContaining({ name: 'New User' })); |
| 80 | + }); |
| 81 | + }); |
| 82 | +}); |
| 83 | + |
| 84 | +describe('OrgManagementPage', () => { |
| 85 | + it('should call dataSource.find("sys_org") on mount', async () => { |
| 86 | + mockFind.mockResolvedValueOnce({ |
| 87 | + data: [{ id: '1', name: 'Acme', slug: 'acme', plan: 'pro', status: 'active', memberCount: 5 }], |
| 88 | + }); |
| 89 | + wrap(<OrgManagementPage />); |
| 90 | + await waitFor(() => { |
| 91 | + expect(mockFind).toHaveBeenCalledWith('sys_org'); |
| 92 | + }); |
| 93 | + expect(screen.getByText('Acme')).toBeInTheDocument(); |
| 94 | + }); |
| 95 | + |
| 96 | + it('should show empty state when no organizations', async () => { |
| 97 | + mockFind.mockResolvedValueOnce({ data: [] }); |
| 98 | + wrap(<OrgManagementPage />); |
| 99 | + await waitFor(() => { |
| 100 | + expect(screen.getByText('No organizations found.')).toBeInTheDocument(); |
| 101 | + }); |
| 102 | + }); |
| 103 | +}); |
| 104 | + |
| 105 | +describe('RoleManagementPage', () => { |
| 106 | + it('should call dataSource.find("sys_role") on mount', async () => { |
| 107 | + mockFind.mockResolvedValueOnce({ |
| 108 | + data: [{ id: '1', name: 'Admin', description: 'Full access', isSystem: true, userCount: 3 }], |
| 109 | + }); |
| 110 | + wrap(<RoleManagementPage />); |
| 111 | + await waitFor(() => { |
| 112 | + expect(mockFind).toHaveBeenCalledWith('sys_role'); |
| 113 | + }); |
| 114 | + expect(screen.getByText('Admin')).toBeInTheDocument(); |
| 115 | + }); |
| 116 | + |
| 117 | + it('should show empty state when no roles', async () => { |
| 118 | + mockFind.mockResolvedValueOnce({ data: [] }); |
| 119 | + wrap(<RoleManagementPage />); |
| 120 | + await waitFor(() => { |
| 121 | + expect(screen.getByText('No roles found.')).toBeInTheDocument(); |
| 122 | + }); |
| 123 | + }); |
| 124 | +}); |
| 125 | + |
| 126 | +describe('AuditLogPage', () => { |
| 127 | + it('should call dataSource.find("sys_audit_log") on mount', async () => { |
| 128 | + mockFind.mockResolvedValueOnce({ |
| 129 | + data: [{ id: '1', action: 'create', resource: 'user', userName: 'Admin', ipAddress: '127.0.0.1', createdAt: '2026-01-01' }], |
| 130 | + }); |
| 131 | + wrap(<AuditLogPage />); |
| 132 | + await waitFor(() => { |
| 133 | + expect(mockFind).toHaveBeenCalledWith('sys_audit_log', expect.objectContaining({ $orderby: 'createdAt desc' })); |
| 134 | + }); |
| 135 | + expect(screen.getByText('create')).toBeInTheDocument(); |
| 136 | + }); |
| 137 | + |
| 138 | + it('should show empty state when no logs', async () => { |
| 139 | + mockFind.mockResolvedValueOnce({ data: [] }); |
| 140 | + wrap(<AuditLogPage />); |
| 141 | + await waitFor(() => { |
| 142 | + expect(screen.getByText('No audit logs found.')).toBeInTheDocument(); |
| 143 | + }); |
| 144 | + }); |
| 145 | +}); |
0 commit comments