import {mountWithTheme, screen} from 'sentry-test/reactTestingLibrary'; import Hook from 'sentry/components/hook'; import HookStore from 'sentry/stores/hookStore'; describe('Hook', function () { const Wrapper = function Wrapper(props) { return
; }; beforeEach(function () { HookStore.add('footer', ({organization} = {}) => ( {organization.slug} )); }); afterEach(function () { // Clear HookStore HookStore.init(); }); it('renders component from a hook', function () { mountWithTheme(
); expect(HookStore.hooks.footer).toHaveLength(1); expect(screen.getByTestId('wrapper')).toBeInTheDocument(); expect(screen.getByTestId('wrapper')).toHaveTextContent('org-slug'); }); it('renders an invalid hook', function () { mountWithTheme(
invalid
); expect(screen.queryByText('org-slug')).not.toBeInTheDocument(); expect(screen.getByText('invalid')).toBeInTheDocument(); }); it('can re-render when hooks get after initial render', function () { mountWithTheme(
); expect(screen.getByTestId('wrapper')).toBeInTheDocument(); HookStore.add('footer', () => ( New Hook )); expect(HookStore.hooks.footer).toHaveLength(2); expect(screen.getAllByTestId('wrapper')).toHaveLength(2); expect(screen.getAllByTestId('wrapper')[1]).toHaveTextContent('New Hook'); }); it('can use children as a render prop', function () { mountWithTheme(
{({hooks}) => hooks.map((hook, i) => {hook})}
); HookStore.add('footer', () => ( New Hook )); // Has 2 Wrappers from store, and each is wrapped by another Wrapper expect(screen.getAllByTestId('wrapper')).toHaveLength(4); }); });