import {render, screen} from 'sentry-test/reactTestingLibrary'; import Hook from 'sentry/components/hook'; import HookStore from 'sentry/stores/hookStore'; const HookWrapper = props => (
{props.children} {JSON.stringify(props?.organization ?? {}, null, 2)}
); describe('Hook', function () { afterEach(function () { HookStore.teardown(); HookStore.init(); }); it('renders component from a hook', function () { HookStore.add('footer', ({organization} = {}) => ( {organization.slug} )); render(
); expect(HookStore.hooks.footer).toHaveLength(1); expect(screen.getByTestId('hook-wrapper')).toBeInTheDocument(); expect(screen.getByTestId('hook-wrapper')).toHaveTextContent('org-slug'); }); it('renders an invalid hook', function () { HookStore.add('footer', ({organization} = {}) => ( {organization.slug} )); render(
invalid
); expect(screen.queryByText('org-slug')).not.toBeInTheDocument(); expect(screen.getByText('invalid')).toBeInTheDocument(); }); it('can re-render when hooks get after initial render', function () { HookStore.add('footer', ({organization} = {}) => ( Old Hook )); const {rerender} = render( ); expect(screen.getByTestId('hook-wrapper')).toBeInTheDocument(); HookStore.add('footer', () => ( New Hook )); rerender(); expect(screen.getAllByTestId('hook-wrapper')).toHaveLength(2); expect(screen.getByText(/New Hook/)).toBeInTheDocument(); expect(screen.getByText(/Old Hook/)).toBeInTheDocument(); }); it('can use children as a render prop', function () { let idx = 0; render( {({hooks}) => hooks.map((hook, i) => ( {hook} {`hook: ${++idx}`} )) } ); HookStore.add('footer', () => ( First Hook )); HookStore.add('footer', () => ( Second Hook )); for (let i = 0; i < idx; i++) { expect(screen.getByText(`hook: ${idx}`)).toBeInTheDocument(); } // Has 2 Wrappers from store, and each is wrapped by another Wrapper expect(screen.getAllByTestId('hook-wrapper')).toHaveLength(4); }); });