transactionThresholdButton.spec.jsx 4.0 KB

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