transactionThresholdButton.spec.tsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import {OrganizationFixture} from 'sentry-fixture/organization';
  2. import {ProjectFixture} from 'sentry-fixture/project';
  3. import {
  4. render,
  5. renderGlobalModal,
  6. screen,
  7. userEvent,
  8. waitFor,
  9. } from 'sentry-test/reactTestingLibrary';
  10. import ProjectsStore from 'sentry/stores/projectsStore';
  11. import type {Organization} from 'sentry/types/organization';
  12. import EventView from 'sentry/utils/discover/eventView';
  13. import TransactionThresholdButton from 'sentry/views/performance/transactionSummary/transactionThresholdButton';
  14. function renderComponent(
  15. eventView: EventView,
  16. organization: Organization,
  17. onChangeThreshold: () => void
  18. ) {
  19. return render(
  20. <TransactionThresholdButton
  21. eventView={eventView}
  22. organization={organization}
  23. transactionName="transaction/threshold"
  24. onChangeThreshold={onChangeThreshold}
  25. />
  26. );
  27. }
  28. describe('TransactionThresholdButton', function () {
  29. const organization = OrganizationFixture({features: ['performance-view']});
  30. const project = ProjectFixture();
  31. const eventView = new EventView({
  32. id: '1',
  33. name: 'my query',
  34. fields: [{field: 'count()'}],
  35. sorts: [{field: 'count', kind: 'desc'}],
  36. query: '',
  37. project: [parseInt(project.id, 10)],
  38. start: '2019-10-01T00:00:00',
  39. end: '2019-10-02T00:00:00',
  40. statsPeriod: '14d',
  41. environment: [],
  42. createdBy: undefined,
  43. display: '',
  44. team: ['myteams'],
  45. topEvents: undefined,
  46. });
  47. const onChangeThreshold = jest.fn();
  48. beforeEach(function () {
  49. MockApiClient.clearMockResponses();
  50. ProjectsStore.loadInitialData([project]);
  51. });
  52. it('renders element correctly', async function () {
  53. const getTransactionThresholdMock = MockApiClient.addMockResponse({
  54. url: '/organizations/org-slug/project-transaction-threshold-override/',
  55. method: 'GET',
  56. body: {
  57. threshold: '800',
  58. metric: 'lcp',
  59. },
  60. });
  61. const getProjectThresholdMock = MockApiClient.addMockResponse({
  62. url: '/projects/org-slug/project-slug/transaction-threshold/configure/',
  63. method: 'GET',
  64. body: {
  65. threshold: '200',
  66. metric: 'duration',
  67. },
  68. });
  69. renderComponent(eventView, organization, onChangeThreshold);
  70. const button = screen.getByRole('button');
  71. await waitFor(() => expect(button).toBeEnabled());
  72. expect(getTransactionThresholdMock).toHaveBeenCalledTimes(1);
  73. expect(getProjectThresholdMock).not.toHaveBeenCalled();
  74. });
  75. it('gets project threshold if transaction threshold does not exist', async function () {
  76. const getTransactionThresholdMock = MockApiClient.addMockResponse({
  77. url: '/organizations/org-slug/project-transaction-threshold-override/',
  78. method: 'GET',
  79. statusCode: 404,
  80. });
  81. const getProjectThresholdMock = MockApiClient.addMockResponse({
  82. url: '/projects/org-slug/project-slug/transaction-threshold/configure/',
  83. method: 'GET',
  84. body: {
  85. threshold: '200',
  86. metric: 'duration',
  87. },
  88. });
  89. renderComponent(eventView, organization, onChangeThreshold);
  90. const button = screen.getByRole('button');
  91. await waitFor(() => expect(button).toBeEnabled());
  92. expect(getTransactionThresholdMock).toHaveBeenCalledTimes(1);
  93. expect(getProjectThresholdMock).toHaveBeenCalledTimes(1);
  94. });
  95. it('mounts modal with the right values', async function () {
  96. MockApiClient.addMockResponse({
  97. url: '/organizations/org-slug/project-transaction-threshold-override/',
  98. method: 'GET',
  99. body: {
  100. threshold: '800',
  101. metric: 'lcp',
  102. },
  103. });
  104. renderComponent(eventView, organization, onChangeThreshold);
  105. const button = screen.getByRole('button');
  106. await waitFor(() => expect(button).toBeEnabled());
  107. await userEvent.click(button);
  108. renderGlobalModal();
  109. expect(screen.getByRole('spinbutton')).toHaveValue(800);
  110. expect(screen.getByText('Largest Contentful Paint')).toBeInTheDocument();
  111. });
  112. });