hook.spec.jsx 2.6 KB

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