Add search and filtering functionality to useAdminUsers hook and associated components

- Enhanced `useAdminUsers` to support `search`, `is_active`, and `is_superuser` filters.
- Updated `UserManagementContent` to read filters from URL parameters and convert them to API-compatible formats.
- Introduced E2E and unit tests to validate filtering behavior and URL param synchronization.
- Ensured proper handling of combined filters and empty states in tests.
This commit is contained in:
Felipe Cardoso
2025-11-06 15:35:13 +01:00
parent 7556353078
commit 5f3a098403
5 changed files with 260 additions and 8 deletions

View File

@@ -391,7 +391,7 @@ describe('UserManagementContent', () => {
renderWithProviders(<UserManagementContent />);
expect(mockUseAdminUsers).toHaveBeenCalledWith(2, 20);
expect(mockUseAdminUsers).toHaveBeenCalledWith(2, 20, null, null, null);
});
it('reads search query from URL params', () => {
@@ -400,9 +400,34 @@ describe('UserManagementContent', () => {
renderWithProviders(<UserManagementContent />);
// Component should read the search param
// This is tested implicitly through the component render
expect(screen.getByTestId('user-list-table')).toBeInTheDocument();
expect(mockUseAdminUsers).toHaveBeenCalledWith(1, 20, 'test', null, null);
});
it('reads active filter from URL params', () => {
const paramsWithActive = new URLSearchParams('active=true');
mockUseSearchParams.mockReturnValue(paramsWithActive as any);
renderWithProviders(<UserManagementContent />);
expect(mockUseAdminUsers).toHaveBeenCalledWith(1, 20, null, true, null);
});
it('reads superuser filter from URL params', () => {
const paramsWithSuperuser = new URLSearchParams('superuser=false');
mockUseSearchParams.mockReturnValue(paramsWithSuperuser as any);
renderWithProviders(<UserManagementContent />);
expect(mockUseAdminUsers).toHaveBeenCalledWith(1, 20, null, null, false);
});
it('reads all params from URL', () => {
const params = new URLSearchParams('page=3&search=admin&active=true&superuser=true');
mockUseSearchParams.mockReturnValue(params as any);
renderWithProviders(<UserManagementContent />);
expect(mockUseAdminUsers).toHaveBeenCalledWith(3, 20, 'admin', true, true);
});
it('passes current user ID to table', () => {

View File

@@ -260,6 +260,117 @@ describe('useAdmin hooks', () => {
await waitFor(() => expect(result.current.isError).toBe(true));
expect(result.current.error).toBeDefined();
});
it('passes search parameter to API', async () => {
mockUseAuth.mockReturnValue({
user: { is_superuser: true } as any,
isAuthenticated: true,
isLoading: false,
login: jest.fn(),
logout: jest.fn(),
});
mockAdminListUsers.mockResolvedValue(mockResponse as any);
renderHook(() => useAdminUsers(1, 50, 'test@example.com'), { wrapper });
await waitFor(() => {
expect(mockAdminListUsers).toHaveBeenCalledWith({
query: { page: 1, limit: 50, search: 'test@example.com' },
throwOnError: false,
});
});
});
it('passes is_active filter parameter to API', async () => {
mockUseAuth.mockReturnValue({
user: { is_superuser: true } as any,
isAuthenticated: true,
isLoading: false,
login: jest.fn(),
logout: jest.fn(),
});
mockAdminListUsers.mockResolvedValue(mockResponse as any);
renderHook(() => useAdminUsers(1, 50, null, true), { wrapper });
await waitFor(() => {
expect(mockAdminListUsers).toHaveBeenCalledWith({
query: { page: 1, limit: 50, is_active: true },
throwOnError: false,
});
});
});
it('passes is_superuser filter parameter to API', async () => {
mockUseAuth.mockReturnValue({
user: { is_superuser: true } as any,
isAuthenticated: true,
isLoading: false,
login: jest.fn(),
logout: jest.fn(),
});
mockAdminListUsers.mockResolvedValue(mockResponse as any);
renderHook(() => useAdminUsers(1, 50, null, null, false), { wrapper });
await waitFor(() => {
expect(mockAdminListUsers).toHaveBeenCalledWith({
query: { page: 1, limit: 50, is_superuser: false },
throwOnError: false,
});
});
});
it('passes all filter parameters to API', async () => {
mockUseAuth.mockReturnValue({
user: { is_superuser: true } as any,
isAuthenticated: true,
isLoading: false,
login: jest.fn(),
logout: jest.fn(),
});
mockAdminListUsers.mockResolvedValue(mockResponse as any);
renderHook(() => useAdminUsers(2, 20, 'admin', true, false), { wrapper });
await waitFor(() => {
expect(mockAdminListUsers).toHaveBeenCalledWith({
query: {
page: 2,
limit: 20,
search: 'admin',
is_active: true,
is_superuser: false
},
throwOnError: false,
});
});
});
it('excludes filter parameters when they are null', async () => {
mockUseAuth.mockReturnValue({
user: { is_superuser: true } as any,
isAuthenticated: true,
isLoading: false,
login: jest.fn(),
logout: jest.fn(),
});
mockAdminListUsers.mockResolvedValue(mockResponse as any);
renderHook(() => useAdminUsers(1, 50, null, null, null), { wrapper });
await waitFor(() => {
expect(mockAdminListUsers).toHaveBeenCalledWith({
query: { page: 1, limit: 50 },
throwOnError: false,
});
});
});
});
describe('useAdminOrganizations', () => {