eventGraph.spec.tsx 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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', 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. });
  116. it('updates query from location param change', async function () {
  117. const [tagKey, tagValue] = ['user.email', 'leander.rodrigues@sentry.io'];
  118. const locationQuery = {
  119. query: {
  120. query: `${tagKey}:${tagValue}`,
  121. },
  122. };
  123. const router = RouterFixture({
  124. location: LocationFixture(locationQuery),
  125. });
  126. render(<EventDetailsHeader {...defaultProps} />, {organization, router});
  127. expect(await screen.findByTestId('event-graph-loading')).not.toBeInTheDocument();
  128. expect(mockEventStats).toHaveBeenCalledWith(
  129. '/organizations/org-slug/events-stats/',
  130. expect.objectContaining({
  131. query: expect.objectContaining({
  132. query: [persistantQuery, locationQuery.query.query].join(' '),
  133. }),
  134. })
  135. );
  136. });
  137. it('allows filtering by date', async function () {
  138. render(<EventDetailsHeader {...defaultProps} />, {organization});
  139. expect(await screen.findByTestId('event-graph-loading')).not.toBeInTheDocument();
  140. await userEvent.click(screen.getByRole('button', {name: '14D'}));
  141. await userEvent.click(await screen.findByRole('option', {name: 'Last 7 days'}));
  142. expect(mockEventStats).toHaveBeenCalledWith(
  143. '/organizations/org-slug/events-stats/',
  144. expect.objectContaining({
  145. query: expect.objectContaining({
  146. statsPeriod: '7d',
  147. }),
  148. })
  149. );
  150. });
  151. it('displays error messages from bad queries', async function () {
  152. const errorMessage = 'wrong, try again';
  153. const mockStats = MockApiClient.addMockResponse({
  154. url: `/organizations/${organization.slug}/events-stats/`,
  155. body: {detail: errorMessage},
  156. method: 'GET',
  157. statusCode: 400,
  158. });
  159. render(<EventDetailsHeader {...defaultProps} />, {organization});
  160. await screen.findByRole('button', {name: '14D'});
  161. expect(mockStats).toHaveBeenCalled();
  162. expect(screen.getByText(RegExp(errorMessage))).toBeInTheDocument();
  163. // Omit the graph
  164. expect(screen.queryByRole('figure')).not.toBeInTheDocument();
  165. });
  166. });