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.

DownloadButton-test.tsx 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2022 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 { subDays } from 'date-fns';
  21. import { shallow } from 'enzyme';
  22. import * as React from 'react';
  23. import { RangeOption } from '../../utils';
  24. import DownloadButton, { DownloadButtonProps } from '../DownloadButton';
  25. jest.mock('date-fns', () => {
  26. const { subDays } = jest.requireActual('date-fns');
  27. return {
  28. endOfDay: jest.fn().mockImplementation(d => d),
  29. startOfDay: jest.fn().mockImplementation(d => d),
  30. subDays
  31. };
  32. });
  33. jest.mock('../../../../helpers/dates', () => {
  34. return {
  35. ...jest.requireActual('../../../../helpers/dates'),
  36. now: jest.fn(() => new Date('2020-07-21T12:00:00Z'))
  37. };
  38. });
  39. it.each([[RangeOption.Today], [RangeOption.Week], [RangeOption.Month], [RangeOption.Trimester]])(
  40. 'should render correctly for %s',
  41. selection => {
  42. expect(shallowRender({ selection })).toMatchSnapshot('default');
  43. }
  44. );
  45. it('should render correctly for custom range', () => {
  46. const baseDate = new Date('2020-07-21T12:00:00Z');
  47. expect(shallowRender({ selection: RangeOption.Custom })).toMatchSnapshot('no dates');
  48. expect(
  49. shallowRender({
  50. dateRange: { from: subDays(baseDate, 2), to: baseDate },
  51. selection: RangeOption.Custom
  52. })
  53. ).toMatchSnapshot('with dates');
  54. });
  55. it('should handle download', () => {
  56. const onStartDownload = jest.fn();
  57. const wrapper = shallowRender({ onStartDownload });
  58. wrapper.find('a').simulate('click');
  59. wrapper.setProps({ downloadStarted: true });
  60. wrapper.find('a').simulate('click');
  61. expect(onStartDownload).toBeCalledTimes(1);
  62. });
  63. function shallowRender(props: Partial<DownloadButtonProps> = {}) {
  64. return shallow<DownloadButtonProps>(
  65. <DownloadButton
  66. downloadStarted={false}
  67. selection={RangeOption.Today}
  68. onStartDownload={jest.fn()}
  69. {...props}
  70. />
  71. );
  72. }