react-onclickoutside: Enzyme TypeError : Cannot read property 'getInstance' of undefined

I’m using Enzyme for testing React, which returns this error (i passed from React 15 to 16.3). Do you know a way to avoid it ?

image

TypeError: Cannot read property 'getInstance' of undefined
      at onClickOutside.getInstance (node_modules/react-onclickoutside/dist/react-onclickoutside.cjs.js:272:18)
      at onClickOutside.componentDidMount (node_modules/react-onclickoutside/dist/react-onclickoutside.cjs.js:287:27)
      at node_modules/enzyme/build/ShallowWrapper.js:126:20
      at Object.batchedUpdates (node_modules/enzyme-adapter-react-16/build/ReactSixteenAdapter.js:342:22)
      at new ShallowWrapper (node_modules/enzyme/build/ShallowWrapper.js:125:24)
      at shallow (node_modules/enzyme/build/shallow.js:19:10)
      at Context.<anonymous> (src/components/select/index.test.js:14:9)

To reproduce

Test file

import { shallow } from 'enzyme'
import { Select } from '.'

describe('<Select />', () => {

    it('works without options', () => {
        shallow(<Select />)
    })

}

Component

import onClickOutside from 'react-onclickoutside'

class Select extends React.Component {

    render = () => <div></div>
}

const COSelect = onClickOutside(Select)
export { COSelect as Select }

Thanks 👐

About this issue

  • Original URL
  • State: open
  • Created 6 years ago
  • Reactions: 8
  • Comments: 19 (6 by maintainers)

Most upvoted comments

I’m running into the same getInstance error, but not using Enzyme.

Nice!

Note that the reason this hasn’t been tackled here yet is that even with 1 million installs a week (no doubt including some very wellfunded projects), no one’s actually sponsoring this library, and so I have to balance what I work on based on how much people actually give back (either as boring-but-welcome cash, or by becoming a code contributor, comaintainer, etc).

So: I may eventually get to this project again, but until then: the best way to make sure I make time is by having your employer sponsor this library, and spreading the word that this library needs sponsoring. If it saves your company several hours of dev work, then giving back a fraction of that to the projects that allow you to steam ahead is a responsible thing to do.

Happening with me in tests using jest and react testing library.

For now, i have mocked the module in my tests, as my use case is not to test the outside click functionality. jest.mock('react-onclickoutside', () => (component) => component);

I’m still going to have to echo my may 24 and july 15 requests: without a PoC for the plain vanilla use, demonstrating this behaviour, as well as demonstrating it is purely related to react-onclickout rather than any additional framework or codebase, there’s not much I can do here. I’ll be happy to debug code that shows off this problem, but I don’t have the time to try to find some way to product this error first, before I can even get to debugging it.

If anyone can add a self-contained test case either as a block of code in a comment, or as a PR that adds a test case to the code itself, I will be happy to do some digging. Until then, I’m going to keep requesting that test case =)