File size: 10,297 Bytes
f0743f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
import React from 'react';
import { render, screen, fireEvent } from '@testing-library/react';
import { ErrorDisplay } from '../ErrorDisplay';

// Mock matchMedia
Object.defineProperty(window, 'matchMedia', {
  writable: true,
  value: jest.fn().mockImplementation((query) => ({
    matches: false,
    media: query,
    onchange: null,
    addListener: jest.fn(),
    removeListener: jest.fn(),
    addEventListener: jest.fn(),
    removeEventListener: jest.fn(),
    dispatchEvent: jest.fn(),
  })),
});

// Mock the localize hook
const mockLocalize = jest.fn((key: string, options?: any) => {
  const translations: Record<string, string> = {
    com_agents_error_title: 'Something went wrong',
    com_agents_error_generic: 'We encountered an issue while loading the content.',
    com_agents_error_suggestion_generic: 'Please try refreshing the page or try again later.',
    com_agents_error_network_title: 'Connection Problem',
    com_agents_error_network_message: 'Unable to connect to the server.',
    com_agents_error_network_suggestion: 'Check your internet connection and try again.',
    com_agents_error_not_found_title: 'Not Found',
    com_agents_error_not_found_message: 'The requested content could not be found.',
    com_agents_error_not_found_suggestion:
      'Try browsing other options or go back to the marketplace.',
    com_agents_error_invalid_request: 'Invalid Request',
    com_agents_error_bad_request_message: 'The request could not be processed.',
    com_agents_error_bad_request_suggestion: 'Please check your input and try again.',
    com_agents_error_server_title: 'Server Error',
    com_agents_error_server_message: 'The server is temporarily unavailable.',
    com_agents_error_server_suggestion: 'Please try again in a few moments.',
    com_agents_error_search_title: 'Search Error',
    com_agents_error_category_title: 'Category Error',
    com_agents_error_timeout_title: 'Connection Timeout',
    com_agents_error_timeout_message: 'The request took too long to complete.',
    com_agents_error_timeout_suggestion: 'Please check your internet connection and try again.',
    com_agents_search_no_results: `No agents found for "${options?.query}"`,
    com_agents_category_empty: `No agents found in the ${options?.category} category`,
    com_agents_error_retry: 'Try Again',
  };

  return translations[key] || key;
});

jest.mock('~/hooks/useLocalize', () => () => mockLocalize);

describe('ErrorDisplay', () => {
  beforeEach(() => {
    mockLocalize.mockClear();
  });

  describe('Backend error responses', () => {
    it('displays user-friendly message from backend response', () => {
      const error = {
        response: {
          data: {
            userMessage: 'Unable to load agents. Please try refreshing the page.',
            suggestion: 'Try refreshing the page or check your network connection',
          },
        },
      };

      render(<ErrorDisplay error={error} />);

      expect(screen.getByText('Something went wrong')).toBeInTheDocument();
      expect(
        screen.getByText('Unable to load agents. Please try refreshing the page.'),
      ).toBeInTheDocument();
      expect(
        screen.getByText('💡 Try refreshing the page or check your network connection'),
      ).toBeInTheDocument();
    });

    it('handles search context with backend response', () => {
      const error = {
        response: {
          data: {
            userMessage: 'Search is temporarily unavailable. Please try again.',
            suggestion: 'Try a different search term or check your network connection',
          },
        },
      };

      render(<ErrorDisplay error={error} context={{ searchQuery: 'test query' }} />);

      expect(screen.getByText('Search Error')).toBeInTheDocument();
      expect(
        screen.getByText('Search is temporarily unavailable. Please try again.'),
      ).toBeInTheDocument();
    });
  });

  describe('Network errors', () => {
    it('displays network error message', () => {
      const error = {
        code: 'NETWORK_ERROR',
        message: 'Network Error',
      };

      render(<ErrorDisplay error={error} />);

      expect(screen.getByText('Connection Problem')).toBeInTheDocument();
      expect(screen.getByText('Unable to connect to the server.')).toBeInTheDocument();
      expect(
        screen.getByText('💡 Check your internet connection and try again.'),
      ).toBeInTheDocument();
    });

    it('handles timeout errors', () => {
      const error = {
        code: 'ECONNABORTED',
        message: 'timeout of 5000ms exceeded',
      };

      render(<ErrorDisplay error={error} />);

      expect(mockLocalize).toHaveBeenCalledWith('com_agents_error_timeout_title');
      expect(mockLocalize).toHaveBeenCalledWith('com_agents_error_timeout_message');
      expect(mockLocalize).toHaveBeenCalledWith('com_agents_error_timeout_suggestion');
    });
  });

  describe('HTTP status codes', () => {
    it('handles 404 errors with search context', () => {
      const error = {
        response: {
          status: 404,
          data: {},
        },
      };

      render(<ErrorDisplay error={error} context={{ searchQuery: 'nonexistent agent' }} />);

      expect(screen.getByText('Not Found')).toBeInTheDocument();
      expect(screen.getByText('No agents found for "nonexistent agent"')).toBeInTheDocument();
    });

    it('handles 404 errors with category context', () => {
      const error = {
        response: {
          status: 404,
          data: {},
        },
      };

      render(<ErrorDisplay error={error} context={{ category: 'productivity' }} />);

      expect(screen.getByText('Not Found')).toBeInTheDocument();
      expect(screen.getByText('No agents found in the productivity category')).toBeInTheDocument();
    });

    it('handles 400 bad request errors', () => {
      const error = {
        response: {
          status: 400,
          data: {
            error: 'Search query is required',
            userMessage: 'Please enter a search term to find agents',
            suggestion: 'Enter a search term to find agents by name or description',
          },
        },
      };

      render(<ErrorDisplay error={error} />);

      expect(screen.getByText('Invalid Request')).toBeInTheDocument();
      expect(screen.getByText('Please enter a search term to find agents')).toBeInTheDocument();
      expect(
        screen.getByText('💡 Enter a search term to find agents by name or description'),
      ).toBeInTheDocument();
    });

    it('handles 500 server errors', () => {
      const error = {
        response: {
          status: 500,
          data: {},
        },
      };

      render(<ErrorDisplay error={error} />);

      expect(screen.getByText('Server Error')).toBeInTheDocument();
      expect(screen.getByText('The server is temporarily unavailable.')).toBeInTheDocument();
      expect(screen.getByText('💡 Please try again in a few moments.')).toBeInTheDocument();
    });
  });

  describe('Retry functionality', () => {
    it('displays retry button when onRetry is provided', () => {
      const mockRetry = jest.fn();
      const error = {
        response: {
          data: {
            userMessage: 'Unable to load agents. Please try refreshing the page.',
          },
        },
      };

      render(<ErrorDisplay error={error} onRetry={mockRetry} />);

      const retryButton = screen.getByText('Try Again');
      expect(retryButton).toBeInTheDocument();

      fireEvent.click(retryButton);
      expect(mockRetry).toHaveBeenCalledTimes(1);
    });

    it('does not display retry button when onRetry is not provided', () => {
      const error = {
        response: {
          data: {
            userMessage: 'Unable to load agents. Please try refreshing the page.',
          },
        },
      };

      render(<ErrorDisplay error={error} />);

      expect(screen.queryByText('Try Again')).not.toBeInTheDocument();
    });
  });

  describe('Context-aware titles', () => {
    it('shows search error title for search context', () => {
      const error = { message: 'Some error' };

      render(<ErrorDisplay error={error} context={{ searchQuery: 'test' }} />);

      expect(mockLocalize).toHaveBeenCalledWith('com_agents_error_search_title');
    });

    it('shows category error title for category context', () => {
      const error = { message: 'Some error' };

      render(<ErrorDisplay error={error} context={{ category: 'productivity' }} />);

      expect(mockLocalize).toHaveBeenCalledWith('com_agents_error_category_title');
    });

    it('shows generic error title when no context', () => {
      const error = { message: 'Some error' };

      render(<ErrorDisplay error={error} />);

      expect(mockLocalize).toHaveBeenCalledWith('com_agents_error_title');
    });
  });

  describe('Fallback error handling', () => {
    it('handles unknown errors gracefully', () => {
      const error = {
        message: 'Unknown error occurred',
      };

      render(<ErrorDisplay error={error} />);

      expect(screen.getByText('Something went wrong')).toBeInTheDocument();
      expect(
        screen.getByText('We encountered an issue while loading the content.'),
      ).toBeInTheDocument();
      expect(
        screen.getByText('💡 Please try refreshing the page or try again later.'),
      ).toBeInTheDocument();
    });

    it('handles null/undefined errors', () => {
      render(<ErrorDisplay error={null} />);

      expect(screen.getByText('Something went wrong')).toBeInTheDocument();
      expect(
        screen.getByText('We encountered an issue while loading the content.'),
      ).toBeInTheDocument();
    });
  });

  describe('Accessibility', () => {
    it('renders error icon with proper accessibility', () => {
      const error = { message: 'Test error' };

      render(<ErrorDisplay error={error} />);

      const errorIcon = screen.getByRole('img', { hidden: true });
      expect(errorIcon).toBeInTheDocument();
    });

    it('has proper heading structure', () => {
      const error = { message: 'Test error' };

      render(<ErrorDisplay error={error} />);

      const heading = screen.getByRole('heading', { level: 3 });
      expect(heading).toHaveTextContent('Something went wrong');
    });
  });
});