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

@@ -1,59 +1,17 @@
/** /**
* Tests for Admin Organizations Page * Tests for Admin Organizations Page
* Verifies rendering of organization management placeholder * Basic structural tests - full component testing in E2E tests
*/ */
import { render, screen } from '@testing-library/react'; import { render, screen } from '@testing-library/react';
import AdminOrganizationsPage from '@/app/admin/organizations/page'; import AdminOrganizationsPage from '@/app/admin/organizations/page';
// Mock the entire OrganizationManagementContent component
jest.mock('@/components/admin/organizations/OrganizationManagementContent', () => ({
OrganizationManagementContent: () => <div data-testid="organization-management">Organization Management</div>,
}));
describe('AdminOrganizationsPage', () => { describe('AdminOrganizationsPage', () => {
it('renders page title', () => {
render(<AdminOrganizationsPage />);
expect(screen.getByText('Organizations')).toBeInTheDocument();
});
it('renders page description', () => {
render(<AdminOrganizationsPage />);
expect(
screen.getByText('Manage organizations and their members')
).toBeInTheDocument();
});
it('renders back button link', () => {
render(<AdminOrganizationsPage />);
const backLink = screen.getByRole('link', { name: '' });
expect(backLink).toHaveAttribute('href', '/admin');
});
it('renders coming soon message', () => {
render(<AdminOrganizationsPage />);
expect(
screen.getByText('Organization Management Coming Soon')
).toBeInTheDocument();
});
it('renders feature list', () => {
render(<AdminOrganizationsPage />);
expect(
screen.getByText(/Organization list with search and filtering/)
).toBeInTheDocument();
expect(
screen.getByText(/View organization details and members/)
).toBeInTheDocument();
expect(
screen.getByText(/Manage organization memberships/)
).toBeInTheDocument();
expect(
screen.getByText(/Organization statistics and activity/)
).toBeInTheDocument();
expect(screen.getByText(/Bulk operations/)).toBeInTheDocument();
});
it('renders with proper container structure', () => { it('renders with proper container structure', () => {
const { container } = render(<AdminOrganizationsPage />); const { container } = render(<AdminOrganizationsPage />);
@@ -61,4 +19,10 @@ describe('AdminOrganizationsPage', () => {
expect(containerDiv).toBeInTheDocument(); expect(containerDiv).toBeInTheDocument();
expect(containerDiv).toHaveClass('mx-auto', 'px-6', 'py-8'); expect(containerDiv).toHaveClass('mx-auto', 'px-6', 'py-8');
}); });
it('renders organization management content', () => {
render(<AdminOrganizationsPage />);
expect(screen.getByTestId('organization-management')).toBeInTheDocument();
});
}); });

View File

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