onDemandMetricRequest.spec.tsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import {OrganizationFixture} 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 = OrganizationFixture();
  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. jest.mocked(doEventsRequest).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', async function () {
  60. const {rerender} = render(
  61. <OnDemandMetricRequest {...DEFAULTS}>{mock}</OnDemandMetricRequest>
  62. );
  63. rerender(
  64. <OnDemandMetricRequest {...DEFAULTS} project={[123]}>
  65. {mock}
  66. </OnDemandMetricRequest>
  67. );
  68. await waitFor(() => expect(doEventsRequest).toHaveBeenCalledTimes(2));
  69. expect(doEventsRequest).toHaveBeenCalledWith(
  70. expect.anything(),
  71. expect.objectContaining({
  72. project: [123],
  73. })
  74. );
  75. });
  76. it('makes a new request if environments prop changes', async function () {
  77. const {rerender} = render(
  78. <OnDemandMetricRequest {...DEFAULTS}>{mock}</OnDemandMetricRequest>
  79. );
  80. rerender(
  81. <OnDemandMetricRequest {...DEFAULTS} environment={['dev']}>
  82. {mock}
  83. </OnDemandMetricRequest>
  84. );
  85. await waitFor(() => expect(doEventsRequest).toHaveBeenCalledTimes(2));
  86. expect(doEventsRequest).toHaveBeenCalledWith(
  87. expect.anything(),
  88. expect.objectContaining({
  89. environment: ['dev'],
  90. })
  91. );
  92. });
  93. it('makes a new request if period prop changes', async function () {
  94. const {rerender} = render(
  95. <OnDemandMetricRequest {...DEFAULTS}>{mock}</OnDemandMetricRequest>
  96. );
  97. rerender(
  98. <OnDemandMetricRequest {...DEFAULTS} period="7d">
  99. {mock}
  100. </OnDemandMetricRequest>
  101. );
  102. await waitFor(() => expect(doEventsRequest).toHaveBeenCalledTimes(2));
  103. expect(doEventsRequest).toHaveBeenCalledWith(
  104. expect.anything(),
  105. expect.objectContaining({
  106. period: '7d',
  107. })
  108. );
  109. });
  110. });
  111. });