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.

AuditApp-test.tsx 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 { getValue } from '../../../../api/settings';
  24. import { waitAndUpdate } from '../../../../helpers/testUtils';
  25. import { AdminPageExtension } from '../../../../types/extension';
  26. import { HousekeepingPolicy, RangeOption } from '../../utils';
  27. import { AuditApp } from '../AuditApp';
  28. import AuditAppRenderer from '../AuditAppRenderer';
  29. jest.mock('../../../../api/settings', () => ({
  30. getValue: jest.fn().mockResolvedValue({})
  31. }));
  32. beforeEach(() => {
  33. jest.clearAllMocks();
  34. });
  35. it('should render correctly', () => {
  36. expect(shallowRender()).toMatchSnapshot();
  37. });
  38. it('should do nothing if governance is not available', async () => {
  39. const wrapper = shallowRender({ adminPages: [] });
  40. await waitAndUpdate(wrapper);
  41. expect(wrapper.type()).toBeNull();
  42. expect(getValue).not.toBeCalled();
  43. });
  44. it('should handle housekeeping policy', async () => {
  45. (getValue as jest.Mock).mockResolvedValueOnce({ value: HousekeepingPolicy.Weekly });
  46. const wrapper = shallowRender();
  47. await waitAndUpdate(wrapper);
  48. expect(wrapper.find(AuditAppRenderer).props().housekeepingPolicy).toBe(HousekeepingPolicy.Weekly);
  49. });
  50. it('should handle date selection', () => {
  51. const wrapper = shallowRender();
  52. const range = { from: subDays(new Date(), 2), to: new Date() };
  53. expect(wrapper.state().selection).toBe(RangeOption.Today);
  54. wrapper
  55. .find(AuditAppRenderer)
  56. .props()
  57. .handleDateSelection(range);
  58. expect(wrapper.state().selection).toBe(RangeOption.Custom);
  59. expect(wrapper.state().dateRange).toBe(range);
  60. });
  61. it('should handle predefined selection', () => {
  62. const wrapper = shallowRender();
  63. const dateRange = { from: subDays(new Date(), 2), to: new Date() };
  64. wrapper.setState({ dateRange, selection: RangeOption.Custom });
  65. wrapper
  66. .find(AuditAppRenderer)
  67. .props()
  68. .handleOptionSelection(RangeOption.Week);
  69. expect(wrapper.state().selection).toBe(RangeOption.Week);
  70. expect(wrapper.state().dateRange).toBeUndefined();
  71. });
  72. it('should handle update to admin pages', async () => {
  73. const wrapper = shallowRender({ adminPages: [] });
  74. await waitAndUpdate(wrapper);
  75. expect(wrapper.type()).toBeNull();
  76. expect(getValue).not.toBeCalled();
  77. wrapper.setProps({ adminPages: [{ key: AdminPageExtension.GovernanceConsole, name: 'name' }] });
  78. await waitAndUpdate(wrapper);
  79. expect(getValue).toBeCalled();
  80. });
  81. function shallowRender(props: Partial<AuditApp['props']> = {}) {
  82. return shallow<AuditApp>(
  83. <AuditApp
  84. adminPages={[{ key: AdminPageExtension.GovernanceConsole, name: 'name' }]}
  85. {...props}
  86. />
  87. );
  88. }