teamMisery.spec.tsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import range from 'lodash/range';
  2. import {Organization} from 'sentry-fixture/organization';
  3. import {initializeOrg} from 'sentry-test/initializeOrg';
  4. import {
  5. render,
  6. screen,
  7. userEvent,
  8. waitForElementToBeRemoved,
  9. } from 'sentry-test/reactTestingLibrary';
  10. import TeamMisery from 'sentry/views/organizationStats/teamInsights/teamMisery';
  11. describe('TeamMisery', () => {
  12. const {routerProps} = initializeOrg();
  13. it('should render misery from projects and expand hidden items', async () => {
  14. const project = TestStubs.Project();
  15. const meta = {
  16. fields: {
  17. transaction: 'string',
  18. project: 'string',
  19. tpm: 'number',
  20. 'count_unique(user)': 'number',
  21. 'count_miserable(user)': 'number',
  22. 'user_misery()': 'number',
  23. },
  24. };
  25. const extraData = {
  26. project: project.slug,
  27. tpm: 30,
  28. count_unique_user: 1000,
  29. count_miserable_user: 122,
  30. project_threshold_config: ['duration', 300],
  31. };
  32. const noChangeItems = 10;
  33. const noChange = range(0, noChangeItems).map(x => ({
  34. transaction: `/apple/${x}`,
  35. 'user_misery()': 0.1,
  36. ...extraData,
  37. }));
  38. const weekMisery = MockApiClient.addMockResponse({
  39. url: `/organizations/org-slug/events/`,
  40. body: {
  41. meta,
  42. data: [
  43. {
  44. transaction: '/apple/cart',
  45. 'user_misery()': 0.5,
  46. ...extraData,
  47. },
  48. {
  49. transaction: '/apple/checkout',
  50. 'user_misery()': 0.1,
  51. ...extraData,
  52. },
  53. ...noChange,
  54. ],
  55. },
  56. match: [MockApiClient.matchQuery({statsPeriod: '7d'})],
  57. });
  58. const periodMisery = MockApiClient.addMockResponse({
  59. url: `/organizations/org-slug/events/`,
  60. body: {
  61. meta,
  62. data: [
  63. {
  64. transaction: '/apple/cart',
  65. 'user_misery()': 0.25,
  66. ...extraData,
  67. },
  68. {
  69. transaction: '/apple/checkout',
  70. 'user_misery()': 0.2,
  71. ...extraData,
  72. },
  73. ...noChange,
  74. ],
  75. },
  76. match: [MockApiClient.matchQuery({statsPeriod: '8w'})],
  77. });
  78. render(
  79. <TeamMisery
  80. organization={Organization()}
  81. projects={[project]}
  82. period="8w"
  83. teamId="0"
  84. {...routerProps}
  85. />
  86. );
  87. await waitForElementToBeRemoved(() => screen.queryByTestId('loading-indicator'));
  88. expect(weekMisery).toHaveBeenCalledTimes(1);
  89. expect(periodMisery).toHaveBeenCalledTimes(1);
  90. // Should have 8 items, the rest are collapsed.
  91. expect(screen.getAllByText(project.slug)).toHaveLength(5);
  92. expect(screen.getByText('10% better')).toBeInTheDocument();
  93. expect(screen.getByText('25% worse')).toBeInTheDocument();
  94. expect(screen.getAllByText('0% change')).toHaveLength(3);
  95. expect(screen.getByText('Show 7 More')).toBeInTheDocument();
  96. await userEvent.click(screen.getByText('Show 7 More'));
  97. expect(screen.getAllByText('0% change')).toHaveLength(noChangeItems);
  98. });
  99. it('should render empty state', () => {
  100. render(
  101. <TeamMisery
  102. organization={Organization()}
  103. projects={[]}
  104. period="8w"
  105. teamId="0"
  106. {...routerProps}
  107. />
  108. );
  109. expect(
  110. screen.getByText('No key transactions starred by this team')
  111. ).toBeInTheDocument();
  112. });
  113. it('should render empty state on error', async () => {
  114. MockApiClient.addMockResponse({
  115. url: `/organizations/org-slug/events/`,
  116. statusCode: 500,
  117. body: {},
  118. });
  119. render(
  120. <TeamMisery
  121. organization={Organization()}
  122. projects={[TestStubs.Project()]}
  123. period="8w"
  124. teamId="0"
  125. {...routerProps}
  126. />
  127. );
  128. await waitForElementToBeRemoved(screen.queryByTestId('loading-indicator'));
  129. expect(screen.getByText('There was an error loading data.')).toBeInTheDocument();
  130. });
  131. });