ant-design/components/config-provider/__tests__/memo.test.tsx

93 lines
2.8 KiB
TypeScript
Raw Normal View History

2022-06-22 14:57:09 +08:00
import React, { useState } from 'react';
import ConfigProvider from '..';
2021-01-18 16:51:34 +08:00
import Tooltip from '../../tooltip';
import { pureRender, fireEvent } from '../../../tests/utils';
interface Props {
spy: () => void;
}
// https://github.com/ant-design/ant-design/issues/27617
describe('ConfigProvider', () => {
const Child: React.FC<Props> = ({ spy }) => {
2021-01-18 16:51:34 +08:00
React.useEffect(() => spy());
return <div />;
};
const Sibling: React.FC<Props> = ({ spy }) => (
2021-01-18 16:51:34 +08:00
<Tooltip>
<Child spy={spy} />
</Tooltip>
);
2021-01-12 21:02:53 +08:00
it('should not generate new context config when render', () => {
const MemoedSibling = React.memo(Sibling);
2021-01-18 16:51:34 +08:00
const spy = jest.fn();
const App: React.FC = () => {
2021-01-12 21:02:53 +08:00
const [pageHeader, setPageHeader] = useState({ ghost: true });
const [, forceRender] = React.useReducer((v) => v + 1, 1);
return (
2021-01-12 21:02:53 +08:00
<ConfigProvider pageHeader={pageHeader}>
<button type="button" className="render" onClick={() => forceRender()}>
Force Render
</button>
<button
type="button"
2021-01-12 21:02:53 +08:00
className="setState"
onClick={() => setPageHeader({ ghost: false })}
>
2021-01-12 21:02:53 +08:00
Change Config
</button>
2021-01-18 16:51:34 +08:00
<MemoedSibling spy={spy} />
</ConfigProvider>
);
};
const { container } = pureRender(<App />);
const startCalledTimes = spy.mock.calls.length;
fireEvent.click(container.querySelector('.render')!);
expect(spy.mock.calls.length).toEqual(startCalledTimes);
fireEvent.click(container.querySelector('.setState')!);
expect(spy.mock.calls.length).toEqual(startCalledTimes + 1);
});
2021-01-12 21:02:53 +08:00
it('should not generate new context config in nested ConfigProvider when render', () => {
const MemoedSibling = React.memo(Sibling);
2021-01-18 16:51:34 +08:00
const spy = jest.fn();
const App: React.FC = () => {
2021-01-12 21:02:53 +08:00
const [pageHeader, setPageHeader] = useState({ ghost: true });
const [, forceRender] = React.useReducer((v) => v + 1, 1);
2021-01-12 21:02:53 +08:00
return (
2021-01-12 21:02:53 +08:00
<ConfigProvider pageHeader={pageHeader}>
<ConfigProvider>
2021-01-12 21:02:53 +08:00
<button type="button" className="render" onClick={() => forceRender()}>
Force Render
</button>
<button
type="button"
2021-01-12 21:02:53 +08:00
className="setState"
onClick={() => setPageHeader({ ghost: false })}
>
2021-01-12 21:02:53 +08:00
Change Config
</button>
2021-01-18 16:51:34 +08:00
<MemoedSibling spy={spy} />
</ConfigProvider>
</ConfigProvider>
);
};
const { container } = pureRender(<App />);
const startCalledTimes = spy.mock.calls.length;
fireEvent.click(container.querySelector('.render')!);
expect(spy.mock.calls.length).toEqual(startCalledTimes);
fireEvent.click(container.querySelector('.setState')!);
expect(spy.mock.calls.length).toEqual(startCalledTimes + 1);
});
});