eventsChart.spec.jsx 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import {chart, doZoom, mockZoomRange} from 'sentry-test/charts';
  2. import {mountWithTheme} from 'sentry-test/enzyme';
  3. import {initializeOrg} from 'sentry-test/initializeOrg';
  4. import * as globalSelection from 'sentry/actionCreators/pageFilters';
  5. import EventsChart from 'sentry/components/charts/eventsChart';
  6. import {WorldMapChart} from 'sentry/components/charts/worldMapChart';
  7. import {getUtcToLocalDateObject} from 'sentry/utils/dates';
  8. jest.mock('sentry/components/charts/eventsRequest', () => jest.fn(() => null));
  9. jest.spyOn(globalSelection, 'updateDateTime');
  10. jest.mock(
  11. 'sentry/components/charts/eventsGeoRequest',
  12. () =>
  13. ({children}) =>
  14. children({
  15. errored: false,
  16. loading: false,
  17. reloading: false,
  18. tableData: [],
  19. })
  20. );
  21. describe('EventsChart', function () {
  22. const {router, routerContext, org} = initializeOrg();
  23. let render;
  24. let wrapper;
  25. beforeEach(function () {
  26. globalSelection.updateDateTime.mockClear();
  27. mockZoomRange(1543449600000, 1543708800000);
  28. wrapper = mountWithTheme(
  29. <EventsChart
  30. api={new MockApiClient()}
  31. location={{query: {}}}
  32. organization={org}
  33. project={[]}
  34. environment={[]}
  35. period="14d"
  36. start={null}
  37. end={null}
  38. utc={false}
  39. router={router}
  40. />,
  41. routerContext
  42. );
  43. // XXX: Note we spy on this AFTER it has already rendered once!
  44. render = jest.spyOn(wrapper.find('ChartZoom').instance(), 'render');
  45. });
  46. it('renders', function () {
  47. expect(render).toHaveBeenCalledTimes(0);
  48. });
  49. it('re-renders if period from props changes', function () {
  50. wrapper.setProps({period: '7d'});
  51. wrapper.update();
  52. expect(render).toHaveBeenCalledTimes(1);
  53. });
  54. it('re-renders if query from props changes', function () {
  55. wrapper.setProps({query: 'newQuery'});
  56. wrapper.update();
  57. expect(render).toHaveBeenCalledTimes(1);
  58. });
  59. it('re-renders if project from props changes', function () {
  60. wrapper.setProps({project: [2]});
  61. wrapper.update();
  62. expect(render).toHaveBeenCalledTimes(1);
  63. });
  64. it('has correct history entries when zooming', function () {
  65. const chartZoomInstance = wrapper.find('ChartZoom').instance();
  66. doZoom(wrapper, chart);
  67. expect(chartZoomInstance.history).toEqual([
  68. {
  69. period: '14d',
  70. start: null,
  71. end: null,
  72. },
  73. ]);
  74. expect(chartZoomInstance.currentPeriod.period).toEqual(null);
  75. expect(chartZoomInstance.currentPeriod.start).toEqual('2018-11-29T00:00:00');
  76. expect(chartZoomInstance.currentPeriod.end).toEqual('2018-12-02T00:00:00');
  77. // Zoom again
  78. mockZoomRange(1543536000000, 1543708800000);
  79. doZoom(wrapper, chart);
  80. expect(chartZoomInstance.currentPeriod.period).toEqual(null);
  81. expect(chartZoomInstance.currentPeriod.start).toEqual('2018-11-30T00:00:00');
  82. expect(chartZoomInstance.currentPeriod.end).toEqual('2018-12-02T00:00:00');
  83. expect(chartZoomInstance.history[0]).toEqual({
  84. period: '14d',
  85. start: null,
  86. end: null,
  87. });
  88. expect(chartZoomInstance.history[1].start).toEqual('2018-11-29T00:00:00');
  89. expect(chartZoomInstance.history[1].end).toEqual('2018-12-02T00:00:00');
  90. // go back in history
  91. mockZoomRange(null, null);
  92. doZoom(wrapper, chart);
  93. expect(chartZoomInstance.currentPeriod.period).toEqual(null);
  94. expect(chartZoomInstance.currentPeriod.start).toEqual('2018-11-29T00:00:00');
  95. expect(chartZoomInstance.currentPeriod.end).toEqual('2018-12-02T00:00:00');
  96. const newParams = {
  97. period: null,
  98. start: getUtcToLocalDateObject('2018-11-29T00:00:00'),
  99. end: getUtcToLocalDateObject('2018-12-02T00:00:00'),
  100. };
  101. expect(globalSelection.updateDateTime).toHaveBeenCalledWith(newParams, router);
  102. wrapper.setProps({
  103. period: newParams.period,
  104. start: newParams.start,
  105. end: newParams.end,
  106. });
  107. wrapper.update();
  108. });
  109. it('updates url params when restoring zoom level on chart', function () {
  110. doZoom(wrapper, chart);
  111. // Zoom again
  112. mockZoomRange(1543536000000, 1543708800000);
  113. doZoom(wrapper, chart);
  114. mockZoomRange(1543622400000, 1543708800000);
  115. doZoom(wrapper, chart);
  116. const chartZoomInstance = wrapper.find('ChartZoom').instance();
  117. expect(chartZoomInstance.history).toHaveLength(3);
  118. // Restore history
  119. chartZoomInstance.handleZoomRestore();
  120. chartZoomInstance.handleChartFinished({}, chart);
  121. expect(chartZoomInstance.currentPeriod).toEqual({
  122. period: '14d',
  123. start: null,
  124. end: null,
  125. });
  126. const newParams = {
  127. period: '14d',
  128. start: null,
  129. end: null,
  130. };
  131. expect(globalSelection.updateDateTime).toHaveBeenLastCalledWith(newParams, router);
  132. wrapper.setProps({
  133. period: '14d',
  134. start: null,
  135. end: null,
  136. });
  137. wrapper.update();
  138. expect(chartZoomInstance.history).toHaveLength(0);
  139. });
  140. it('renders with World Map when given WorldMapChart chartComponent', function () {
  141. wrapper.setProps({chartComponent: WorldMapChart, yAxis: ['count()']});
  142. expect(wrapper.find('WorldMapChart').length).toEqual(1);
  143. });
  144. });