import React, { memo, useState, useRef, useContext } from 'react'; import { mount } from 'enzyme'; import LocaleProvider, { ANT_MARK } from '..'; import LocaleContext from '../context'; const defaultLocale = { locale: 'locale', }; // we use'memo' here in order to only render inner component while context changed. const CacheInner = memo(() => { const countRef = useRef(0); countRef.current++; // subscribe locale context useContext(LocaleContext); return (
Child Rendering Count: {countRef.current}
); }); const CacheOuter = () => { // We use 'useState' here in order to trigger parent component rendering. const [count, setCount] = useState(1); const handleClick = () => { setCount(count + 1); }; // During each rendering phase, the cached context value returned from method 'LocaleProvider#getMemoizedContextValue' will take effect. // So 'CacheInner' component won't rerender. return (
Parent Rendering Count: {count}
); }; it("Rendering on LocaleProvider won't trigger rendering on child component.", () => { const wrapper = mount(); wrapper.find('#parent_btn').at(0).simulate('click'); expect(wrapper.find('#parent_count').text()).toBe('2'); // child component won't rerender expect(wrapper.find('#child_count').text()).toBe('1'); wrapper.find('#parent_btn').at(0).simulate('click'); expect(wrapper.find('#parent_count').text()).toBe('3'); // child component won't rerender expect(wrapper.find('#child_count').text()).toBe('1'); });