You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ZoomTimeLine-test.tsx 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. import { scaleTime } from 'd3-scale';
  21. import { shallow } from 'enzyme';
  22. import * as React from 'react';
  23. import testTheme from '../../../config/jest/testTheme';
  24. import ZoomTimeLine from '../ZoomTimeLine';
  25. const series = [
  26. {
  27. data: [
  28. {
  29. x: new Date('2020-01-01'),
  30. y: 'beginning',
  31. },
  32. {
  33. x: new Date('2020-02-01'),
  34. y: 'end',
  35. },
  36. ],
  37. name: 'foo',
  38. translatedName: 'foo-translated',
  39. type: 'bar',
  40. },
  41. ];
  42. it('should render correctly', () => {
  43. expect(shallowRender({ width: undefined })).toMatchSnapshot('no width');
  44. expect(shallowRender({ height: undefined })).toMatchSnapshot('no height');
  45. });
  46. it('should draw a graph with lines', () => {
  47. const wrapper = shallowRender();
  48. expect(wrapper.find('.line-chart-grid').exists()).toBe(true);
  49. expect(wrapper.find('.line-chart-path').exists()).toBe(true);
  50. expect(wrapper.find('.chart-zoom-tick').exists()).toBe(true);
  51. expect(wrapper.find('.line-chart-area').exists()).toBe(false);
  52. });
  53. it('should be zoomable', () => {
  54. expect(shallowRender().find('.chart-zoom').exists()).toBe(true);
  55. });
  56. it('should render a leak period', () => {
  57. expect(
  58. shallowRender({ leakPeriodDate: new Date('2020-01-01') })
  59. .find('ContextConsumer')
  60. .dive()
  61. .find(`rect[fill="${testTheme.colors.leakPrimaryColor}"]`)
  62. .exists()
  63. ).toBe(true);
  64. });
  65. it('should render areas under the graph lines', () => {
  66. expect(shallowRender({ showAreas: true }).find('.line-chart-area').exists()).toBe(true);
  67. });
  68. it('should handle zoom update correctly', () => {
  69. const updateZoom = jest.fn();
  70. const startDate = new Date('1970-01-01T00:00:00.001Z');
  71. const endDate = new Date('2000-01-01T00:00:00.001Z');
  72. let wrapper = shallowRender({ updateZoom, startDate, endDate });
  73. wrapper
  74. .instance()
  75. .handleZoomUpdate(scaleTime().domain([startDate, endDate]).range([0, 150]), [3, 50]);
  76. expect(updateZoom).toBeCalledWith(
  77. new Date('1970-08-08T03:21:36.001Z'),
  78. new Date('1980-01-01T08:00:00.001Z')
  79. );
  80. updateZoom.mockClear();
  81. // We throttle the handleZoomUpdate so re-render to avoid issue
  82. wrapper = shallowRender({ updateZoom, startDate, endDate });
  83. wrapper
  84. .instance()
  85. .handleZoomUpdate(scaleTime().domain([startDate, endDate]).range([0, 150]), [-1, 151]);
  86. expect(updateZoom).toBeCalledWith(undefined, undefined);
  87. });
  88. function shallowRender(props: Partial<ZoomTimeLine['props']> = {}) {
  89. return shallow<ZoomTimeLine>(
  90. <ZoomTimeLine
  91. width={300}
  92. series={series}
  93. updateZoom={jest.fn()}
  94. metricType="RATING"
  95. height={300}
  96. {...props}
  97. />
  98. );
  99. }