2022-11-05 21:29:56 +08:00
|
|
|
import React, { memo, useContext } from 'react';
|
2022-01-21 18:39:47 +08:00
|
|
|
import Row from '../row';
|
|
|
|
import RowContext from '../RowContext';
|
2022-11-05 21:29:56 +08:00
|
|
|
import { fireEvent, pureRender } from '../../../tests/utils';
|
2022-01-21 18:39:47 +08:00
|
|
|
|
2022-11-05 21:29:56 +08:00
|
|
|
let innerCount = 0;
|
|
|
|
let outerCount = 0;
|
2022-01-21 18:39:47 +08:00
|
|
|
|
2022-11-05 21:29:56 +08:00
|
|
|
const handleClick = () => {
|
|
|
|
outerCount++;
|
2022-01-21 18:39:47 +08:00
|
|
|
};
|
|
|
|
|
2022-11-05 21:29:56 +08:00
|
|
|
const CacheInner: React.FC = memo(() => {
|
|
|
|
innerCount++;
|
|
|
|
useContext(RowContext);
|
|
|
|
return null;
|
|
|
|
});
|
2022-01-21 18:39:47 +08:00
|
|
|
|
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-01-21 18:39:47 +08:00
|
|
|
|
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);
|
2022-08-16 10:14:41 +08:00
|
|
|
fireEvent.click(container.querySelector('#parent_btn')!);
|
2022-11-05 21:29:56 +08:00
|
|
|
expect(outerCount).toBe(1);
|
|
|
|
expect(innerCount).toBe(1);
|
|
|
|
unmount();
|
2022-01-21 18:39:47 +08:00
|
|
|
});
|