feat(tests): enhance test coverage and error handling across various components

- Added a test case in CrowdSecConfig to show improved error message when preset is not cached.
- Introduced a new test suite for the Dashboard component, verifying counts and health status.
- Updated SMTPSettings tests to utilize a shared render function and added tests for backend validation errors.
- Modified Security.audit tests to improve input handling and removed redundant export failure test.
- Refactored Security tests to remove export functionality and ensure correct rendering of components.
- Enhanced UsersPage tests with new scenarios for updating user permissions and manual invite link flow.
- Created a new utility for rendering components with a QueryClient and MemoryRouter for better test isolation.
- Updated go-test-coverage script to improve error handling and coverage reporting.
This commit is contained in:
GitHub Actions
2025-12-11 00:26:07 +00:00
parent ca4cfc4e65
commit e299aa6b52
81 changed files with 8960 additions and 450 deletions

View File

@@ -0,0 +1,52 @@
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
import {
getSecurityNotificationSettings,
updateSecurityNotificationSettings,
SecurityNotificationSettings,
} from '../api/notifications';
import { toast } from '../utils/toast';
export function useSecurityNotificationSettings() {
return useQuery({
queryKey: ['security-notification-settings'],
queryFn: getSecurityNotificationSettings,
});
}
export function useUpdateSecurityNotificationSettings() {
const queryClient = useQueryClient();
return useMutation({
mutationFn: (settings: Partial<SecurityNotificationSettings>) =>
updateSecurityNotificationSettings(settings),
onMutate: async (newSettings) => {
// Cancel any outgoing refetches
await queryClient.cancelQueries({ queryKey: ['security-notification-settings'] });
// Snapshot the previous value
const previousSettings = queryClient.getQueryData(['security-notification-settings']);
// Optimistically update to the new value
queryClient.setQueryData(['security-notification-settings'], (old: unknown) => {
if (old && typeof old === 'object') {
return { ...old, ...newSettings };
}
return old;
});
return { previousSettings };
},
onError: (err, _newSettings, context) => {
// Rollback on error
if (context?.previousSettings) {
queryClient.setQueryData(['security-notification-settings'], context.previousSettings);
}
const message = err instanceof Error ? err.message : 'Failed to update notification settings';
toast.error(message);
},
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ['security-notification-settings'] });
toast.success('Notification settings updated');
},
});
}