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

85 lines
2.5 KiB
JavaScript
Raw Normal View History

import { mount } from 'enzyme';
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';
// 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-18 16:51:34 +08:00
const Sibling = ({ spy }) => (
<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 = () => {
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 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 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 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 = () => {
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 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 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);
});
});