Refactor Admin Organizations tests and enhance useAdmin hooks with session stats

- Simplified `AdminOrganizationsPage` tests by mocking `OrganizationManagementContent` and focusing on essential structure and content rendering.
- Updated `useAdmin` hooks to integrate `adminListSessions` and accommodate session statistics in superuser scenarios.
- Added relevant test coverage for session data fetching and validation.
This commit is contained in:
Felipe Cardoso
2025-11-06 20:10:38 +01:00
parent ff758f5d10
commit 234c197ee1
2 changed files with 27 additions and 49 deletions

View File

@@ -19,6 +19,7 @@ import {
import {
adminListUsers,
adminListOrganizations,
adminListSessions,
adminCreateUser,
adminUpdateUser,
adminDeleteUser,
@@ -34,6 +35,7 @@ jest.mock('@/lib/auth/AuthContext');
const mockAdminListUsers = adminListUsers as jest.MockedFunction<typeof adminListUsers>;
const mockAdminListOrganizations = adminListOrganizations as jest.MockedFunction<typeof adminListOrganizations>;
const mockAdminListSessions = adminListSessions as jest.MockedFunction<typeof adminListSessions>;
const mockUseAuth = useAuth as jest.MockedFunction<typeof useAuth>;
describe('useAdmin hooks', () => {
@@ -74,6 +76,12 @@ describe('useAdmin hooks', () => {
},
};
const mockSessionsData = {
data: {
pagination: { total: 10 },
},
};
it('fetches and calculates stats when user is superuser', async () => {
mockUseAuth.mockReturnValue({
user: { is_superuser: true } as any,
@@ -85,6 +93,7 @@ describe('useAdmin hooks', () => {
mockAdminListUsers.mockResolvedValue(mockUsersData as any);
mockAdminListOrganizations.mockResolvedValue(mockOrgsData as any);
mockAdminListSessions.mockResolvedValue(mockSessionsData as any);
const { result } = renderHook(() => useAdminStats(), { wrapper });
@@ -94,7 +103,7 @@ describe('useAdmin hooks', () => {
totalUsers: 3,
activeUsers: 2,
totalOrganizations: 5,
totalSessions: 0,
totalSessions: 10,
});
expect(mockAdminListUsers).toHaveBeenCalledWith({
@@ -106,6 +115,11 @@ describe('useAdmin hooks', () => {
query: { page: 1, limit: 100 },
throwOnError: false,
});
expect(mockAdminListSessions).toHaveBeenCalledWith({
query: { page: 1, limit: 100 },
throwOnError: false,
});
});
it('does not fetch when user is not superuser', async () => {