|
// api.test.js |
|
const axios = require('axios'); |
|
const updateUser = require('../api'); |
|
|
|
// Mock the axios module |
|
jest.mock('axios'); |
|
|
|
describe('PUT Request Tests', () => { |
|
beforeEach(() => { |
|
jest.clearAllMocks(); |
|
}); |
|
|
|
it('updates a user successfully', async () => { |
|
const userId = 1; |
|
const updatedData = { name: 'Jane Doe' }; |
|
const response = { id: userId, ...updatedData }; |
|
|
|
axios.put.mockResolvedValue({ data: response }); |
|
|
|
const result = await updateUser(userId, updatedData); |
|
expect(result).toEqual(response); |
|
expect(axios.put).toHaveBeenCalledWith(`https://api.example.com/users/${userId}`, updatedData); |
|
}); |
|
|
|
it('handles API error during user update', async () => { |
|
const userId = 1; |
|
const updatedData = { name: 'Jane Doe' }; |
|
const errorMessage = 'Update Failed'; |
|
|
|
axios.put.mockRejectedValue(new Error(errorMessage)); |
|
|
|
await expect(updateUser(userId, updatedData)).rejects.toThrow(errorMessage); |
|
expect(axios.put).toHaveBeenCalledWith(`https://api.example.com/users/${userId}`, updatedData); |
|
}); |
|
}); |