eventsChart.spec.jsx 5.4 KB

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