22
let setClientRects;
23
let mockRangeClientRects;
24
let assertConsoleErrorDev;
25
+let assertConsoleWarnDev;
26
27
function Wrapper({children}) {
28
return children;
45
mockRangeClientRects = IntersectionMocks.mockRangeClientRects;
46
assertConsoleErrorDev =
47
require('internal-test-utils').assertConsoleErrorDev;
48
+ assertConsoleWarnDev = require('internal-test-utils').assertConsoleWarnDev;
49
50
container = document.createElement('div');
51
document.body.innerHTML = '';
2590
fragmentRef.current.scrollIntoView();
2591
expect(parentRef.current.scrollIntoView).toHaveBeenCalledTimes(1);
2592
});
2593
+
2594
+ // @gate enableFragmentRefs && enableFragmentRefsScrollIntoView
2595
+ it('scrolls the host element when the fallback target is a ShadowRoot container', async () => {
2596
+ const fragmentRef = React.createRef();
2597
+ const host = document.createElement('div');
2598
+ container.appendChild(host);
2599
+ const shadowRoot = host.attachShadow({mode: 'open'});
2600
+ const root = ReactDOMClient.createRoot(shadowRoot);
2601
+ await act(() => {
2602
+ root.render(<Fragment ref={fragmentRef} />);
2603
+ });
2604
+
2605
+ // The ShadowRoot's host element marks where the fragment's content
2606
+ // would appear
2607
+ host.scrollIntoView = jest.fn();
2608
+ fragmentRef.current.scrollIntoView();
2609
+ expect(host.scrollIntoView).toHaveBeenCalledTimes(1);
2610
+ });
2611
+
2612
+ // @gate enableFragmentRefs && enableFragmentRefsScrollIntoView
2613
+ it('warns without scrolling when the fallback target is a detached DocumentFragment container', async () => {
2614
+ const fragmentRef = React.createRef();
2615
+ const root = ReactDOMClient.createRoot(
2616
+ document.createDocumentFragment(),
2617
+ );
2618
+ await act(() => {
2619
+ root.render(<Fragment ref={fragmentRef} />);
2620
+ });
2621
+
2622
+ expect(() => fragmentRef.current.scrollIntoView()).not.toThrow();
2623
+ assertConsoleWarnDev(
2624
+ [
2625
+ 'You are attempting to scroll a FragmentInstance that is only ' +
2626
+ 'mounted inside a detached DocumentFragment. No scroll was ' +
2627
+ 'performed.',
2628
+ ],
2629
+ {withoutStack: true},
2630
+ );
2631
+ });
2632
});
2633
});
2634