|
| 1 | +import { fetchSupportFrontendData } from './supportFrontend'; |
| 2 | + |
| 3 | +describe('fetchSupportFrontendData', () => { |
| 4 | + const originalFetch = global.fetch; |
| 5 | + |
| 6 | + beforeAll(() => { |
| 7 | + global.fetch = jest.fn(); |
| 8 | + }); |
| 9 | + |
| 10 | + afterAll(() => { |
| 11 | + global.fetch = originalFetch; |
| 12 | + }); |
| 13 | + it('should throw an error if no endpoint is provided', async () => { |
| 14 | + await expect(fetchSupportFrontendData('')).rejects.toThrow('No endpoint value supplied'); |
| 15 | + }); |
| 16 | + it('should throw an error if fetch response is not ok', async () => { |
| 17 | + (global.fetch as jest.Mock).mockResolvedValueOnce({ |
| 18 | + ok: false, |
| 19 | + status: 404, |
| 20 | + }); |
| 21 | + await expect(fetchSupportFrontendData('test-endpoint')).rejects.toThrow( |
| 22 | + 'HTTP error! Status: 404', |
| 23 | + ); |
| 24 | + }); |
| 25 | + |
| 26 | + it('should return response text if fetch is successful', async () => { |
| 27 | + const mockText = 'response data'; |
| 28 | + (global.fetch as jest.Mock).mockResolvedValueOnce({ |
| 29 | + ok: true, |
| 30 | + text: jest.fn().mockResolvedValueOnce(mockText), |
| 31 | + }); |
| 32 | + const data = await fetchSupportFrontendData('test-endpoint'); |
| 33 | + expect(data).toBe(mockText); |
| 34 | + }); |
| 35 | + |
| 36 | + it('should throw an error if fetch fails', async () => { |
| 37 | + const mockError = new Error('Network error'); |
| 38 | + (global.fetch as jest.Mock).mockRejectedValueOnce(mockError); |
| 39 | + await expect(fetchSupportFrontendData('test-endpoint')).rejects.toThrow( |
| 40 | + 'Data fetch error: Error: Network error', |
| 41 | + ); |
| 42 | + }); |
| 43 | +}); |
0 commit comments