Skip to content

Commit

Permalink
[Tests] Add useImperativeHandle
Browse files Browse the repository at this point in the history
  • Loading branch information
chenesan authored and ljharb committed Jun 13, 2019
1 parent 0456949 commit 65f53d2
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/enzyme-test-suite/test/ReactWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -1050,6 +1050,7 @@ describeWithDOM('mount', () => {
'useContext',
'useDebugValue',
'useEffect',
'useImperativeHandle',
'useLayoutEffect',
'useMemo',
'useReducer',
Expand Down
1 change: 1 addition & 0 deletions packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -1231,6 +1231,7 @@ describe('shallow', () => {
'useContext',
'useDebugValue',
'useEffect',
'useImperativeHandle',
'useLayoutEffect',
'useMemo',
'useReducer',
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <div />;
}

const FancyComputer = forwardRef && forwardRef(Computer);

class ParentComputer extends React.Component {
componentDidMount() {
if (this.ref) {
this.ref.compute();
}
}

render() {
return <FancyComputer ref={(ref) => { this.ref = ref; }} {...this.props} />;
}
}

it('able to call method with imperative handle', () => {
const compute = sinon.spy();
Wrap(<ParentComputer compute={compute} />);

expect(compute).to.have.property('callCount', isShallow ? 0 : 1);
});
});
}

0 comments on commit 65f53d2

Please sign in to comment.