Skip to content

Instantly share code, notes, and snippets.

@msrose
Created June 6, 2020 02:17
Show Gist options
  • Save msrose/116231dc98da488f033526fcc5b14618 to your computer and use it in GitHub Desktop.
Save msrose/116231dc98da488f033526fcc5b14618 to your computer and use it in GitHub Desktop.
Mock function as prop
module.exports = {
presets: ['@babel/preset-react', '@babel/preset-env']
}
import React from 'react'
import Enzyme, { mount } from 'enzyme'
import Adapter from 'enzyme-adapter-react-16'
Enzyme.configure({ adapter: new Adapter() })
const Foo = ({ onClick }) => {
const handleClick = () => {
onClick()
}
return <button onClick={handleClick} />
}
test('Foo calls onClick when button is clicked', () => {
const clickMock = jest.fn()
const wrapper = mount(<Foo onClick={clickMock} />)
wrapper.find('button').simulate('click')
expect(clickMock).toHaveBeenCalled()
})
{
"dependencies": {
"@babel/preset-env": "^7.10.2",
"@babel/preset-react": "^7.10.1",
"babel-jest": "^26.0.1",
"enzyme": "^3.11.0",
"enzyme-adapter-react-16": "^1.15.2",
"jest": "^26.0.1",
"react": "^16.13.1",
"react-dom": "^16.13.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment