transactionThresholdButton.spec.tsx 3.7 KB

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