ant-design/components/config-provider/__tests__/memo.test.tsx
二货爱吃白萝卜 86b5c50cb4
refactor: Tooltip support auto arrow & auto shift (#40632)
* chore: adjust doc

* chore: simplify arrow offset

* chore: auto adjust shift

* docs: adjust demo

* chore: update snapshot

* chore: provide ref

* test: prepare

* chore: update deps

* test: fix part test

* test: fix part test

* test: fix part test

* test: fix part test

* test: fix part test

* test: update snapshot

* fix: missing pure render

* fix: Popover pure panel

* test: update snapshot

* test: update tour snapshot

* chore: bump trigger ver

* test: fix render ssr logic

* test: skip unnecessary case

* test: fix test case

* test: update snapshot

* test: update snapshot

* test: update snapshot

* test: update snapshot

* test: update snapshot

* test: fix test case

* test: fix test case

* chore: clean up useless warning

* test: add check for placement

* chore: ignore default
2023-02-15 10:21:28 +08:00

93 lines
2.8 KiB
TypeScript

import React, { useState } from 'react';
import ConfigProvider from '..';
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 }) => {
React.useEffect(() => spy());
return <div />;
};
const Sibling: React.FC<Props> = ({ spy }) => (
<Tooltip>
<Child spy={spy} />
</Tooltip>
);
it('should not generate new context config when render', () => {
const MemoedSibling = React.memo(Sibling);
const spy = jest.fn();
const App: React.FC = () => {
const [pageHeader, setPageHeader] = useState({ ghost: true });
const [, forceRender] = React.useReducer((v) => v + 1, 1);
return (
<ConfigProvider pageHeader={pageHeader}>
<button type="button" className="render" onClick={() => forceRender()}>
Force Render
</button>
<button
type="button"
className="setState"
onClick={() => setPageHeader({ ghost: false })}
>
Change Config
</button>
<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);
});
it('should not generate new context config in nested ConfigProvider when render', () => {
const MemoedSibling = React.memo(Sibling);
const spy = jest.fn();
const App: React.FC = () => {
const [pageHeader, setPageHeader] = useState({ ghost: true });
const [, forceRender] = React.useReducer((v) => v + 1, 1);
return (
<ConfigProvider pageHeader={pageHeader}>
<ConfigProvider>
<button type="button" className="render" onClick={() => forceRender()}>
Force Render
</button>
<button
type="button"
className="setState"
onClick={() => setPageHeader({ ghost: false })}
>
Change Config
</button>
<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);
});
});