| describe('GET Request Tests', () => { | |
| beforeEach(() => { | |
| jest.clearAllMocks(); | |
| }); | |
| it('fetches successfully data from an API', async () => { | |
| const data = { id: 1, name: 'John Doe' }; | |
| axios.get.mockResolvedValue({ data }); | |
| const result = await fetchData('https://api.example.com/user/1'); | |
| expect(result).toEqual(data); | |
| expect(axios.get).toHaveBeenCalledWith('https://api.example.com/user/1'); | |
| }); | |
| it('fetches data error from an API', async () => { | |
| const errorMessage = 'Network Error'; | |
| axios.get.mockRejectedValue(new Error(errorMessage)); | |
| await expect(fetchData('https://api.example.com/user/1')).rejects.toThrow(errorMessage); | |
| }); | |
| }); |