2022-11-04 11:24:56 +08:00
|
|
|
import React, { memo, useContext } from 'react';
|
2022-02-18 18:34:21 +08:00
|
|
|
import Menu from '../index';
|
|
|
|
import MenuContext from '../MenuContext';
|
2022-11-04 11:24:56 +08:00
|
|
|
import { fireEvent, pureRender } from '../../../tests/utils';
|
|
|
|
|
|
|
|
let innerCount = 0;
|
|
|
|
let outerCount = 0;
|
|
|
|
|
|
|
|
const handleClick = () => {
|
|
|
|
outerCount++;
|
|
|
|
};
|
2022-02-18 18:34:21 +08:00
|
|
|
|
|
|
|
// we use'memo' here in order to only render inner component while context changed.
|
2022-11-04 11:24:56 +08:00
|
|
|
const CacheInner: React.FC = memo(() => {
|
|
|
|
innerCount++;
|
|
|
|
// subscribe locale context
|
2022-02-18 18:34:21 +08:00
|
|
|
useContext(MenuContext);
|
2022-11-04 11:24:56 +08:00
|
|
|
return null;
|
2022-02-18 18:34:21 +08:00
|
|
|
});
|
|
|
|
|
2022-11-04 11:24:56 +08:00
|
|
|
const CacheOuter: React.FC = memo(() => (
|
|
|
|
<>
|
|
|
|
<button type="button" onClick={handleClick} id="parent_btn">
|
|
|
|
Click
|
|
|
|
</button>
|
|
|
|
<Menu>
|
|
|
|
<Menu.Item key="test">
|
|
|
|
<CacheInner />
|
|
|
|
</Menu.Item>
|
|
|
|
</Menu>
|
|
|
|
</>
|
|
|
|
));
|
2022-02-18 18:34:21 +08:00
|
|
|
|
|
|
|
it("Rendering on Menu without changed MenuContext won't trigger rendering on child component.", () => {
|
2022-11-04 11:24:56 +08:00
|
|
|
const { container, unmount } = pureRender(<CacheOuter />);
|
|
|
|
expect(outerCount).toBe(0);
|
|
|
|
expect(innerCount).toBe(1);
|
2022-08-30 10:06:38 +08:00
|
|
|
fireEvent.click(container.querySelector('#parent_btn')!);
|
2022-11-04 11:24:56 +08:00
|
|
|
expect(outerCount).toBe(1);
|
|
|
|
expect(innerCount).toBe(1);
|
2022-08-30 10:06:38 +08:00
|
|
|
unmount();
|
2022-02-18 18:34:21 +08:00
|
|
|
});
|