Browse Source
* fix: fix * fix: fix * fix: fix * fix: fix * fix: fix * fix: fix * fix: fix * fix: fix * fix: fix * test: fix test * fix: fix * fix: fix * fix: fix * test: add test * fix: fix testpull/37915/head
lijianan
2 years ago
committed by
GitHub
5 changed files with 85 additions and 126 deletions
@ -1,51 +1,38 @@ |
|||||
import React, { memo, useContext, useRef, useState } from 'react'; |
import React, { memo, useContext } from 'react'; |
||||
import { render, fireEvent } from '../../../tests/utils'; |
import { fireEvent, pureRender } from '../../../tests/utils'; |
||||
import LocaleProvider, { ANT_MARK } from '..'; |
import LocaleProvider from '..'; |
||||
import LocaleContext from '../context'; |
import LocaleContext from '../context'; |
||||
|
|
||||
const defaultLocale = { locale: 'locale' }; |
let innerCount = 0; |
||||
|
let outerCount = 0; |
||||
|
|
||||
|
const handleClick = () => { |
||||
|
outerCount++; |
||||
|
}; |
||||
|
|
||||
// we use'memo' here in order to only render inner component while context changed.
|
// we use'memo' here in order to only render inner component while context changed.
|
||||
const CacheInner = memo(() => { |
const CacheInner: React.FC = memo(() => { |
||||
const countRef = useRef(0); |
innerCount++; |
||||
countRef.current++; |
|
||||
// subscribe locale context
|
// subscribe locale context
|
||||
useContext(LocaleContext); |
useContext(LocaleContext); |
||||
return ( |
return null; |
||||
<div> |
|
||||
Child Rendering Count: <span id="child_count">{countRef.current}</span> |
|
||||
</div> |
|
||||
); |
|
||||
}); |
}); |
||||
|
|
||||
const CacheOuter: React.FC = () => { |
const CacheOuter: React.FC = memo(() => ( |
||||
// We use 'useState' here in order to trigger parent component rendering.
|
<> |
||||
const [count, setCount] = useState(1); |
<button type="button" onClick={handleClick} id="parent_btn"> |
||||
const handleClick = () => { |
Click |
||||
setCount(count + 1); |
</button> |
||||
}; |
<LocaleProvider locale={{ locale: 'locale' }}> |
||||
// During each rendering phase, the cached context value returned from method 'LocaleProvider#getMemoizedContextValue' will take effect.
|
<CacheInner /> |
||||
// So 'CacheInner' component won't rerender.
|
</LocaleProvider> |
||||
return ( |
</> |
||||
<div> |
)); |
||||
<button type="button" onClick={handleClick} id="parent_btn"> |
|
||||
Click |
|
||||
</button> |
|
||||
Parent Rendering Count: <span id="parent_count">{count}</span> |
|
||||
<LocaleProvider locale={defaultLocale} _ANT_MARK__={ANT_MARK}> |
|
||||
<CacheInner /> |
|
||||
</LocaleProvider> |
|
||||
</div> |
|
||||
); |
|
||||
}; |
|
||||
|
|
||||
it("Rendering on LocaleProvider won't trigger rendering on child component.", () => { |
it("Rendering on LocaleProvider won't trigger rendering on child component.", () => { |
||||
const { container } = render(<CacheOuter />); |
const { container } = pureRender(<CacheOuter />); |
||||
fireEvent.click(container.querySelector('#parent_btn')!); |
expect(outerCount).toBe(0); |
||||
expect(container.querySelector('#parent_count')?.innerHTML).toBe('2'); |
expect(innerCount).toBe(1); |
||||
// child component won't rerender
|
|
||||
expect(container.querySelector('#child_count')?.innerHTML).toBe('1'); |
|
||||
fireEvent.click(container.querySelector('#parent_btn')!); |
fireEvent.click(container.querySelector('#parent_btn')!); |
||||
expect(container.querySelector('#parent_count')?.innerHTML).toBe('3'); |
expect(outerCount).toBe(1); |
||||
// child component won't rerender
|
expect(innerCount).toBe(1); |
||||
expect(container.querySelector('#child_count')?.innerHTML).toBe('1'); |
|
||||
}); |
}); |
||||
|
Loading…
Reference in new issue