Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Mock event.preventDefault() with Jest
static async handleDelete(event) {
let success = true;
await Axios.delete('/todos')
.then(() => {})
.catch(error => {
success = false;
console.error(error);
});
event.preventDefault();
return success;
}
import Axios from 'axios';
import MockAdapter from 'axios-mock-adapter';
import CreateTodo from './../../../../src/client/components/containers/createTodo.jsx';
let mockAxios;
describe('client/components/containers/createTodo -> <CreateTodo />', () => {
const event = { preventDefault: () => {} };
beforeEach(() => {
mockAxios = new MockAdapter(Axios);
jest.spyOn(event, 'preventDefault');
});
afterEach(() => {
mockAxios.reset();
});
describe('CreateTodo.handleDelete(event)', () => {
test('should call Axios.delete and event.preventDefault()', async () => {
mockAxios.onDelete('/todos').reply(200);
const success = await CreateTodo.handleDelete(event);
expect(event.preventDefault).toBeCalled();
expect(success).toBeTruthy();
});
test('should call Axios.delete and throw error', async () => {
mockAxios.onDelete('/todos').networkError();
const success = await CreateTodo.handleDelete(event);
expect(event.preventDefault).toBeCalled();
expect(success).toBeFalsy();
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.