import React, { memo, useContext, useRef, useState } from 'react';
import Row from '../row';
import RowContext from '../RowContext';
import { render, fireEvent } from '../../../tests/utils';
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 { container } = render();
const childCount = container.querySelector('#child_count')?.textContent;
fireEvent.click(container.querySelector('#parent_btn')!);
expect(container.querySelector('#parent_count')?.textContent).toBe('2');
// child component won't rerender
expect(container.querySelector('#child_count')?.textContent).toBe(childCount);
fireEvent.click(container.querySelector('#parent_btn')!);
expect(container.querySelector('#parent_count')?.textContent).toBe('3');
// child component won't rerender
expect(container.querySelector('#child_count')?.textContent).toBe(childCount);
});