transactionThresholdButton.spec.tsx 3.8 KB

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