transactionThresholdModal.spec.jsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import {mountWithTheme} from 'sentry-test/enzyme';
  2. import {act} from 'sentry-test/reactTestingLibrary';
  3. import {selectByLabel} from 'sentry-test/select-new';
  4. import ProjectsStore from 'sentry/stores/projectsStore';
  5. import EventView from 'sentry/utils/discover/eventView';
  6. import TransactionThresholdModal from 'sentry/views/performance/transactionSummary/transactionThresholdModal';
  7. const stubEl = props => <div>{props.children}</div>;
  8. function clickSubmit(wrapper) {
  9. // Click on submit.
  10. const button = wrapper.find('Button[data-test-id="apply-threshold"] button');
  11. button.simulate('click');
  12. return tick();
  13. }
  14. function clickReset(wrapper) {
  15. // Click on submit.
  16. const button = wrapper.find('Button[data-test-id="reset-all"] button');
  17. button.simulate('click');
  18. return tick();
  19. }
  20. function mountModal(eventView, organization, onApply) {
  21. return mountWithTheme(
  22. <TransactionThresholdModal
  23. Header={stubEl}
  24. Footer={stubEl}
  25. Body={stubEl}
  26. eventView={eventView}
  27. organization={organization}
  28. transactionName="transaction/threshold"
  29. transactionThreshold="400"
  30. transactionThresholdMetric="lcp"
  31. onApply={onApply}
  32. closeModal={() => void 0}
  33. />
  34. );
  35. }
  36. describe('TransactionThresholdModal', function () {
  37. const organization = TestStubs.Organization({features: ['performance-view']});
  38. const project = TestStubs.Project();
  39. const eventView = new EventView({
  40. id: '1',
  41. name: 'my query',
  42. fields: [{field: 'count()'}],
  43. sorts: [{field: 'count', kind: 'desc'}],
  44. query: '',
  45. project: [project.id],
  46. start: '2019-10-01T00:00:00',
  47. end: '2019-10-02T00:00:00',
  48. statsPeriod: '14d',
  49. environment: [],
  50. });
  51. const onApply = jest.fn();
  52. let postTransactionThresholdMock;
  53. beforeEach(function () {
  54. MockApiClient.clearMockResponses();
  55. act(() => ProjectsStore.loadInitialData([project]));
  56. postTransactionThresholdMock = MockApiClient.addMockResponse({
  57. url: '/organizations/org-slug/project-transaction-threshold-override/',
  58. method: 'POST',
  59. body: {
  60. data: [],
  61. },
  62. });
  63. });
  64. it('can update threshold', async function () {
  65. const wrapper = mountModal(eventView, organization, onApply);
  66. await tick();
  67. wrapper.update();
  68. const input = wrapper.find('Input[name="threshold"]');
  69. input.simulate('change', {target: {value: '1000'}}).simulate('blur');
  70. await clickSubmit(wrapper);
  71. expect(postTransactionThresholdMock).toHaveBeenCalledWith(
  72. '/organizations/org-slug/project-transaction-threshold-override/',
  73. expect.objectContaining({
  74. data: {metric: 'lcp', threshold: '1000', transaction: 'transaction/threshold'},
  75. })
  76. );
  77. wrapper.unmount();
  78. });
  79. it('can update metric', async function () {
  80. const wrapper = mountModal(eventView, organization, onApply);
  81. await tick();
  82. wrapper.update();
  83. selectByLabel(wrapper, 'Transaction Duration', {
  84. name: 'responseMetric',
  85. at: 0,
  86. control: true,
  87. });
  88. expect(wrapper.find('input[name="responseMetric"]').props().value).toEqual(
  89. 'duration'
  90. );
  91. await clickSubmit(wrapper);
  92. expect(postTransactionThresholdMock).toHaveBeenCalledWith(
  93. '/organizations/org-slug/project-transaction-threshold-override/',
  94. expect.objectContaining({
  95. data: {
  96. metric: 'duration',
  97. threshold: '400',
  98. transaction: 'transaction/threshold',
  99. },
  100. })
  101. );
  102. wrapper.unmount();
  103. });
  104. it('can clear metrics', async function () {
  105. const wrapper = mountModal(eventView, organization, onApply);
  106. await tick();
  107. wrapper.update();
  108. expect(wrapper.find('input[name="responseMetric"]').props().value).toEqual('lcp');
  109. expect(wrapper.find('input[name="threshold"]').props().value).toEqual('400');
  110. const deleteTransactionThresholdMock = MockApiClient.addMockResponse({
  111. url: '/organizations/org-slug/project-transaction-threshold-override/',
  112. method: 'DELETE',
  113. });
  114. const getProjectThresholdMock = MockApiClient.addMockResponse({
  115. url: '/projects/org-slug/project-slug/transaction-threshold/configure/',
  116. method: 'GET',
  117. body: {
  118. threshold: '200',
  119. metric: 'duration',
  120. },
  121. });
  122. await clickReset(wrapper);
  123. wrapper.update();
  124. expect(deleteTransactionThresholdMock).toHaveBeenCalledTimes(1);
  125. // Replace with project fallback
  126. expect(getProjectThresholdMock).toHaveBeenCalledTimes(1);
  127. wrapper.unmount();
  128. });
  129. });