Skip to content

Commit dee77bd

Browse files
committed
[Tests]: add failed useState test case in shallow for hooks
1 parent 9faac9d commit dee77bd

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import {
3131
forwardRef,
3232
memo,
3333
PureComponent,
34+
useState,
3435
} from './_helpers/react-compat';
3536
import {
3637
describeIf,
@@ -1534,6 +1535,35 @@ describe('shallow', () => {
15341535
});
15351536
});
15361537

1538+
describeIf(is('>= 16.8'), 'hooks', () => {
1539+
it('handles useState', () => {
1540+
const ComponentUsingStateHook = () => {
1541+
const [count] = useState(0);
1542+
return <div>{count}</div>;
1543+
};
1544+
1545+
const wrapper = shallow(<ComponentUsingStateHook />);
1546+
1547+
expect(wrapper.find('div').length).to.equal(1);
1548+
expect(wrapper.find('div').text()).to.equal('0');
1549+
});
1550+
1551+
it('handles setState returned from useState', () => {
1552+
const ComponentUsingStateHook = () => {
1553+
const [count, setCount] = useState(0);
1554+
return <div onClick={() => setCount(count + 1)}>{count}</div>;
1555+
};
1556+
1557+
const wrapper = shallow(<ComponentUsingStateHook />);
1558+
const div = wrapper.find('div');
1559+
const setCount = div.prop('onClick');
1560+
setCount();
1561+
wrapper.update();
1562+
1563+
expect(wrapper.find('div').text()).to.equal('1');
1564+
});
1565+
});
1566+
15371567
describeWithDOM('find DOM elements by constructor', () => {
15381568
const { elements, all } = getData();
15391569

0 commit comments

Comments
 (0)