eventGraph.spec.tsx 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. import {EventFixture} from 'sentry-fixture/event';
  2. import {EventsStatsFixture} from 'sentry-fixture/events';
  3. import {GroupFixture} from 'sentry-fixture/group';
  4. import {LocationFixture} from 'sentry-fixture/locationFixture';
  5. import {OrganizationFixture} from 'sentry-fixture/organization';
  6. import {ProjectFixture} from 'sentry-fixture/project';
  7. import {RouterFixture} from 'sentry-fixture/routerFixture';
  8. import {TagsFixture} from 'sentry-fixture/tags';
  9. import {render, screen, userEvent} from 'sentry-test/reactTestingLibrary';
  10. import PageFiltersStore from 'sentry/stores/pageFiltersStore';
  11. import ProjectsStore from 'sentry/stores/projectsStore';
  12. import {EventGraph} from 'sentry/views/issueDetails/streamline/eventGraph';
  13. import {EventDetailsHeader} from './eventDetailsHeader';
  14. describe('EventGraph', () => {
  15. const organization = OrganizationFixture();
  16. const project = ProjectFixture({
  17. environments: ['production', 'staging', 'development'],
  18. });
  19. const group = GroupFixture();
  20. const event = EventFixture({id: 'event-id'});
  21. const persistantQuery = `issue:${group.shortId}`;
  22. const defaultProps = {group, event, project};
  23. let mockEventStats: jest.Mock;
  24. beforeEach(() => {
  25. MockApiClient.clearMockResponses();
  26. MockApiClient.addMockResponse({
  27. url: '/organizations/org-slug/flags/logs/',
  28. body: {data: []},
  29. });
  30. MockApiClient.addMockResponse({
  31. url: `/organizations/${organization.slug}/issues/${group.id}/tags/`,
  32. body: TagsFixture(),
  33. method: 'GET',
  34. });
  35. MockApiClient.addMockResponse({
  36. url: `/organizations/${organization.slug}/releases/stats/`,
  37. body: [],
  38. });
  39. PageFiltersStore.init();
  40. PageFiltersStore.onInitializeUrlState(
  41. {
  42. projects: [],
  43. environments: [],
  44. datetime: {start: null, end: null, period: '14d', utc: null},
  45. },
  46. new Set(['environments'])
  47. );
  48. ProjectsStore.loadInitialData([project]);
  49. mockEventStats = MockApiClient.addMockResponse({
  50. url: `/organizations/${organization.slug}/events-stats/`,
  51. body: {'count()': EventsStatsFixture(), 'count_unique(user)': EventsStatsFixture()},
  52. method: 'GET',
  53. });
  54. MockApiClient.addMockResponse({
  55. url: `/organizations/${organization.slug}/events/`,
  56. body: {data: [{'count_unique(user)': 21}]},
  57. });
  58. });
  59. it('displays allows toggling data sets', async function () {
  60. render(<EventDetailsHeader {...defaultProps} />, {organization});
  61. expect(await screen.findByTestId('event-graph-loading')).not.toBeInTheDocument();
  62. const eventsToggle = screen.getByRole('button', {
  63. name: 'Toggle graph series - Events',
  64. });
  65. const usersToggle = screen.getByRole('button', {name: 'Toggle graph series - Users'});
  66. expect(eventsToggle).toHaveTextContent('444');
  67. expect(usersToggle).toHaveTextContent('21');
  68. // Defaults to events graph
  69. expect(eventsToggle).toBeDisabled();
  70. expect(usersToggle).toBeEnabled();
  71. // Switch to users graph
  72. await userEvent.click(usersToggle);
  73. expect(eventsToggle).toBeEnabled();
  74. expect(usersToggle).toBeDisabled();
  75. // Another click should do nothing
  76. await userEvent.click(usersToggle);
  77. expect(eventsToggle).toBeEnabled();
  78. expect(usersToggle).toBeDisabled();
  79. // Switch back to events
  80. await userEvent.click(eventsToggle);
  81. expect(eventsToggle).toBeDisabled();
  82. expect(usersToggle).toBeEnabled();
  83. });
  84. it('renders the graph using a discover event stats query', async function () {
  85. render(<EventGraph {...defaultProps} />, {organization});
  86. expect(await screen.findByTestId('event-graph-loading')).not.toBeInTheDocument();
  87. expect(mockEventStats).toHaveBeenCalledWith(
  88. '/organizations/org-slug/events-stats/',
  89. expect.objectContaining({
  90. query: {
  91. dataset: 'errors',
  92. environment: [],
  93. field: expect.anything(),
  94. partial: 1,
  95. interval: '4h',
  96. per_page: 50,
  97. project: [project.id],
  98. query: persistantQuery,
  99. referrer: 'issue_details.streamline_graph',
  100. statsPeriod: '14d',
  101. yAxis: ['count()', 'count_unique(user)'],
  102. },
  103. })
  104. );
  105. });
  106. it('allows filtering by environment, and shows unfiltered stats', async function () {
  107. render(<EventDetailsHeader {...defaultProps} />, {organization});
  108. expect(await screen.findByTestId('event-graph-loading')).not.toBeInTheDocument();
  109. await userEvent.click(screen.getByRole('button', {name: 'All Envs'}));
  110. await userEvent.click(screen.getByRole('row', {name: 'production'}));
  111. expect(mockEventStats).toHaveBeenCalledWith(
  112. '/organizations/org-slug/events-stats/',
  113. expect.objectContaining({
  114. query: expect.objectContaining({
  115. environment: ['production'],
  116. }),
  117. })
  118. );
  119. // Also makes request without environment filter
  120. expect(mockEventStats).toHaveBeenCalledWith(
  121. '/organizations/org-slug/events-stats/',
  122. expect.objectContaining({
  123. query: expect.objectContaining({
  124. environment: [],
  125. }),
  126. })
  127. );
  128. });
  129. it('updates query from location param change', async function () {
  130. const [tagKey, tagValue] = ['user.email', 'leander.rodrigues@sentry.io'];
  131. const locationQuery = {
  132. query: {
  133. query: `${tagKey}:${tagValue}`,
  134. },
  135. };
  136. const router = RouterFixture({
  137. location: LocationFixture(locationQuery),
  138. });
  139. render(<EventDetailsHeader {...defaultProps} />, {organization, router});
  140. expect(await screen.findByTestId('event-graph-loading')).not.toBeInTheDocument();
  141. expect(mockEventStats).toHaveBeenCalledWith(
  142. '/organizations/org-slug/events-stats/',
  143. expect.objectContaining({
  144. query: expect.objectContaining({
  145. query: [persistantQuery, locationQuery.query.query].join(' '),
  146. }),
  147. })
  148. );
  149. // Also makes request without tag filter
  150. expect(mockEventStats).toHaveBeenCalledWith(
  151. '/organizations/org-slug/events-stats/',
  152. expect.objectContaining({
  153. query: expect.objectContaining({
  154. query: persistantQuery,
  155. }),
  156. })
  157. );
  158. });
  159. it('allows filtering by date', async function () {
  160. render(<EventDetailsHeader {...defaultProps} />, {organization});
  161. expect(await screen.findByTestId('event-graph-loading')).not.toBeInTheDocument();
  162. await userEvent.click(screen.getByRole('button', {name: '14D'}));
  163. await userEvent.click(await screen.findByRole('option', {name: 'Last 7 days'}));
  164. expect(mockEventStats).toHaveBeenCalledWith(
  165. '/organizations/org-slug/events-stats/',
  166. expect.objectContaining({
  167. query: expect.objectContaining({
  168. statsPeriod: '7d',
  169. }),
  170. })
  171. );
  172. });
  173. it('displays error messages from bad queries', async function () {
  174. const errorMessage = 'wrong, try again';
  175. const mockStats = MockApiClient.addMockResponse({
  176. url: `/organizations/${organization.slug}/events-stats/`,
  177. body: {detail: errorMessage},
  178. method: 'GET',
  179. statusCode: 400,
  180. });
  181. render(<EventDetailsHeader {...defaultProps} />, {organization});
  182. await screen.findByRole('button', {name: '14D'});
  183. expect(mockStats).toHaveBeenCalled();
  184. expect(screen.getByText(RegExp(errorMessage))).toBeInTheDocument();
  185. // Omit the graph
  186. expect(screen.queryByRole('figure')).not.toBeInTheDocument();
  187. });
  188. });