Created
April 6, 2019 08:07
-
-
Save natterstefan/e54f6f3918d0c33bd18d2eea1dc3e343 to your computer and use it in GitHub Desktop.
JEST & ENZYME | test component with React.createRef()
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 React from 'react' | |
import { mount } from 'enzyme' | |
describe('Foo', () => { | |
it('enzyme should mount object refs', () => { | |
// inspired by: | |
// - https://stackoverflow.com/a/55292957/1238150 | |
// - https://github.com/styled-components/styled-components/blob/e6f1b85726512cde163dd5fcdd7fb0f2439d4a09/packages/styled-components/src/test/basic.test.js | |
// - https://github.com/styled-components/styled-components/blob/e6f1b85726512cde163dd5fcdd7fb0f2439d4a09/packages/styled-components/src/hoc/withTheme.js | |
class Foo extends React.Component { | |
constructor(props) { | |
super(props) | |
this.setRef = React.createRef() | |
} | |
render() { | |
return <div ref={this.setRef} className="foo" /> | |
} | |
} | |
const wrapper = mount(<Foo />) | |
const element = wrapper.find('.foo').instance() | |
expect(wrapper.instance().setRef).toHaveProperty('current', element) | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment