projectCharts.spec.tsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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', () => {
  53. renderProjectCharts(['anr-rate'], 'android');
  54. userEvent.click(screen.getByRole('button', {name: 'Display Crash Free Sessions'}));
  55. expect(screen.getByText('Foreground ANR Rate')).toBeInTheDocument();
  56. expect(screen.getByText('ANR Rate')).toBeInTheDocument();
  57. });
  58. it('does not render ANR options for non-android platforms', () => {
  59. renderProjectCharts(['anr-rate'], 'python');
  60. userEvent.click(screen.getByRole('button', {name: 'Display Crash Free Sessions'}));
  61. expect(screen.queryByText('Foreground ANR Rate')).not.toBeInTheDocument();
  62. expect(screen.queryByText('ANR Rate')).not.toBeInTheDocument();
  63. });
  64. it('makes the right ANR sessions request', async () => {
  65. const responseBody = {
  66. query: '',
  67. intervals: [
  68. '2021-03-05T00:00:00Z',
  69. '2021-03-06T00:00:00Z',
  70. '2021-03-07T00:00:00Z',
  71. '2021-03-08T00:00:00Z',
  72. '2021-03-09T00:00:00Z',
  73. '2021-03-10T00:00:00Z',
  74. '2021-03-11T00:00:00Z',
  75. '2021-03-12T00:00:00Z',
  76. '2021-03-13T00:00:00Z',
  77. '2021-03-14T00:00:00Z',
  78. '2021-03-15T00:00:00Z',
  79. '2021-03-16T00:00:00Z',
  80. '2021-03-17T00:00:00Z',
  81. '2021-03-18T00:00:00Z',
  82. ],
  83. groups: [
  84. {
  85. by: {},
  86. totals: {
  87. 'anr_rate()': 492,
  88. 'count_unique(user)': 3,
  89. },
  90. series: {
  91. 'anr_rate()': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 490],
  92. 'count_unique(user)': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 1],
  93. },
  94. },
  95. ],
  96. };
  97. mockSessions = MockApiClient.addMockResponse({
  98. method: 'GET',
  99. url: '/organizations/org-slug/sessions/',
  100. body: responseBody,
  101. });
  102. renderProjectCharts(['anr-rate'], 'android', 'anr_rate');
  103. expect(screen.getByText('ANR Rate')).toBeInTheDocument();
  104. await waitFor(() =>
  105. expect(mockSessions).toHaveBeenCalledWith(
  106. '/organizations/org-slug/sessions/',
  107. expect.objectContaining({
  108. query: expect.objectContaining({
  109. field: ['anr_rate()', 'count_unique(user)'],
  110. }),
  111. })
  112. )
  113. );
  114. });
  115. });