File size: 2,719 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
import React from 'react';
import ConfigProvider from '..';
import { fireEvent, pureRender } from '../../../tests/utils';
import Tooltip from '../../tooltip';
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 [flex, setFlex] = React.useState({ vertical: true });
const [, forceRender] = React.useReducer((v) => v + 1, 1);
return (
<ConfigProvider flex={flex}>
<button type="button" className="render" onClick={forceRender}>
Force Render
</button>
<button type="button" className="setState" onClick={() => setFlex({ vertical: 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 [flex, setFlex] = React.useState({ vertical: true });
const [, forceRender] = React.useReducer((v) => v + 1, 1);
return (
<ConfigProvider flex={flex}>
<ConfigProvider>
<button type="button" className="render" onClick={forceRender}>
Force Render
</button>
<button type="button" className="setState" onClick={() => setFlex({ vertical: 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);
});
});
|