-
-
Save rmbrntt/69d9ebf3b2c9413f65c23d2ce4489a94 to your computer and use it in GitHub Desktop.
Mock event.preventDefault() with Jest
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
static async handleDelete(event) { | |
let success = true; | |
await Axios.delete('/todos') | |
.then(() => {}) | |
.catch(error => { | |
success = false; | |
console.error(error); | |
}); | |
event.preventDefault(); | |
return success; | |
} |
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
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