transactionThresholdButton.spec.jsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. });
  36. const onChangeThreshold = jest.fn();
  37. beforeEach(function () {
  38. MockApiClient.clearMockResponses();
  39. ProjectsStore.loadInitialData([project]);
  40. });
  41. it('renders element correctly', async function () {
  42. const getTransactionThresholdMock = MockApiClient.addMockResponse({
  43. url: '/organizations/org-slug/project-transaction-threshold-override/',
  44. method: 'GET',
  45. body: {
  46. threshold: '800',
  47. metric: 'lcp',
  48. },
  49. });
  50. const getProjectThresholdMock = MockApiClient.addMockResponse({
  51. url: '/projects/org-slug/project-slug/transaction-threshold/configure/',
  52. method: 'GET',
  53. body: {
  54. threshold: '200',
  55. metric: 'duration',
  56. },
  57. });
  58. renderComponent(eventView, organization, onChangeThreshold);
  59. const button = screen.getByRole('button');
  60. await waitFor(() => expect(button).toBeEnabled());
  61. expect(getTransactionThresholdMock).toHaveBeenCalledTimes(1);
  62. expect(getProjectThresholdMock).not.toHaveBeenCalled();
  63. });
  64. it('gets project threshold if transaction threshold does not exist', async function () {
  65. const getTransactionThresholdMock = MockApiClient.addMockResponse({
  66. url: '/organizations/org-slug/project-transaction-threshold-override/',
  67. method: 'GET',
  68. statusCode: 404,
  69. });
  70. const getProjectThresholdMock = MockApiClient.addMockResponse({
  71. url: '/projects/org-slug/project-slug/transaction-threshold/configure/',
  72. method: 'GET',
  73. body: {
  74. threshold: '200',
  75. metric: 'duration',
  76. },
  77. });
  78. renderComponent(eventView, organization, onChangeThreshold);
  79. const button = screen.getByRole('button');
  80. await waitFor(() => expect(button).toBeEnabled());
  81. expect(getTransactionThresholdMock).toHaveBeenCalledTimes(1);
  82. expect(getProjectThresholdMock).toHaveBeenCalledTimes(1);
  83. });
  84. it('mounts modal with the right values', async function () {
  85. MockApiClient.addMockResponse({
  86. url: '/organizations/org-slug/project-transaction-threshold-override/',
  87. method: 'GET',
  88. body: {
  89. threshold: '800',
  90. metric: 'lcp',
  91. },
  92. });
  93. renderComponent(eventView, organization, onChangeThreshold);
  94. const button = screen.getByRole('button');
  95. await waitFor(() => expect(button).toBeEnabled());
  96. userEvent.click(button);
  97. renderGlobalModal();
  98. expect(screen.getByRole('spinbutton')).toHaveValue(800);
  99. expect(screen.getByText('Largest Contentful Paint')).toBeInTheDocument();
  100. });
  101. });