hook.spec.jsx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import {Organization} from 'fixtures/js-stubs/organization';
  2. import {render, screen} from 'sentry-test/reactTestingLibrary';
  3. import Hook from 'sentry/components/hook';
  4. import HookStore from 'sentry/stores/hookStore';
  5. const HookWrapper = props => (
  6. <div data-test-id="hook-wrapper">
  7. {props.children}
  8. <span>{JSON.stringify(props?.organization ?? {}, null, 2)}</span>
  9. </div>
  10. );
  11. describe('Hook', function () {
  12. afterEach(function () {
  13. HookStore.init();
  14. });
  15. it('renders component from a hook', function () {
  16. HookStore.add('footer', ({organization} = {}) => (
  17. <HookWrapper key={0} organization={organization}>
  18. {organization.slug}
  19. </HookWrapper>
  20. ));
  21. render(
  22. <div>
  23. <Hook name="footer" organization={Organization()} />
  24. </div>
  25. );
  26. expect(HookStore.hooks.footer).toHaveLength(1);
  27. expect(screen.getByTestId('hook-wrapper')).toBeInTheDocument();
  28. expect(screen.getByTestId('hook-wrapper')).toHaveTextContent('org-slug');
  29. });
  30. it('renders an invalid hook', function () {
  31. HookStore.add('footer', ({organization} = {}) => (
  32. <HookWrapper key={0} organization={organization}>
  33. {organization.slug}
  34. </HookWrapper>
  35. ));
  36. render(
  37. <div>
  38. <Hook name="invalid-hook" organization={Organization()} />
  39. invalid
  40. </div>
  41. );
  42. expect(screen.queryByText('org-slug')).not.toBeInTheDocument();
  43. expect(screen.getByText('invalid')).toBeInTheDocument();
  44. });
  45. it('can re-render when hooks get after initial render', function () {
  46. HookStore.add('footer', ({organization} = {}) => (
  47. <HookWrapper key={0} organization={organization}>
  48. Old Hook
  49. </HookWrapper>
  50. ));
  51. const {rerender} = render(<Hook name="footer" organization={Organization()} />);
  52. expect(screen.getByTestId('hook-wrapper')).toBeInTheDocument();
  53. HookStore.add('footer', () => (
  54. <HookWrapper key="new" organization={null}>
  55. New Hook
  56. </HookWrapper>
  57. ));
  58. rerender(<Hook name="footer" organization={Organization()} />);
  59. expect(screen.getAllByTestId('hook-wrapper')).toHaveLength(2);
  60. expect(screen.getByText(/New Hook/)).toBeInTheDocument();
  61. expect(screen.getByText(/Old Hook/)).toBeInTheDocument();
  62. });
  63. it('can use children as a render prop', function () {
  64. let idx = 0;
  65. render(
  66. <Hook name="footer" organization={Organization()}>
  67. {({hooks}) =>
  68. hooks.map((hook, i) => (
  69. <HookWrapper key={i}>
  70. {hook} {`hook: ${++idx}`}
  71. </HookWrapper>
  72. ))
  73. }
  74. </Hook>
  75. );
  76. HookStore.add('footer', () => (
  77. <HookWrapper key="new" organization={null}>
  78. First Hook
  79. </HookWrapper>
  80. ));
  81. HookStore.add('footer', () => (
  82. <HookWrapper key="new" organization={null}>
  83. Second Hook
  84. </HookWrapper>
  85. ));
  86. for (let i = 0; i < idx; i++) {
  87. expect(screen.getByText(`hook: ${idx}`)).toBeInTheDocument();
  88. }
  89. // Has 2 Wrappers from store, and each is wrapped by another Wrapper
  90. expect(screen.getAllByTestId('hook-wrapper')).toHaveLength(4);
  91. });
  92. });