transactionThresholdButton.spec.jsx 3.9 KB

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