transactionThresholdButton.spec.tsx 3.6 KB

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