Created
December 22, 2017 23:58
-
-
Save blairg/b6575a23ce96603a120d841f70463f76 to your computer and use it in GitHub Desktop.
Mock event.preventDefault() with Jest
This file contains hidden or 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 hidden or 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