Browse Source

test: simplify test case (#38403)

pull/38406/head
lijianan 2 years ago
committed by GitHub
parent
commit
66e4b1c91c
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 66
      components/grid/__tests__/cached-row-context.test.tsx

66
components/grid/__tests__/cached-row-context.test.tsx

@ -1,48 +1,38 @@
import React, { memo, useContext, useRef, useState } from 'react';
import React, { memo, useContext } from 'react';
import Row from '../row';
import RowContext from '../RowContext';
import { render, fireEvent } from '../../../tests/utils';
import { fireEvent, pureRender } from '../../../tests/utils';
const CacheInner = memo(() => {
const countRef = useRef(0);
countRef.current++;
useContext(RowContext);
return (
<div>
Child Rendering Count: <span id="child_count">{countRef.current}</span>
</div>
);
});
let innerCount = 0;
let outerCount = 0;
const CacheOuter = () => {
const [count, setCount] = useState(1);
const handleClick = () => {
setCount(count + 1);
};
return (
<div>
<button type="button" onClick={handleClick} id="parent_btn">
Click
</button>
Parent Rendering Count: <span id="parent_count">{count}</span>
<Row>
<CacheInner />
</Row>
</div>
);
const handleClick = () => {
outerCount++;
};
it('Cached RowContext is working', () => {
const { container } = render(<CacheOuter />);
const childCount = container.querySelector('#child_count')?.textContent;
const CacheInner: React.FC = memo(() => {
innerCount++;
useContext(RowContext);
return null;
});
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);
const CacheOuter: React.FC = memo(() => (
<>
<button type="button" onClick={handleClick} id="parent_btn">
Click
</button>
<Row>
<CacheInner />
</Row>
</>
));
it('Cached RowContext is working', () => {
const { container, unmount } = pureRender(<CacheOuter />);
expect(outerCount).toBe(0);
expect(innerCount).toBe(1);
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);
expect(outerCount).toBe(1);
expect(innerCount).toBe(1);
unmount();
});

Loading…
Cancel
Save