import React, { ComponentProps, StrictMode } from 'react'; import { render, waitFor } from '@testing-library/react'; import { Popover } from '@leafygreen-ui/popover'; import { TestDescendant, TestDescendant2, TestDescendantContext, TestParent, TestParent2, } from '../testUtils/components.testutils'; import { renderDescendantsTestContext } from '../testUtils/renderDescendantsTestContext.testutils'; import { useInitDescendants } from '.'; describe('packages/descendants', () => { describe('rendering', () => { test('renders a basic list of descendants', () => { const { queryByText } = render( Apple Banana Carrot , ); const apple = queryByText('Apple'); const banana = queryByText('Banana'); const carrot = queryByText('Carrot'); expect(apple).toHaveAttribute('data-index', '0'); expect(banana).toHaveAttribute('data-index', '1'); expect(carrot).toHaveAttribute('data-index', '2'); }); test('renders a nested list of descendants', () => { const { queryByText } = render( Peppers Bell Yellow Watermelon , ); const peppers = queryByText('Peppers'); const bell = queryByText('Bell'); const yellow = queryByText('Yellow'); const watermelon = queryByText('Watermelon'); expect(peppers).toHaveAttribute('data-index', '0'); expect(bell).toHaveAttribute('data-index', '1'); expect(yellow).toHaveAttribute('data-index', '2'); expect(watermelon).toHaveAttribute('data-index', '3'); }); test('adds items to the rendered list', () => { const { queryByText, rerender } = render( Apple Banana Carrot , ); rerender( Apple Watermelon Banana Carrot , ); const apple = queryByText('Apple'); const banana = queryByText('Banana'); const carrot = queryByText('Carrot'); const watermelon = queryByText('Watermelon'); expect(apple).toHaveAttribute('data-index', '0'); expect(watermelon).toHaveAttribute('data-index', '1'); expect(banana).toHaveAttribute('data-index', '2'); expect(carrot).toHaveAttribute('data-index', '3'); }); test('removes items from the rendered list', () => { const { queryByText, rerender } = render( Apple Banana Carrot Dragonfruit , ); rerender( Apple Carrot Dragonfruit , ); const apple = queryByText('Apple'); const banana = queryByText('Banana'); const carrot = queryByText('Carrot'); const dragonfruit = queryByText('Dragonfruit'); expect(apple).toHaveAttribute('data-index', '0'); expect(banana).not.toBeInTheDocument(); expect(carrot).toHaveAttribute('data-index', '1'); expect(dragonfruit).toHaveAttribute('data-index', '2'); }); test('reorders items in the rendered list', () => { const { queryByText, rerender } = render( Apple Banana Carrot Dragonfruit , ); rerender( Banana Dragonfruit Apple Carrot , ); const apple = queryByText('Apple'); const banana = queryByText('Banana'); const carrot = queryByText('Carrot'); const dragonfruit = queryByText('Dragonfruit'); expect(banana).toHaveAttribute('data-index', '0'); expect(dragonfruit).toHaveAttribute('data-index', '1'); expect(apple).toHaveAttribute('data-index', '2'); expect(carrot).toHaveAttribute('data-index', '3'); }); test('renders multiple nested descendants contexts', () => { const { queryByText } = render( Apple Banana Peppers Anaheim Habanero , ); const apple = queryByText('Apple'); const banana = queryByText('Banana'); const peppers = queryByText('Peppers'); const anaheim = queryByText('Anaheim'); const habanero = queryByText('Habanero'); expect(apple).toHaveAttribute('data-index', '0'); expect(banana).toHaveAttribute('data-index', '1'); expect(peppers).toHaveAttribute('data-index', '2'); expect(anaheim).toHaveAttribute('data-index', '0'); expect(habanero).toHaveAttribute('data-index', '1'); }); test('registers descendants rendered inside an iframe', () => { const iframe = document.createElement('iframe'); document.body.appendChild(iframe); const iframeDocument = iframe.contentDocument!; const container = iframeDocument.createElement('div'); iframeDocument.body.appendChild(container); const { queryByText } = render( Apple Banana Carrot , { container }, ); expect(queryByText('Apple')).toHaveAttribute('data-index', '0'); expect(queryByText('Banana')).toHaveAttribute('data-index', '1'); expect(queryByText('Carrot')).toHaveAttribute('data-index', '2'); document.body.removeChild(iframe); }); }); describe('useInitDescendants', () => { test('descendants register when wrapped in a fragment', () => { const { hook } = renderDescendantsTestContext( <> Apple Banana , ); const current = hook.result.current; const appleDescendant = current!.descendants?.[0]; expect(appleDescendant).toBeDefined(); }); test('descendants register when individually wrapped in an HTML element', () => { const { hook } = renderDescendantsTestContext( <>
Apple
Banana , ); const current = hook.result.current; const appleDescendant = current!.descendants?.[0]; expect(appleDescendant).toBeDefined(); }); test('elements not rendered with `useDescendants` are not registered', () => { const { hook, renderResult } = renderDescendantsTestContext( <>
Fruit
Apple Banana , ); const current = hook.result.current; const appleElement = renderResult.getByTestId('apple'); const appleDescendant = current!.descendants?.[0]; expect(appleDescendant).toBeDefined(); expect(appleDescendant.element).toEqual(appleElement); }); test('descendants object has access to index', () => { const { hook } = renderDescendantsTestContext( <> Apple Banana , ); const current = hook.result.current; const appleDescendant = current!.descendants?.[0]; expect(appleDescendant.index).toEqual(0); }); test('descendants object has access to child props', () => { const { hook } = renderDescendantsTestContext( <> Apple Banana , ); const current = hook.result.current; const appleDescendant = current!.descendants?.[0]; expect(appleDescendant.props?.group).toEqual('fruit'); }); test('descendants object index updates after rerender', () => { const { hook, rerender } = renderDescendantsTestContext( <> Apple Banana Carrot , ); rerender( <> Banana Carrot Apple , ); const current = hook.result.current; const appleDescendant = current!.descendants?.[2]; expect(appleDescendant.element).toHaveTextContent('Apple'); expect(appleDescendant.index).toEqual(2); }); test('inserting an element updates the index of following elements', () => { const { hook, rerender } = renderDescendantsTestContext( <> Apple Carrot , ); rerender( <> Apple Banana Carrot , ); const current = hook.result.current; const descendantCount = current!.descendants?.length ?? 0; for (let i = 0; i < descendantCount; i++) { const descendant = current!.descendants?.[i]; expect(descendant.index).toEqual(i); } }); test('Accessing descendants from a callback are not stale', async () => { const logDescendants = jest.fn(); const Parent = ({ children, open, ...rest }: ComponentProps<'div'> & { open: boolean }) => { const { Provider, getDescendants } = useInitDescendants( TestDescendantContext, ); const handleTransition = () => { logDescendants(getDescendants()); }; return (
{children}
); }; const renderResult = render( , ); renderResult.rerender( , ); await waitFor(() => { const child = renderResult.getByTestId('child'); const expectedDescendants = expect.arrayContaining([ expect.objectContaining({ element: child }), ]); expect(logDescendants).toHaveBeenCalledWith(expectedDescendants); }); }); }); });