onDemandMetricRequest.spec.tsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import {Organization} from 'sentry-fixture/organization';
  2. import {render, waitFor} from 'sentry-test/reactTestingLibrary';
  3. import {doEventsRequest} from 'sentry/actionCreators/events';
  4. import {OnDemandMetricRequest} from 'sentry/components/charts/onDemandMetricRequest';
  5. const SAMPLE_RATE = 0.5;
  6. jest.mock('sentry/actionCreators/events', () => ({
  7. doEventsRequest: jest.fn(),
  8. }));
  9. describe('OnDemandMetricRequest', function () {
  10. const organization = Organization();
  11. const mock = jest.fn(() => null);
  12. const DEFAULTS = {
  13. api: new MockApiClient(),
  14. period: '24h',
  15. organization,
  16. includePrevious: false,
  17. interval: '24h',
  18. limit: 30,
  19. query: 'transaction.duration:>1',
  20. children: () => null,
  21. partial: false,
  22. includeTransformedData: true,
  23. sampleRate: SAMPLE_RATE,
  24. };
  25. describe('with props changes', function () {
  26. beforeAll(function () {
  27. (doEventsRequest as jest.Mock).mockImplementation(() =>
  28. Promise.resolve({
  29. isMetricsData: true,
  30. data: [],
  31. })
  32. );
  33. });
  34. it('makes requests', async function () {
  35. render(<OnDemandMetricRequest {...DEFAULTS}>{mock}</OnDemandMetricRequest>);
  36. expect(mock).toHaveBeenNthCalledWith(
  37. 1,
  38. expect.objectContaining({
  39. loading: true,
  40. })
  41. );
  42. await waitFor(() =>
  43. expect(mock).toHaveBeenLastCalledWith(
  44. expect.objectContaining({
  45. loading: false,
  46. timeseriesData: [
  47. {
  48. // isExtrapolatedData: true,
  49. seriesName: expect.anything(),
  50. data: [],
  51. },
  52. ],
  53. originalTimeseriesData: [],
  54. })
  55. )
  56. );
  57. expect(doEventsRequest).toHaveBeenCalled();
  58. });
  59. it('makes a new request if projects prop changes', function () {
  60. const {rerender} = render(
  61. <OnDemandMetricRequest {...DEFAULTS}>{mock}</OnDemandMetricRequest>
  62. );
  63. doEventsRequest as jest.Mock;
  64. rerender(
  65. <OnDemandMetricRequest {...DEFAULTS} project={[123]}>
  66. {mock}
  67. </OnDemandMetricRequest>
  68. );
  69. expect(doEventsRequest).toHaveBeenCalledTimes(2);
  70. expect(doEventsRequest).toHaveBeenCalledWith(
  71. expect.anything(),
  72. expect.objectContaining({
  73. project: [123],
  74. })
  75. );
  76. });
  77. it('makes a new request if environments prop changes', function () {
  78. const {rerender} = render(
  79. <OnDemandMetricRequest {...DEFAULTS}>{mock}</OnDemandMetricRequest>
  80. );
  81. doEventsRequest as jest.Mock;
  82. rerender(
  83. <OnDemandMetricRequest {...DEFAULTS} environment={['dev']}>
  84. {mock}
  85. </OnDemandMetricRequest>
  86. );
  87. expect(doEventsRequest).toHaveBeenCalledTimes(2);
  88. expect(doEventsRequest).toHaveBeenCalledWith(
  89. expect.anything(),
  90. expect.objectContaining({
  91. environment: ['dev'],
  92. })
  93. );
  94. });
  95. it('makes a new request if period prop changes', function () {
  96. const {rerender} = render(
  97. <OnDemandMetricRequest {...DEFAULTS}>{mock}</OnDemandMetricRequest>
  98. );
  99. doEventsRequest as jest.Mock;
  100. rerender(
  101. <OnDemandMetricRequest {...DEFAULTS} period="7d">
  102. {mock}
  103. </OnDemandMetricRequest>
  104. );
  105. expect(doEventsRequest).toHaveBeenCalledTimes(2);
  106. expect(doEventsRequest).toHaveBeenCalledWith(
  107. expect.anything(),
  108. expect.objectContaining({
  109. period: '7d',
  110. })
  111. );
  112. });
  113. });
  114. });