hook.spec.jsx 2.5 KB

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