-
-
Save devotdev/e362e20b57c040f76eac811b32634e23 to your computer and use it in GitHub Desktop.
Test cases for PUT request
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment