From 1f98e03c02b4812f0f35407df736bad6187b4197 Mon Sep 17 00:00:00 2001 From: GSRN Date: Mon, 15 Sep 2025 22:37:57 +0200 Subject: [PATCH] fix: Enhance error handling tests and add service status checks ### Summary of Changes - Introduced new tests for `handleRequestError` to cover various error scenarios including connection timeouts and service errors. - Added tests for `determineServiceStatus` to verify service availability states. - Updated `formatServiceData` and `formatEventData` tests to ensure correct formatting and handling of invalid data. ### Expected Results - Improved coverage and reliability of error handling utilities tests, ensuring accurate error responses and service status checks. - Enhanced maintainability of test code by applying clean code principles and better organization. --- frontend/src/setupTests.js | 5 ++ frontend/src/utils/errorHandling.test.js | 85 +++++++++++++++++------- 2 files changed, 67 insertions(+), 23 deletions(-) create mode 100644 frontend/src/setupTests.js diff --git a/frontend/src/setupTests.js b/frontend/src/setupTests.js new file mode 100644 index 0000000..8f2609b --- /dev/null +++ b/frontend/src/setupTests.js @@ -0,0 +1,5 @@ +// jest-dom adds custom jest matchers for asserting on DOM nodes. +// allows you to do things like: +// expect(element).toHaveTextContent(/react/i) +// learn more: https://github.com/testing-library/jest-dom +import '@testing-library/jest-dom'; diff --git a/frontend/src/utils/errorHandling.test.js b/frontend/src/utils/errorHandling.test.js index aacea89..63f8748 100644 --- a/frontend/src/utils/errorHandling.test.js +++ b/frontend/src/utils/errorHandling.test.js @@ -1,29 +1,51 @@ -import { formatError, formatServiceData, formatEventData } from './errorHandling' +import { handleRequestError, determineServiceStatus, formatServiceData, formatEventData } from './errorHandling' describe('Error Handling Utils', () => { - describe('formatError', () => { - it('should format error objects correctly', () => { - const error = new Error('Test error message') - const formatted = formatError(error) + describe('handleRequestError', () => { + it('should handle connection timeout errors', () => { + const error = { code: 'ECONNABORTED' } + const result = handleRequestError(error) - expect(formatted).toHaveProperty('message', 'Test error message') - expect(formatted).toHaveProperty('type', 'Error') + expect(result).toHaveProperty('error') + expect(result.error).toContain('Request timeout') }) - it('should handle string errors', () => { - const error = 'Simple string error' - const formatted = formatError(error) + it('should handle response errors', () => { + const error = { response: { status: 500 } } + const result = handleRequestError(error) - expect(formatted).toHaveProperty('message', 'Simple string error') - expect(formatted).toHaveProperty('type', 'string') + expect(result).toHaveProperty('error') + expect(result.error).toContain('Service error') }) - it('should handle unknown error types', () => { + it('should handle request errors', () => { + const error = { request: {} } + const result = handleRequestError(error) + + expect(result).toHaveProperty('error') + expect(result.error).toContain('Service unavailable') + }) + + it('should handle unknown errors', () => { const error = { someProperty: 'value' } - const formatted = formatError(error) + const result = handleRequestError(error) - expect(formatted).toHaveProperty('message', 'Unknown error occurred') - expect(formatted).toHaveProperty('type', 'unknown') + expect(result).toHaveProperty('error') + expect(result.error).toContain('Unknown error') + }) + }) + + describe('determineServiceStatus', () => { + it('should return offline when no services available', () => { + expect(determineServiceStatus(0, 3)).toBe('offline') + }) + + it('should return online when all services available', () => { + expect(determineServiceStatus(3, 3)).toBe('online') + }) + + it('should return partial when some services available', () => { + expect(determineServiceStatus(2, 3)).toBe('partial') }) }) @@ -31,21 +53,31 @@ describe('Error Handling Utils', () => { it('should format service data correctly', () => { const rawData = { 'api-gateway': { + name: 'API Gateway', status: 'healthy', - lastCheck: '2024-01-01T00:00:00.000Z' + responseTime: '1d 2h' } } const formatted = formatServiceData(rawData) - expect(formatted).toHaveProperty('api-gateway') - expect(formatted['api-gateway']).toHaveProperty('status', 'healthy') - expect(formatted['api-gateway']).toHaveProperty('lastCheck') + expect(Array.isArray(formatted)).toBe(true) + expect(formatted).toHaveLength(1) + expect(formatted[0]).toHaveProperty('name', 'API Gateway') + expect(formatted[0]).toHaveProperty('status', 'online') + expect(formatted[0]).toHaveProperty('uptime', '1d 2h') }) it('should handle empty data', () => { const formatted = formatServiceData({}) - expect(formatted).toEqual({}) + expect(Array.isArray(formatted)).toBe(true) + expect(formatted).toHaveLength(0) + }) + + it('should handle invalid data', () => { + const formatted = formatServiceData(null) + expect(Array.isArray(formatted)).toBe(true) + expect(formatted).toHaveLength(0) }) }) @@ -53,7 +85,6 @@ describe('Error Handling Utils', () => { it('should format event data correctly', () => { const rawEvents = [ { - id: '1', timestamp: '2024-01-01T00:00:00.000Z', service: 'api-gateway', event_type: 'health_check' @@ -63,7 +94,9 @@ describe('Error Handling Utils', () => { const formatted = formatEventData(rawEvents) expect(Array.isArray(formatted)).toBe(true) - expect(formatted[0]).toHaveProperty('id', '1') + expect(formatted).toHaveLength(1) + expect(formatted[0]).toHaveProperty('time') + expect(formatted[0]).toHaveProperty('event', 'health_check from api-gateway') expect(formatted[0]).toHaveProperty('service', 'api-gateway') }) @@ -72,5 +105,11 @@ describe('Error Handling Utils', () => { expect(Array.isArray(formatted)).toBe(true) expect(formatted).toHaveLength(0) }) + + it('should handle invalid data', () => { + const formatted = formatEventData(null) + expect(Array.isArray(formatted)).toBe(true) + expect(formatted).toHaveLength(0) + }) }) })