diff --git a/packages/enzyme-test-suite/test/ReactWrapper-spec.jsx b/packages/enzyme-test-suite/test/ReactWrapper-spec.jsx index e614820cf..007a8de7b 100644 --- a/packages/enzyme-test-suite/test/ReactWrapper-spec.jsx +++ b/packages/enzyme-test-suite/test/ReactWrapper-spec.jsx @@ -1050,6 +1050,7 @@ describeWithDOM('mount', () => { 'useContext', 'useDebugValue', 'useEffect', + 'useImperativeHandle', 'useLayoutEffect', 'useMemo', 'useReducer', diff --git a/packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx b/packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx index 90c683a33..25e2fb2be 100644 --- a/packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx +++ b/packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx @@ -1231,6 +1231,7 @@ describe('shallow', () => { 'useContext', 'useDebugValue', 'useEffect', + 'useImperativeHandle', 'useLayoutEffect', 'useMemo', 'useReducer', diff --git a/packages/enzyme-test-suite/test/shared/hooks/useImperativeHandle.jsx b/packages/enzyme-test-suite/test/shared/hooks/useImperativeHandle.jsx new file mode 100644 index 000000000..1382fb2bc --- /dev/null +++ b/packages/enzyme-test-suite/test/shared/hooks/useImperativeHandle.jsx @@ -0,0 +1,52 @@ +import React from 'react'; +import { expect } from 'chai'; +import sinon from 'sinon-sandbox'; + +import { + describeIf, +} from '../../_helpers'; + +import { + useImperativeHandle, + useRef, + forwardRef, +} from '../../_helpers/react-compat'; + +export default function describeUseImperativeHandle({ + hasHooks, + Wrap, + isShallow, +}) { + describeIf(hasHooks, 'hooks: useImperativeHandle', () => { + function Computer({ compute }, ref) { + const computerRef = useRef({ compute }); + useImperativeHandle(ref, () => ({ + compute: () => { + computerRef.current.compute(); + }, + })); + return
; + } + + const FancyComputer = forwardRef && forwardRef(Computer); + + class ParentComputer extends React.Component { + componentDidMount() { + if (this.ref) { + this.ref.compute(); + } + } + + render() { + return { this.ref = ref; }} {...this.props} />; + } + } + + it('able to call method with imperative handle', () => { + const compute = sinon.spy(); + Wrap(); + + expect(compute).to.have.property('callCount', isShallow ? 0 : 1); + }); + }); +}