useButtonTracking.spec.tsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import {LocationFixture} from 'sentry-fixture/locationFixture';
  2. import {OrganizationFixture} from 'sentry-fixture/organization';
  3. import {RouterFixture} from 'sentry-fixture/routerFixture';
  4. import {ProjectFixture} from 'getsentry-test/fixtures/project';
  5. import {renderHook} from 'sentry-test/reactTestingLibrary';
  6. import type {ButtonProps} from 'sentry/components/core/button';
  7. import {OrganizationContext} from 'sentry/views/organizationContext';
  8. import {TestRouteContext} from 'sentry/views/routeContext';
  9. import useButtonTracking from 'getsentry/hooks/useButtonTracking';
  10. import rawTrackAnalyticsEvent from 'getsentry/utils/rawTrackAnalyticsEvent';
  11. jest.mock('getsentry/utils/rawTrackAnalyticsEvent');
  12. describe('buttonTracking', function () {
  13. const organization = OrganizationFixture();
  14. const project = ProjectFixture({organization});
  15. const router = {
  16. location: LocationFixture({
  17. pathname: `/settings/${organization.slug}/${project.slug}/`,
  18. }),
  19. params: {orgId: organization.slug},
  20. routes: [
  21. {path: '/'},
  22. {path: '/settings/'},
  23. {path: ':orgId/'},
  24. {path: 'projects/:projectId/'},
  25. ],
  26. router: RouterFixture(),
  27. };
  28. const wrapper = ({children}: ButtonProps) => (
  29. <OrganizationContext.Provider value={organization}>
  30. <TestRouteContext.Provider value={router}>{children}</TestRouteContext.Provider>
  31. </OrganizationContext.Provider>
  32. );
  33. afterEach(function () {
  34. (rawTrackAnalyticsEvent as jest.Mock).mockClear();
  35. });
  36. it('calls rawTrackAnalyticsEvent with default values', function () {
  37. const {result} = renderHook(useButtonTracking, {
  38. initialProps: {'aria-label': 'Create Alert'},
  39. wrapper,
  40. });
  41. result.current();
  42. expect(rawTrackAnalyticsEvent).toHaveBeenCalledWith({
  43. eventName: null,
  44. eventKey: 'button_click.settings.:org_id.projects.:project_id',
  45. organization: expect.objectContaining(organization),
  46. parameterized_path: 'settings.:org_id.projects.:project_id',
  47. text: 'Create Alert',
  48. });
  49. expect(rawTrackAnalyticsEvent).toHaveBeenCalledTimes(1);
  50. });
  51. it('calls rawTrackAnalyticsEvent with data', function () {
  52. const {result} = renderHook(useButtonTracking, {
  53. initialProps: {
  54. 'aria-label': 'Create Alert',
  55. analyticsEventKey: 'settings.create_alert',
  56. analyticsEventName: 'Settings: Create Alert',
  57. analyticsParams: {priority: 'primary', href: 'sentry.io/settings/create_alert'},
  58. },
  59. wrapper,
  60. });
  61. result.current();
  62. expect(rawTrackAnalyticsEvent).toHaveBeenCalledWith({
  63. eventName: 'Settings: Create Alert',
  64. eventKey: 'settings.create_alert',
  65. organization: expect.objectContaining(organization),
  66. parameterized_path: 'settings.:org_id.projects.:project_id',
  67. text: 'Create Alert',
  68. priority: 'primary',
  69. href: 'sentry.io/settings/create_alert',
  70. });
  71. expect(rawTrackAnalyticsEvent).toHaveBeenCalledTimes(1);
  72. });
  73. it('calls rawTrackAnalyticsEvent with new event names', function () {
  74. const {result} = renderHook(useButtonTracking, {
  75. initialProps: {
  76. 'aria-label': 'Create Alert',
  77. analyticsEventKey: 'settings.create_alert',
  78. analyticsEventName: 'Settings: Create Alert',
  79. },
  80. wrapper,
  81. });
  82. result.current();
  83. expect(rawTrackAnalyticsEvent).toHaveBeenCalledWith({
  84. eventName: 'Settings: Create Alert',
  85. eventKey: 'settings.create_alert',
  86. organization: expect.objectContaining(organization),
  87. parameterized_path: 'settings.:org_id.projects.:project_id',
  88. text: 'Create Alert',
  89. });
  90. expect(rawTrackAnalyticsEvent).toHaveBeenCalledTimes(1);
  91. });
  92. });