eventCauseEmpty.spec.jsx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. import React from 'react';
  2. import moment from 'moment';
  3. import {mount} from 'enzyme';
  4. import EventCauseEmpty from 'app/components/events/eventCauseEmpty';
  5. import {trackAdhocEvent, trackAnalyticsEvent} from 'app/utils/analytics';
  6. jest.mock('app/utils/analytics');
  7. describe('EventCauseEmpty', function() {
  8. let putMock;
  9. const routerContext = TestStubs.routerContext();
  10. const organization = TestStubs.Organization();
  11. const project = TestStubs.Project({platform: 'javascript'});
  12. beforeEach(function() {
  13. MockApiClient.clearMockResponses();
  14. MockApiClient.addMockResponse({
  15. url: '/projects/org-slug/project-slug/releases/completion/',
  16. body: [{step: 'commit', complete: false}],
  17. });
  18. MockApiClient.addMockResponse({
  19. method: 'GET',
  20. url: '/promptsactivity/',
  21. body: {},
  22. });
  23. putMock = MockApiClient.addMockResponse({
  24. method: 'PUT',
  25. url: '/promptsactivity/',
  26. });
  27. });
  28. it('renders', async function() {
  29. const wrapper = mount(
  30. <EventCauseEmpty organization={organization} project={project} />,
  31. routerContext
  32. );
  33. await tick();
  34. wrapper.update();
  35. expect(wrapper.find('ExampleCommitPanel').exists()).toBe(true);
  36. expect(trackAdhocEvent).toHaveBeenCalledWith({
  37. eventKey: 'event_cause.viewed',
  38. org_id: parseInt(organization.id, 10),
  39. project_id: parseInt(project.id, 10),
  40. platform: project.platform,
  41. });
  42. });
  43. it('can be snoozed', async function() {
  44. const wrapper = mount(
  45. <EventCauseEmpty organization={organization} project={project} />,
  46. routerContext
  47. );
  48. await tick();
  49. wrapper.update();
  50. wrapper
  51. .find('[data-test-id="snoozed"]')
  52. .first()
  53. .simulate('click');
  54. expect(putMock).toHaveBeenCalledWith(
  55. '/promptsactivity/',
  56. expect.objectContaining({
  57. method: 'PUT',
  58. data: {
  59. organization_id: organization.id,
  60. project_id: project.id,
  61. feature: 'suspect_commits',
  62. status: 'snoozed',
  63. },
  64. })
  65. );
  66. expect(wrapper.find('ExampleCommitPanel').exists()).toBe(false);
  67. expect(trackAnalyticsEvent).toHaveBeenCalledWith({
  68. eventKey: 'event_cause.snoozed',
  69. eventName: 'Event Cause Snoozed',
  70. organization_id: parseInt(organization.id, 10),
  71. project_id: parseInt(project.id, 10),
  72. platform: project.platform,
  73. });
  74. });
  75. it('does not render when snoozed', async function() {
  76. const snoozed_ts = moment()
  77. .subtract(1, 'day')
  78. .unix();
  79. MockApiClient.addMockResponse({
  80. method: 'GET',
  81. url: '/promptsactivity/',
  82. body: {data: {snoozed_ts}},
  83. });
  84. const wrapper = mount(
  85. <EventCauseEmpty organization={organization} project={project} />,
  86. routerContext
  87. );
  88. await tick();
  89. wrapper.update();
  90. expect(wrapper.find('ExampleCommitPanel').exists()).toBe(false);
  91. });
  92. it('renders when snoozed more than 7 days ago', async function() {
  93. const snoozed_ts = moment()
  94. .subtract(9, 'day')
  95. .unix();
  96. MockApiClient.addMockResponse({
  97. method: 'GET',
  98. url: '/promptsactivity/',
  99. body: {data: {snoozed_ts}},
  100. });
  101. const wrapper = mount(
  102. <EventCauseEmpty organization={organization} project={project} />,
  103. routerContext
  104. );
  105. await tick();
  106. wrapper.update();
  107. expect(wrapper.find('ExampleCommitPanel').exists()).toBe(true);
  108. });
  109. it('can be dismissed', async function() {
  110. const wrapper = mount(
  111. <EventCauseEmpty organization={organization} project={project} />,
  112. routerContext
  113. );
  114. await tick();
  115. wrapper.update();
  116. wrapper
  117. .find('[data-test-id="dismissed"]')
  118. .first()
  119. .simulate('click');
  120. expect(putMock).toHaveBeenCalledWith(
  121. '/promptsactivity/',
  122. expect.objectContaining({
  123. method: 'PUT',
  124. data: {
  125. organization_id: organization.id,
  126. project_id: project.id,
  127. feature: 'suspect_commits',
  128. status: 'dismissed',
  129. },
  130. })
  131. );
  132. expect(wrapper.find('ExampleCommitPanel').exists()).toBe(false);
  133. expect(trackAnalyticsEvent).toHaveBeenCalledWith({
  134. eventKey: 'event_cause.dismissed',
  135. eventName: 'Event Cause Dismissed',
  136. organization_id: parseInt(organization.id, 10),
  137. project_id: parseInt(project.id, 10),
  138. platform: project.platform,
  139. });
  140. });
  141. it('does not render when dismissed', async function() {
  142. MockApiClient.addMockResponse({
  143. method: 'GET',
  144. url: '/promptsactivity/',
  145. body: {data: {dismissed_ts: moment().unix()}},
  146. });
  147. const wrapper = mount(
  148. <EventCauseEmpty organization={organization} project={project} />,
  149. routerContext
  150. );
  151. await tick();
  152. wrapper.update();
  153. expect(wrapper.find('ExampleCommitPanel').exists()).toBe(false);
  154. });
  155. it('can capture analytics on docs click', async function() {
  156. const wrapper = mount(
  157. <EventCauseEmpty organization={organization} project={project} />,
  158. routerContext
  159. );
  160. await tick();
  161. wrapper.update();
  162. wrapper
  163. .find('[data-test-id="read-the-docs"]')
  164. .first()
  165. .simulate('click');
  166. expect(trackAnalyticsEvent).toHaveBeenCalledWith({
  167. eventKey: 'event_cause.docs_clicked',
  168. eventName: 'Event Cause Docs Clicked',
  169. organization_id: parseInt(organization.id, 10),
  170. project_id: parseInt(project.id, 10),
  171. platform: project.platform,
  172. });
  173. });
  174. });