Skip to content

Commit

Permalink
[enzyme-test-suites] Add useImperativeHandle tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chenesan committed Jun 13, 2019
1 parent 71543a6 commit f38b1b6
Show file tree
Hide file tree
Showing 3 changed files with 51 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,49 @@
import React from 'react';
import { expect } from 'chai';
import sinon from 'sinon-sandbox';

import {
describeIf,
itIf,
} from '../../_helpers';

import {
useImperativeHandle,
useRef,
} from '../../_helpers/react-compat';

export default function describeUseImperativeHandle({
hasHooks,
Wrap,
isMount,
}) {
describeIf(hasHooks, 'hooks: useImperativeHandle', () => {
function Computer({ compute }, ref) {
const computerRef = useRef({ compute });
useImperativeHandle(ref, () => ({
compute: () => {
computerRef.current.compute();
},
}));
return <div />;
}
const FancyComputer = React.forwardRef(Computer);
class ParentComputer extends React.Component {
componentDidMount() {
if (this.ref) {
this.ref.compute();
}
}

render() {
return <FancyComputer ref={(ref) => { this.ref = ref; }} {...this.props} />;
}
}
itIf(isMount, 'able to call method with imperative handle', () => {
const compute = sinon.spy();
Wrap(<ParentComputer compute={compute} />);

expect(compute.called).to.equal(true);
});
});
}

0 comments on commit f38b1b6

Please sign in to comment.