I am trying to test a connected react component, however once wrapping it, I cannot get the instance of it using instance(), it returns null. for non-connected components it does return the instance, what is the difference and how can i get an instance of a connected component?
it('connected component', () => {
const wrapper = mount(
<Provider store={store}>
<BrowserRouter>
<ConnectedComponent />
</BrowserRouter>
</Provider>
)
const myComp = wrapper.find(ConnectedComponent)
expect(myComp).toHaveLength(1) // passes
console.log(myComp.instance()) //null
})
it('non-connected component', () => {
const wrapper = mount(
<Provider store={store}>
<BrowserRouter>
<NonConnectedComponent />
</BrowserRouter>
</Provider>
)
const myComp = wrapper.find(NonConnectedComponent)
expect(myComp).toHaveLength(1) // passes
console.log(myComp.instance()) // prints the instancce
})
const myComp = wrapper.find('NonConnectedComponent')
(note the quotes around the name). Not the most beautiful solution, but good for workaround if works. – Jahncke