2022-10-10 14:15:08 +08:00
|
|
|
import React, { memo, useContext } from 'react';
|
|
|
|
import { fireEvent, pureRender } from '../../../tests/utils';
|
2022-12-09 15:04:08 +08:00
|
|
|
import LocaleProvider from '../../locale';
|
|
|
|
import LocaleContext from '../../locale/context';
|
2022-01-14 18:34:27 +08:00
|
|
|
|
2022-10-10 14:15:08 +08:00
|
|
|
let innerCount = 0;
|
|
|
|
let outerCount = 0;
|
|
|
|
|
|
|
|
const handleClick = () => {
|
|
|
|
outerCount++;
|
|
|
|
};
|
|
|
|
|
2022-01-14 18:34:27 +08:00
|
|
|
// we use'memo' here in order to only render inner component while context changed.
|
2022-10-10 14:15:08 +08:00
|
|
|
const CacheInner: React.FC = memo(() => {
|
|
|
|
innerCount++;
|
2022-01-14 18:34:27 +08:00
|
|
|
// subscribe locale context
|
|
|
|
useContext(LocaleContext);
|
2022-10-10 14:15:08 +08:00
|
|
|
return null;
|
2022-01-14 18:34:27 +08:00
|
|
|
});
|
|
|
|
|
2022-10-10 14:15:08 +08:00
|
|
|
const CacheOuter: React.FC = memo(() => (
|
|
|
|
<>
|
|
|
|
<button type="button" onClick={handleClick} id="parent_btn">
|
|
|
|
Click
|
|
|
|
</button>
|
|
|
|
<LocaleProvider locale={{ locale: 'locale' }}>
|
|
|
|
<CacheInner />
|
|
|
|
</LocaleProvider>
|
|
|
|
</>
|
|
|
|
));
|
2022-11-04 11:20:53 +08:00
|
|
|
|
2022-01-14 18:34:27 +08:00
|
|
|
it("Rendering on LocaleProvider won't trigger rendering on child component.", () => {
|
2022-11-04 11:20:53 +08:00
|
|
|
const { container, unmount } = pureRender(<CacheOuter />);
|
2022-10-10 14:15:08 +08:00
|
|
|
expect(outerCount).toBe(0);
|
|
|
|
expect(innerCount).toBe(1);
|
2022-08-23 21:50:45 +08:00
|
|
|
fireEvent.click(container.querySelector('#parent_btn')!);
|
2022-10-10 14:15:08 +08:00
|
|
|
expect(outerCount).toBe(1);
|
|
|
|
expect(innerCount).toBe(1);
|
2022-11-04 11:20:53 +08:00
|
|
|
unmount();
|
2022-01-14 18:34:27 +08:00
|
|
|
});
|