-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSystemPages.test.tsx
More file actions
276 lines (241 loc) · 10.1 KB
/
SystemPages.test.tsx
File metadata and controls
276 lines (241 loc) · 10.1 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/**
* System Admin Pages Integration Tests
*
* Tests that system pages render the correct page header and delegate
* data rendering to the ObjectView component from @object-ui/plugin-view,
* configured with the appropriate object metadata from systemObjects.ts.
*/
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { render, screen, waitFor, fireEvent } from '@testing-library/react';
import '@testing-library/jest-dom';
import { MemoryRouter } from 'react-router-dom';
// --- Capture ObjectView props for assertion ---
let lastObjectViewProps: any = null;
vi.mock('@object-ui/plugin-view', () => ({
ObjectView: (props: any) => {
lastObjectViewProps = props;
return (
<div
data-testid="plugin-object-view"
data-objectname={props.schema?.objectName}
data-operations={JSON.stringify(props.schema?.operations)}
/>
);
},
}));
// --- Shared mock adapter ---
const mockFind = vi.fn().mockResolvedValue({ data: [], total: 0 });
const mockCreate = vi.fn().mockResolvedValue({ id: 'new-1' });
const mockDelete = vi.fn().mockResolvedValue({});
const mockAdapter = {
find: mockFind,
create: mockCreate,
delete: mockDelete,
update: vi.fn(),
findOne: vi.fn(),
getObjectSchema: vi.fn().mockResolvedValue({ name: 'test', fields: {} }),
};
vi.mock('../context/AdapterProvider', () => ({
useAdapter: () => mockAdapter,
}));
vi.mock('@object-ui/auth', () => ({
useAuth: () => ({ user: { id: 'u1', name: 'Admin', role: 'admin' } }),
}));
vi.mock('sonner', () => ({
toast: { success: vi.fn(), error: vi.fn() },
}));
const mockNavigate = vi.fn();
vi.mock('react-router-dom', async () => {
const actual = await vi.importActual('react-router-dom');
return {
...actual,
useNavigate: () => mockNavigate,
useParams: () => ({ appName: 'test-app' }),
};
});
const mockRefresh = vi.fn().mockResolvedValue(undefined);
vi.mock('../context/MetadataProvider', () => ({
useMetadata: () => ({
apps: [
{ name: 'crm', label: 'CRM', description: 'Customer management', active: true, isDefault: true },
{ name: 'hr', label: 'HR', description: 'Human resources', active: false, isDefault: false },
],
objects: [],
dashboards: [],
reports: [],
pages: [],
loading: false,
error: null,
refresh: mockRefresh,
}),
}));
// Import after mocks
import { UserManagementPage } from '../pages/system/UserManagementPage';
import { OrgManagementPage } from '../pages/system/OrgManagementPage';
import { RoleManagementPage } from '../pages/system/RoleManagementPage';
import { AuditLogPage } from '../pages/system/AuditLogPage';
import { SystemHubPage } from '../pages/system/SystemHubPage';
import { AppManagementPage } from '../pages/system/AppManagementPage';
import { PermissionManagementPage } from '../pages/system/PermissionManagementPage';
function wrap(ui: React.ReactElement) {
return render(<MemoryRouter>{ui}</MemoryRouter>);
}
beforeEach(() => {
vi.clearAllMocks();
lastObjectViewProps = null;
});
describe('UserManagementPage', () => {
it('should render ObjectView with sys_user object and page header', () => {
wrap(<UserManagementPage />);
expect(screen.getByText('User Management')).toBeInTheDocument();
expect(screen.getByText('Manage system users and their roles')).toBeInTheDocument();
expect(screen.getByTestId('plugin-object-view')).toBeInTheDocument();
expect(screen.getByTestId('plugin-object-view').dataset.objectname).toBe('sys_user');
});
it('should pass the adapter as dataSource to ObjectView', () => {
wrap(<UserManagementPage />);
expect(lastObjectViewProps.dataSource).toBe(mockAdapter);
});
it('should enable CRUD operations for admin users', () => {
wrap(<UserManagementPage />);
const ops = lastObjectViewProps.schema.operations;
expect(ops).toEqual({ create: true, update: true, delete: true });
});
it('should configure table columns from systemObjects metadata', () => {
wrap(<UserManagementPage />);
expect(lastObjectViewProps.schema.table.columns).toEqual(
['name', 'email', 'role', 'status', 'lastLoginAt']
);
});
});
describe('OrgManagementPage', () => {
it('should render ObjectView with sys_org object and page header', () => {
wrap(<OrgManagementPage />);
expect(screen.getByText('Organization Management')).toBeInTheDocument();
expect(screen.getByText('Manage organizations and their members')).toBeInTheDocument();
expect(screen.getByTestId('plugin-object-view').dataset.objectname).toBe('sys_org');
});
it('should configure table columns from systemObjects metadata', () => {
wrap(<OrgManagementPage />);
expect(lastObjectViewProps.schema.table.columns).toEqual(
['name', 'slug', 'plan', 'status', 'memberCount']
);
});
});
describe('RoleManagementPage', () => {
it('should render ObjectView with sys_role object and page header', () => {
wrap(<RoleManagementPage />);
expect(screen.getByText('Role Management')).toBeInTheDocument();
expect(screen.getByText('Define roles and assign permissions')).toBeInTheDocument();
expect(screen.getByTestId('plugin-object-view').dataset.objectname).toBe('sys_role');
});
it('should configure table columns from systemObjects metadata', () => {
wrap(<RoleManagementPage />);
expect(lastObjectViewProps.schema.table.columns).toEqual(
['name', 'description', 'isSystem', 'userCount']
);
});
});
describe('AuditLogPage', () => {
it('should render ObjectView with sys_audit_log object and page header', () => {
wrap(<AuditLogPage />);
expect(screen.getByText('Audit Log')).toBeInTheDocument();
expect(screen.getByText('View system activity and user actions')).toBeInTheDocument();
expect(screen.getByTestId('plugin-object-view').dataset.objectname).toBe('sys_audit_log');
});
it('should disable all mutation operations (read-only)', () => {
wrap(<AuditLogPage />);
const ops = lastObjectViewProps.schema.operations;
expect(ops).toEqual({ create: false, update: false, delete: false });
});
it('should configure table columns from systemObjects metadata', () => {
wrap(<AuditLogPage />);
expect(lastObjectViewProps.schema.table.columns).toEqual(
['action', 'resource', 'userName', 'ipAddress', 'createdAt']
);
});
});
describe('SystemHubPage', () => {
it('should render System Settings heading and all hub cards', async () => {
mockFind.mockResolvedValue({ data: [] });
wrap(<SystemHubPage />);
expect(screen.getByText('System Settings')).toBeInTheDocument();
await waitFor(() => {
expect(screen.getByTestId('hub-card-applications')).toBeInTheDocument();
expect(screen.getByTestId('hub-card-users')).toBeInTheDocument();
expect(screen.getByTestId('hub-card-organizations')).toBeInTheDocument();
expect(screen.getByTestId('hub-card-roles')).toBeInTheDocument();
expect(screen.getByTestId('hub-card-permissions')).toBeInTheDocument();
expect(screen.getByTestId('hub-card-audit-log')).toBeInTheDocument();
expect(screen.getByTestId('hub-card-profile')).toBeInTheDocument();
});
});
it('should fetch counts from dataSource on mount', async () => {
mockFind.mockResolvedValue({ data: [{ id: '1' }] });
wrap(<SystemHubPage />);
await waitFor(() => {
expect(mockFind).toHaveBeenCalledWith('sys_user');
expect(mockFind).toHaveBeenCalledWith('sys_org');
expect(mockFind).toHaveBeenCalledWith('sys_role');
expect(mockFind).toHaveBeenCalledWith('sys_permission');
expect(mockFind).toHaveBeenCalledWith('sys_audit_log');
});
});
it('should navigate to section when card is clicked', async () => {
mockFind.mockResolvedValue({ data: [] });
wrap(<SystemHubPage />);
await waitFor(() => {
expect(screen.getByTestId('hub-card-users')).toBeInTheDocument();
});
fireEvent.click(screen.getByTestId('hub-card-users'));
expect(mockNavigate).toHaveBeenCalledWith('/apps/test-app/system/users');
});
});
describe('AppManagementPage', () => {
it('should render app list from metadata', () => {
wrap(<AppManagementPage />);
expect(screen.getByText('Applications')).toBeInTheDocument();
expect(screen.getByTestId('app-card-crm')).toBeInTheDocument();
expect(screen.getByTestId('app-card-hr')).toBeInTheDocument();
});
it('should filter apps by search query', () => {
wrap(<AppManagementPage />);
fireEvent.change(screen.getByTestId('app-search-input'), { target: { value: 'CRM' } });
expect(screen.getByTestId('app-card-crm')).toBeInTheDocument();
expect(screen.queryByTestId('app-card-hr')).not.toBeInTheDocument();
});
it('should show empty state when no matching apps', () => {
wrap(<AppManagementPage />);
fireEvent.change(screen.getByTestId('app-search-input'), { target: { value: 'nonexistent' } });
expect(screen.getByTestId('no-apps-message')).toBeInTheDocument();
});
it('should navigate to create-app on New App button click', () => {
wrap(<AppManagementPage />);
fireEvent.click(screen.getByTestId('create-app-btn'));
expect(mockNavigate).toHaveBeenCalledWith('/apps/test-app/create-app');
});
});
describe('PermissionManagementPage', () => {
it('should render ObjectView with sys_permission object and page header', () => {
wrap(<PermissionManagementPage />);
expect(screen.getByText('Permissions')).toBeInTheDocument();
expect(screen.getByText('Manage permission rules and assignments')).toBeInTheDocument();
expect(screen.getByTestId('plugin-object-view').dataset.objectname).toBe('sys_permission');
});
it('should enable CRUD operations for admin users', () => {
wrap(<PermissionManagementPage />);
const ops = lastObjectViewProps.schema.operations;
expect(ops).toEqual({ create: true, update: true, delete: true });
});
it('should configure table columns from systemObjects metadata', () => {
wrap(<PermissionManagementPage />);
expect(lastObjectViewProps.schema.table.columns).toEqual(
['name', 'resource', 'action', 'description']
);
});
it('should enable search and filters', () => {
wrap(<PermissionManagementPage />);
expect(lastObjectViewProps.schema.showSearch).toBe(true);
expect(lastObjectViewProps.schema.showFilters).toBe(true);
});
});