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.

SourceViewerBase-test.tsx 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 { shallow } from 'enzyme';
  21. import * as React from 'react';
  22. import { getComponentData, getComponentForSourceViewer, getSources } from '../../../api/components';
  23. import { mockMainBranch } from '../../../helpers/mocks/branch-like';
  24. import { mockSourceLine, mockSourceViewerFile } from '../../../helpers/mocks/sources';
  25. import { mockIssue } from '../../../helpers/testMocks';
  26. import { waitAndUpdate } from '../../../helpers/testUtils';
  27. import defaultLoadIssues from '../helpers/loadIssues';
  28. import SourceViewerBase from '../SourceViewerBase';
  29. jest.mock('../helpers/loadIssues', () => jest.fn().mockRejectedValue({}));
  30. jest.mock('../../../api/components', () => ({
  31. getComponentForSourceViewer: jest.fn().mockRejectedValue(''),
  32. getComponentData: jest.fn().mockRejectedValue(''),
  33. getSources: jest.fn().mockRejectedValue('')
  34. }));
  35. beforeEach(() => {
  36. jest.resetAllMocks();
  37. });
  38. it('should render nothing from the start', () => {
  39. expect(shallowRender().type()).toBeNull();
  40. });
  41. it('should render correctly', async () => {
  42. (defaultLoadIssues as jest.Mock).mockResolvedValueOnce([mockIssue()]);
  43. (getComponentForSourceViewer as jest.Mock).mockResolvedValueOnce(mockSourceViewerFile());
  44. (getComponentData as jest.Mock).mockResolvedValueOnce({
  45. component: { leakPeriodDate: '2018-06-20T17:12:19+0200' }
  46. });
  47. (getSources as jest.Mock).mockResolvedValueOnce([]);
  48. const wrapper = shallowRender();
  49. await waitAndUpdate(wrapper);
  50. expect(wrapper).toMatchSnapshot();
  51. });
  52. it('should use load props if provided', () => {
  53. const loadIssues = jest.fn().mockResolvedValue([]);
  54. const wrapper = shallowRender({
  55. loadIssues
  56. });
  57. expect(wrapper.instance().loadIssues).toBe(loadIssues);
  58. });
  59. it('should reload', async () => {
  60. (defaultLoadIssues as jest.Mock)
  61. .mockResolvedValueOnce([mockIssue()])
  62. .mockResolvedValueOnce([mockIssue()]);
  63. (getComponentForSourceViewer as jest.Mock).mockResolvedValueOnce(mockSourceViewerFile());
  64. (getComponentData as jest.Mock).mockResolvedValueOnce({
  65. component: { leakPeriodDate: '2018-06-20T17:12:19+0200' }
  66. });
  67. (getSources as jest.Mock).mockResolvedValueOnce([mockSourceLine()]);
  68. const wrapper = shallowRender();
  69. await waitAndUpdate(wrapper);
  70. wrapper.instance().reloadIssues();
  71. expect(defaultLoadIssues).toBeCalledTimes(2);
  72. await waitAndUpdate(wrapper);
  73. expect(wrapper.state().issues).toHaveLength(1);
  74. });
  75. it('should load sources before', async () => {
  76. (defaultLoadIssues as jest.Mock)
  77. .mockResolvedValueOnce([mockIssue(false, { key: 'issue1' })])
  78. .mockResolvedValueOnce([mockIssue(false, { key: 'issue2' })]);
  79. (getComponentForSourceViewer as jest.Mock).mockResolvedValueOnce(mockSourceViewerFile());
  80. (getComponentData as jest.Mock).mockResolvedValueOnce({
  81. component: { leakPeriodDate: '2018-06-20T17:12:19+0200' }
  82. });
  83. (getSources as jest.Mock)
  84. .mockResolvedValueOnce([mockSourceLine()])
  85. .mockResolvedValueOnce([mockSourceLine()]);
  86. const wrapper = shallowRender();
  87. await waitAndUpdate(wrapper);
  88. wrapper.instance().loadSourcesBefore();
  89. expect(wrapper.state().loadingSourcesBefore).toBe(true);
  90. expect(defaultLoadIssues).toBeCalledTimes(2);
  91. expect(getSources).toBeCalledTimes(2);
  92. await waitAndUpdate(wrapper);
  93. expect(wrapper.state().loadingSourcesBefore).toBe(false);
  94. expect(wrapper.state().issues).toHaveLength(2);
  95. });
  96. it('should load sources after', async () => {
  97. (defaultLoadIssues as jest.Mock)
  98. .mockResolvedValueOnce([mockIssue(false, { key: 'issue1' })])
  99. .mockResolvedValueOnce([mockIssue(false, { key: 'issue2' })]);
  100. (getComponentForSourceViewer as jest.Mock).mockResolvedValueOnce(mockSourceViewerFile());
  101. (getComponentData as jest.Mock).mockResolvedValueOnce({
  102. component: { leakPeriodDate: '2018-06-20T17:12:19+0200' }
  103. });
  104. (getSources as jest.Mock)
  105. .mockResolvedValueOnce([mockSourceLine()])
  106. .mockResolvedValueOnce([mockSourceLine()]);
  107. const wrapper = shallowRender();
  108. await waitAndUpdate(wrapper);
  109. wrapper.instance().loadSourcesAfter();
  110. expect(wrapper.state().loadingSourcesAfter).toBe(true);
  111. expect(defaultLoadIssues).toBeCalledTimes(2);
  112. expect(getSources).toBeCalledTimes(2);
  113. await waitAndUpdate(wrapper);
  114. expect(wrapper.state().loadingSourcesAfter).toBe(false);
  115. expect(wrapper.state().issues).toHaveLength(2);
  116. });
  117. it('should handle no sources when checking ranges', () => {
  118. const wrapper = shallowRender();
  119. wrapper.setState({ sources: undefined });
  120. expect(wrapper.instance().isLineOutsideOfRange(12)).toBe(true);
  121. });
  122. function shallowRender(overrides: Partial<SourceViewerBase['props']> = {}) {
  123. return shallow<SourceViewerBase>(
  124. <SourceViewerBase branchLike={mockMainBranch()} component="my-component" {...overrides} />
  125. );
  126. }