import React, { memo, useRef, useState, useContext } from 'react'; import { mount } from 'enzyme'; import Row from '../row'; import RowContext from '../RowContext'; const CacheInner = memo(() => { const countRef = useRef(0); countRef.current++; useContext(RowContext); return (
Child Rendering Count: {countRef.current}
); }); const CacheOuter = () => { const [count, setCount] = useState(1); const handleClick = () => { setCount(count + 1); }; return (
Parent Rendering Count: {count}
); }; it('Cached RowContext is working', () => { const wrapper = mount(); const childCount = wrapper.find('#child_count').text(); 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(childCount); 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(childCount); });