projectCharts.spec.tsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import {initializeOrg} from 'sentry-test/initializeOrg';
  2. import {render, screen, userEvent, waitFor} from 'sentry-test/reactTestingLibrary';
  3. import ProjectCharts from 'sentry/views/projectDetail/projectCharts';
  4. function renderProjectCharts(
  5. features?: string[],
  6. platform?: string,
  7. chartDisplay?: string
  8. ) {
  9. const {organization, router, project} = initializeOrg({
  10. organization: TestStubs.Organization({features}),
  11. projects: [{platform}],
  12. router: {
  13. params: {orgId: 'org-slug', projectId: 'project-slug'},
  14. location: {
  15. pathname: '/organizations/org-slug/projects/project-slug/',
  16. query: {chart1: chartDisplay ?? 'crash_free'},
  17. },
  18. },
  19. } as Parameters<typeof initializeOrg>[0]);
  20. return render(
  21. <ProjectCharts
  22. chartId="chart1"
  23. chartIndex={0}
  24. hasSessions
  25. hasTransactions
  26. location={router.location}
  27. organization={organization}
  28. router={router}
  29. visibleCharts={['chart1', 'chart2']}
  30. project={project}
  31. />
  32. );
  33. }
  34. describe('ProjectDetail > ProjectCharts', () => {
  35. let mockSessions;
  36. beforeEach(() => {
  37. MockApiClient.addMockResponse({
  38. url: `/organizations/org-slug/releases/stats/`,
  39. body: [],
  40. });
  41. mockSessions = MockApiClient.addMockResponse({
  42. method: 'GET',
  43. url: '/organizations/org-slug/sessions/',
  44. body: TestStubs.SessionsField({
  45. field: `sum(session)`,
  46. }),
  47. });
  48. });
  49. afterEach(() => {
  50. jest.resetAllMocks();
  51. });
  52. it('renders ANR options', async () => {
  53. renderProjectCharts(['anr-rate'], 'android');
  54. await userEvent.click(
  55. screen.getByRole('button', {name: 'Display Crash Free Sessions'})
  56. );
  57. expect(screen.getByText('Foreground ANR Rate')).toBeInTheDocument();
  58. expect(screen.getByText('ANR Rate')).toBeInTheDocument();
  59. });
  60. it('does not render ANR options for non-android platforms', async () => {
  61. renderProjectCharts(['anr-rate'], 'python');
  62. await userEvent.click(
  63. screen.getByRole('button', {name: 'Display Crash Free Sessions'})
  64. );
  65. expect(screen.queryByText('Foreground ANR Rate')).not.toBeInTheDocument();
  66. expect(screen.queryByText('ANR Rate')).not.toBeInTheDocument();
  67. });
  68. it('makes the right ANR sessions request', async () => {
  69. const responseBody = {
  70. query: '',
  71. intervals: [
  72. '2021-03-05T00:00:00Z',
  73. '2021-03-06T00:00:00Z',
  74. '2021-03-07T00:00:00Z',
  75. '2021-03-08T00:00:00Z',
  76. '2021-03-09T00:00:00Z',
  77. '2021-03-10T00:00:00Z',
  78. '2021-03-11T00:00:00Z',
  79. '2021-03-12T00:00:00Z',
  80. '2021-03-13T00:00:00Z',
  81. '2021-03-14T00:00:00Z',
  82. '2021-03-15T00:00:00Z',
  83. '2021-03-16T00:00:00Z',
  84. '2021-03-17T00:00:00Z',
  85. '2021-03-18T00:00:00Z',
  86. ],
  87. groups: [
  88. {
  89. by: {},
  90. totals: {
  91. 'anr_rate()': 492,
  92. 'count_unique(user)': 3,
  93. },
  94. series: {
  95. 'anr_rate()': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 490],
  96. 'count_unique(user)': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 1],
  97. },
  98. },
  99. ],
  100. };
  101. mockSessions = MockApiClient.addMockResponse({
  102. method: 'GET',
  103. url: '/organizations/org-slug/sessions/',
  104. body: responseBody,
  105. });
  106. renderProjectCharts(['anr-rate'], 'android', 'anr_rate');
  107. expect(screen.getByText('ANR Rate')).toBeInTheDocument();
  108. await waitFor(() =>
  109. expect(mockSessions).toHaveBeenCalledWith(
  110. '/organizations/org-slug/sessions/',
  111. expect.objectContaining({
  112. query: expect.objectContaining({
  113. field: ['anr_rate()', 'count_unique(user)'],
  114. }),
  115. })
  116. )
  117. );
  118. });
  119. });