hook.spec.jsx 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import {mountWithTheme} from 'sentry-test/enzyme';
  2. import Hook from 'app/components/hook';
  3. import HookStore from 'app/stores/hookStore';
  4. describe('Hook', function () {
  5. const Wrapper = function Wrapper(props) {
  6. return <div {...props} />;
  7. };
  8. const routerContext = TestStubs.routerContext();
  9. beforeEach(function () {
  10. HookStore.add('footer', ({organization} = {}) => (
  11. <Wrapper key="initial" organization={organization}>
  12. {organization.slug}
  13. </Wrapper>
  14. ));
  15. });
  16. afterEach(function () {
  17. // Clear HookStore
  18. HookStore.init();
  19. });
  20. it('renders component from a hook', function () {
  21. const wrapper = mountWithTheme(
  22. <div>
  23. <Hook name="footer" organization={TestStubs.Organization()} />
  24. </div>,
  25. routerContext
  26. );
  27. expect(HookStore.hooks.footer).toHaveLength(1);
  28. expect(wrapper.find('Wrapper')).toHaveLength(1);
  29. expect(wrapper.find('Wrapper').prop('organization').slug).toBe('org-slug');
  30. });
  31. it('renders an invalid hook', function () {
  32. const wrapper = mountWithTheme(
  33. <div>
  34. <Hook name="invalid-hook" organization={TestStubs.Organization()} />
  35. </div>,
  36. routerContext
  37. );
  38. expect(wrapper.find('Wrapper')).toHaveLength(0);
  39. expect(wrapper.find('div')).toHaveLength(1);
  40. });
  41. it('can re-render when hooks get after initial render', function () {
  42. const wrapper = mountWithTheme(
  43. <div>
  44. <Hook name="footer" organization={TestStubs.Organization()} />
  45. </div>,
  46. routerContext
  47. );
  48. expect(wrapper.find('Wrapper')).toHaveLength(1);
  49. HookStore.add('footer', () => (
  50. <Wrapper key="new" organization={null}>
  51. New Hook
  52. </Wrapper>
  53. ));
  54. wrapper.update();
  55. expect(HookStore.hooks.footer).toHaveLength(2);
  56. expect(wrapper.find('Wrapper')).toHaveLength(2);
  57. expect(wrapper.find('Wrapper').at(1).prop('organization')).toEqual(null);
  58. });
  59. it('can use children as a render prop', function () {
  60. const wrapper = mountWithTheme(
  61. <div>
  62. <Hook name="footer" organization={TestStubs.Organization()}>
  63. {({hooks}) => hooks.map((hook, i) => <Wrapper key={i}>{hook}</Wrapper>)}
  64. </Hook>
  65. </div>,
  66. routerContext
  67. );
  68. HookStore.add('footer', () => (
  69. <Wrapper key="new" organization={null}>
  70. New Hook
  71. </Wrapper>
  72. ));
  73. wrapper.update();
  74. // Has 2 Wrappers from store, and each is wrapped by another Wrapper
  75. expect(wrapper.find('Wrapper')).toHaveLength(4);
  76. });
  77. });