import '@testing-library/jest-dom' import { fireEvent, render, screen } from '@testing-library/react' import React from 'react' import tunnel from '../src' describe('tunnelrat', () => { it('transports the children of In into Out', () => { const t = tunnel() const Outlet = () => ( ) const Inlets = () => (
  • One
  • ) const { container } = render( <> ) expect(container).toMatchInlineSnapshot(`
    • One
    `) }) it('can handle multiple children', () => { const t = tunnel() const Outlet = () => (
    ) const Inlets = () => (
  • One
  • Two
  • ) const { container } = render( <> ) expect(container).toMatchInlineSnapshot(`
    • One
    • Two
    `) }) it('retains the expected order of multiple children after un- and remounts', () => { const t = tunnel() const Rat = ({ name }: { name: string }) => { const [visible, setVisible] = React.useState(true) return (
    {visible ? (
  • {name}
  • ) : null}
    ) } const Outlet = () => (
    ) const Inlets = () => (
    ) const { container } = render( <> ) expect(container).toMatchInlineSnapshot(`
    • One
    • Two
    • Three
    `) /* Remove the middle rat */ fireEvent.click(screen.getByText('Toggle Two')) expect(container).toMatchInlineSnapshot(`
    • One
    • Three
    `) /* Re-add it */ fireEvent.click(screen.getByText('Toggle Two')) /* The "Two" rat gets re-added, and at the top of the list. */ expect(container).toMatchInlineSnapshot(`
    • One
    • Two
    • Three
    `) }) })