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:
@@ -1,59 +1,17 @@
|
||||
/**
|
||||
* 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 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', () => {
|
||||
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', () => {
|
||||
const { container } = render(<AdminOrganizationsPage />);
|
||||
|
||||
@@ -61,4 +19,10 @@ describe('AdminOrganizationsPage', () => {
|
||||
expect(containerDiv).toBeInTheDocument();
|
||||
expect(containerDiv).toHaveClass('mx-auto', 'px-6', 'py-8');
|
||||
});
|
||||
|
||||
it('renders organization management content', () => {
|
||||
render(<AdminOrganizationsPage />);
|
||||
|
||||
expect(screen.getByTestId('organization-management')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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 () => {
|
||||
|
||||
Reference in New Issue
Block a user