eventGraph.spec.tsx 7.2 KB

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