2022-06-22 14:57:09 +08:00
|
|
|
import React, { memo, useContext, useRef, useState } from 'react';
|
2022-01-21 18:39:47 +08:00
|
|
|
import Row from '../row';
|
|
|
|
import RowContext from '../RowContext';
|
2022-08-16 10:14:41 +08:00
|
|
|
import { render, fireEvent } from '../../../tests/utils';
|
2022-01-21 18:39:47 +08:00
|
|
|
|
|
|
|
const CacheInner = memo(() => {
|
|
|
|
const countRef = useRef(0);
|
|
|
|
countRef.current++;
|
|
|
|
useContext(RowContext);
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
Child Rendering Count: <span id="child_count">{countRef.current}</span>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
const CacheOuter = () => {
|
|
|
|
const [count, setCount] = useState(1);
|
|
|
|
const handleClick = () => {
|
|
|
|
setCount(count + 1);
|
|
|
|
};
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<button type="button" onClick={handleClick} id="parent_btn">
|
|
|
|
Click
|
|
|
|
</button>
|
|
|
|
Parent Rendering Count: <span id="parent_count">{count}</span>
|
|
|
|
<Row>
|
|
|
|
<CacheInner />
|
|
|
|
</Row>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
it('Cached RowContext is working', () => {
|
2022-08-16 10:14:41 +08:00
|
|
|
const { container } = render(<CacheOuter />);
|
|
|
|
const childCount = container.querySelector('#child_count')?.textContent;
|
2022-01-21 18:39:47 +08:00
|
|
|
|
2022-08-16 10:14:41 +08:00
|
|
|
fireEvent.click(container.querySelector('#parent_btn')!);
|
|
|
|
expect(container.querySelector('#parent_count')?.textContent).toBe('2');
|
2022-01-21 18:39:47 +08:00
|
|
|
// child component won't rerender
|
2022-08-16 10:14:41 +08:00
|
|
|
expect(container.querySelector('#child_count')?.textContent).toBe(childCount);
|
2022-01-21 18:39:47 +08:00
|
|
|
|
2022-08-16 10:14:41 +08:00
|
|
|
fireEvent.click(container.querySelector('#parent_btn')!);
|
|
|
|
expect(container.querySelector('#parent_count')?.textContent).toBe('3');
|
2022-01-21 18:39:47 +08:00
|
|
|
// child component won't rerender
|
2022-08-16 10:14:41 +08:00
|
|
|
expect(container.querySelector('#child_count')?.textContent).toBe(childCount);
|
2022-01-21 18:39:47 +08:00
|
|
|
});
|