|
10 | 10 | * governing permissions and limitations under the License.
|
11 | 11 | */
|
12 | 12 |
|
| 13 | +import {ClearSlots, SlotProvider, useSlotProps} from '../'; |
13 | 14 | import {pointerMap, render} from '@react-spectrum/test-utils-internal';
|
14 |
| -import React, {useRef} from 'react'; |
15 |
| -import {SlotProvider, useSlotProps} from '../'; |
| 15 | +import React, {StrictMode, useRef} from 'react'; |
16 | 16 | import {useId, useSlotId} from '@react-aria/utils';
|
17 | 17 | import {usePress} from '@react-aria/interactions';
|
18 | 18 | import userEvent from '@testing-library/user-event';
|
@@ -190,4 +190,99 @@ describe('Slots', function () {
|
190 | 190 | expect(getByRole('presentation')).toHaveAttribute('aria-controls', id);
|
191 | 191 | });
|
192 | 192 |
|
| 193 | + it('does not rerender slots consumers when the slot provider rerenders with stable values', function () { |
| 194 | + let slots = { |
| 195 | + slotname: {label: 'foo'} |
| 196 | + }; |
| 197 | + let renderCount = 0; |
| 198 | + |
| 199 | + const TestComponent = (props) => { |
| 200 | + useSlotProps(props, 'slotname'); |
| 201 | + React.useEffect(() => { |
| 202 | + renderCount++; |
| 203 | + }); |
| 204 | + |
| 205 | + return <p>test component</p>; |
| 206 | + }; |
| 207 | + |
| 208 | + const MemoizedComponent = React.memo(function MemoizedComponent(props) { |
| 209 | + return props.children; |
| 210 | + }); |
| 211 | + |
| 212 | + const FullComponentTree = () => { |
| 213 | + const StableTestComponent = React.useMemo(() => <TestComponent prop1="value1" />, []); |
| 214 | + |
| 215 | + return ( |
| 216 | + <StrictMode> |
| 217 | + <SlotProvider> |
| 218 | + <MemoizedComponent> |
| 219 | + {StableTestComponent} |
| 220 | + </MemoizedComponent> |
| 221 | + </SlotProvider> |
| 222 | + </StrictMode> |
| 223 | + ); |
| 224 | + }; |
| 225 | + |
| 226 | + const {rerender} = render( |
| 227 | + <FullComponentTree slots={slots} /> |
| 228 | + ); |
| 229 | + |
| 230 | + let renderCountBeforeRerender = renderCount; |
| 231 | + |
| 232 | + // Trigger a rerender with the same stable props |
| 233 | + rerender( |
| 234 | + <FullComponentTree slots={slots} /> |
| 235 | + ); |
| 236 | + |
| 237 | + expect(renderCount).toEqual(renderCountBeforeRerender); |
| 238 | + }); |
| 239 | + |
| 240 | + it('does not rerender slots consumers when <ClearSlot /> wrapper is placed between SlotProvider and Consumer', function () { |
| 241 | + let slots = { |
| 242 | + slotname: {label: 'foo'} |
| 243 | + }; |
| 244 | + let renderCount = 0; |
| 245 | + |
| 246 | + const TestComponent = (props) => { |
| 247 | + useSlotProps(props, 'slotname'); |
| 248 | + React.useEffect(() => { |
| 249 | + renderCount++; |
| 250 | + }); |
| 251 | + |
| 252 | + return <p>test component</p>; |
| 253 | + }; |
| 254 | + |
| 255 | + const MemoizedComponent = React.memo(function MemoizedComponent(props) { |
| 256 | + return props.children; |
| 257 | + }); |
| 258 | + |
| 259 | + const FullComponentTree = () => { |
| 260 | + const StableTestComponent = React.useMemo(() => <TestComponent prop1="value1" />, []); |
| 261 | + |
| 262 | + return ( |
| 263 | + <StrictMode> |
| 264 | + <SlotProvider> |
| 265 | + <MemoizedComponent> |
| 266 | + <ClearSlots> |
| 267 | + {StableTestComponent} |
| 268 | + </ClearSlots> |
| 269 | + </MemoizedComponent> |
| 270 | + </SlotProvider> |
| 271 | + </StrictMode> |
| 272 | + ); |
| 273 | + }; |
| 274 | + |
| 275 | + const {rerender} = render( |
| 276 | + <FullComponentTree slots={slots} /> |
| 277 | + ); |
| 278 | + |
| 279 | + let renderCountBeforeRerender = renderCount; |
| 280 | + |
| 281 | + // Trigger a rerender with the same stable props |
| 282 | + rerender( |
| 283 | + <FullComponentTree slots={slots} /> |
| 284 | + ); |
| 285 | + |
| 286 | + expect(renderCount).toEqual(renderCountBeforeRerender); |
| 287 | + }); |
193 | 288 | });
|
0 commit comments