Skip to content

Instantly share code, notes, and snippets.

@devotdev
Created December 5, 2024 14:51
Show Gist options
  • Save devotdev/e362e20b57c040f76eac811b32634e23 to your computer and use it in GitHub Desktop.
Save devotdev/e362e20b57c040f76eac811b32634e23 to your computer and use it in GitHub Desktop.
Test cases for PUT request
// 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