2022-06-22 14:57:09 +08:00
|
|
|
import React, { memo, useContext, useRef, useState } from 'react';
|
2022-02-18 18:34:21 +08:00
|
|
|
import Menu from '../index';
|
|
|
|
import MenuContext from '../MenuContext';
|
2022-08-30 10:06:38 +08:00
|
|
|
import { render, fireEvent } from '../../../tests/utils';
|
2022-02-18 18:34:21 +08:00
|
|
|
|
|
|
|
// we use'memo' here in order to only render inner component while context changed.
|
|
|
|
const CacheInner = memo(() => {
|
|
|
|
const countRef = useRef(0);
|
|
|
|
countRef.current++;
|
|
|
|
// subscribe anchor context
|
|
|
|
useContext(MenuContext);
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
Child Rendering Count: <span id="child_count">{countRef.current}</span>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
const CacheOuter = () => {
|
|
|
|
// We use 'useState' here in order to trigger parent component rendering.
|
|
|
|
const [count, setCount] = useState(1);
|
|
|
|
const handleClick = () => {
|
|
|
|
setCount(count + 1);
|
|
|
|
};
|
|
|
|
// During each rendering phase, the cached context value returned from method 'Menu#getMemoizedContextValue' will take effect.
|
|
|
|
// So 'CacheInner' component won't rerender.
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<button type="button" onClick={handleClick} id="parent_btn">
|
|
|
|
Click
|
|
|
|
</button>
|
|
|
|
Parent Rendering Count: <span id="parent_count">{count}</span>
|
|
|
|
<Menu>
|
|
|
|
<Menu.Item key="test">
|
|
|
|
<CacheInner />
|
|
|
|
</Menu.Item>
|
|
|
|
</Menu>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
it("Rendering on Menu without changed MenuContext won't trigger rendering on child component.", () => {
|
2022-08-30 10:06:38 +08:00
|
|
|
const { container, unmount } = render(<CacheOuter />);
|
|
|
|
const childCount = container.querySelector('#child_count')?.textContent;
|
|
|
|
fireEvent.click(container.querySelector('#parent_btn')!);
|
|
|
|
expect(container.querySelector('#parent_count')?.textContent).toBe('2');
|
2022-02-18 18:34:21 +08:00
|
|
|
// child component won't rerender
|
2022-08-30 10:06:38 +08:00
|
|
|
expect(container.querySelector('#child_count')?.textContent).toBe(childCount);
|
|
|
|
fireEvent.click(container.querySelector('#parent_btn')!);
|
|
|
|
expect(container.querySelector('#parent_count')?.textContent).toBe('3');
|
2022-02-18 18:34:21 +08:00
|
|
|
// child component won't rerender
|
2022-08-30 10:06:38 +08:00
|
|
|
expect(container.querySelector('#child_count')?.textContent).toBe(childCount);
|
2022-02-18 18:34:21 +08:00
|
|
|
// in order to depress warning "Warning: An update to Menu inside a test was not wrapped in act(...)."
|
2022-08-30 10:06:38 +08:00
|
|
|
unmount();
|
2022-02-18 18:34:21 +08:00
|
|
|
});
|