2021-01-12 21:02:53 +08:00
|
|
|
import React, { useState } from 'react';
|
2021-01-09 15:22:08 +08:00
|
|
|
import { mount } from 'enzyme';
|
|
|
|
import ConfigProvider from '..';
|
2021-01-18 16:51:34 +08:00
|
|
|
import Tooltip from '../../tooltip';
|
2021-01-09 15:22:08 +08:00
|
|
|
|
|
|
|
// https://github.com/ant-design/ant-design/issues/27617
|
|
|
|
describe('ConfigProvider', () => {
|
2021-01-18 16:51:34 +08:00
|
|
|
const Child = ({ spy }) => {
|
|
|
|
React.useEffect(() => spy());
|
|
|
|
return <div />;
|
2021-01-09 15:22:08 +08:00
|
|
|
};
|
|
|
|
|
2021-01-18 16:51:34 +08:00
|
|
|
const Sibling = ({ spy }) => (
|
|
|
|
<Tooltip>
|
|
|
|
<Child spy={spy} />
|
|
|
|
</Tooltip>
|
2021-01-09 15:22:08 +08:00
|
|
|
);
|
|
|
|
|
2021-01-12 21:02:53 +08:00
|
|
|
it('should not generate new context config when render', () => {
|
2021-01-09 15:22:08 +08:00
|
|
|
const MemoedSibling = React.memo(Sibling);
|
2021-01-18 16:51:34 +08:00
|
|
|
const spy = jest.fn();
|
2021-01-09 15:22:08 +08:00
|
|
|
const App = () => {
|
2021-01-12 21:02:53 +08:00
|
|
|
const [pageHeader, setPageHeader] = useState({ ghost: true });
|
2021-01-12 10:40:15 +08:00
|
|
|
const [, forceRender] = React.useReducer(v => v + 1, 1);
|
2021-01-09 15:22:08 +08:00
|
|
|
|
2021-01-12 10:40:15 +08:00
|
|
|
return (
|
2021-01-12 21:02:53 +08:00
|
|
|
<ConfigProvider pageHeader={pageHeader}>
|
|
|
|
<button type="button" className="render" onClick={() => forceRender()}>
|
|
|
|
Force Render
|
2021-01-09 15:22:08 +08:00
|
|
|
</button>
|
|
|
|
<button
|
|
|
|
type="button"
|
2021-01-12 21:02:53 +08:00
|
|
|
className="setState"
|
|
|
|
onClick={() => setPageHeader({ ghost: false })}
|
2021-01-09 15:22:08 +08:00
|
|
|
>
|
2021-01-12 21:02:53 +08:00
|
|
|
Change Config
|
2021-01-09 15:22:08 +08:00
|
|
|
</button>
|
2021-01-18 16:51:34 +08:00
|
|
|
<MemoedSibling spy={spy} />
|
2021-01-09 15:22:08 +08:00
|
|
|
</ConfigProvider>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const wrapper = mount(<App />);
|
2021-01-12 21:02:53 +08:00
|
|
|
wrapper.find('.render').simulate('click');
|
2021-01-18 16:51:34 +08:00
|
|
|
expect(spy.mock.calls.length).toEqual(1);
|
2021-01-09 15:22:08 +08:00
|
|
|
|
2021-01-12 21:02:53 +08:00
|
|
|
wrapper.find('.setState').simulate('click');
|
2021-01-18 16:51:34 +08:00
|
|
|
expect(spy.mock.calls.length).toEqual(2);
|
2021-01-09 15:22:08 +08:00
|
|
|
});
|
2021-01-12 10:40:15 +08:00
|
|
|
|
2021-01-12 21:02:53 +08:00
|
|
|
it('should not generate new context config in nested ConfigProvider when render', () => {
|
2021-01-12 10:40:15 +08:00
|
|
|
const MemoedSibling = React.memo(Sibling);
|
2021-01-18 16:51:34 +08:00
|
|
|
const spy = jest.fn();
|
2021-01-12 10:40:15 +08:00
|
|
|
const App = () => {
|
2021-01-12 21:02:53 +08:00
|
|
|
const [pageHeader, setPageHeader] = useState({ ghost: true });
|
2021-01-12 10:40:15 +08:00
|
|
|
const [, forceRender] = React.useReducer(v => v + 1, 1);
|
2021-01-12 21:02:53 +08:00
|
|
|
|
2021-01-12 10:40:15 +08:00
|
|
|
return (
|
2021-01-12 21:02:53 +08:00
|
|
|
<ConfigProvider pageHeader={pageHeader}>
|
2021-01-12 10:40:15 +08:00
|
|
|
<ConfigProvider>
|
2021-01-12 21:02:53 +08:00
|
|
|
<button type="button" className="render" onClick={() => forceRender()}>
|
|
|
|
Force Render
|
2021-01-12 10:40:15 +08:00
|
|
|
</button>
|
|
|
|
<button
|
|
|
|
type="button"
|
2021-01-12 21:02:53 +08:00
|
|
|
className="setState"
|
|
|
|
onClick={() => setPageHeader({ ghost: false })}
|
2021-01-12 10:40:15 +08:00
|
|
|
>
|
2021-01-12 21:02:53 +08:00
|
|
|
Change Config
|
2021-01-12 10:40:15 +08:00
|
|
|
</button>
|
2021-01-18 16:51:34 +08:00
|
|
|
<MemoedSibling spy={spy} />
|
2021-01-12 10:40:15 +08:00
|
|
|
</ConfigProvider>
|
|
|
|
</ConfigProvider>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const wrapper = mount(<App />);
|
2021-01-12 21:02:53 +08:00
|
|
|
wrapper.find('.render').simulate('click');
|
2021-01-18 16:51:34 +08:00
|
|
|
expect(spy.mock.calls.length).toEqual(1);
|
2021-01-12 10:40:15 +08:00
|
|
|
|
2021-01-12 21:02:53 +08:00
|
|
|
wrapper.find('.setState').simulate('click');
|
2021-01-18 16:51:34 +08:00
|
|
|
expect(spy.mock.calls.length).toEqual(2);
|
2021-01-12 10:40:15 +08:00
|
|
|
});
|
2021-01-09 15:22:08 +08:00
|
|
|
});
|