ant-design/components/grid/__tests__/cached-row-context.test.tsx

39 lines
863 B
TypeScript
Raw Normal View History

2022-11-05 21:29:56 +08:00
import React, { memo, useContext } from 'react';
import Row from '../row';
import RowContext from '../RowContext';
2022-11-05 21:29:56 +08:00
import { fireEvent, pureRender } from '../../../tests/utils';
2022-11-05 21:29:56 +08:00
let innerCount = 0;
let outerCount = 0;
2022-11-05 21:29:56 +08:00
const handleClick = () => {
outerCount++;
};
2022-11-05 21:29:56 +08:00
const CacheInner: React.FC = memo(() => {
innerCount++;
useContext(RowContext);
return null;
});
2022-11-05 21:29:56 +08:00
const CacheOuter: React.FC = memo(() => (
<>
<button type="button" onClick={handleClick} id="parent_btn">
Click
</button>
<Row>
<CacheInner />
</Row>
</>
));
2022-11-05 21:29:56 +08:00
it('Cached RowContext is working', () => {
const { container, unmount } = pureRender(<CacheOuter />);
expect(outerCount).toBe(0);
expect(innerCount).toBe(1);
fireEvent.click(container.querySelector('#parent_btn')!);
2022-11-05 21:29:56 +08:00
expect(outerCount).toBe(1);
expect(innerCount).toBe(1);
unmount();
});