您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

SnippetViewer-test.tsx 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 { mount, shallow } from 'enzyme';
  21. import { range } from 'lodash';
  22. import * as React from 'react';
  23. import { mockSourceLine, mockSourceViewerFile } from '../../../../helpers/mocks/sources';
  24. import { scrollHorizontally } from '../../../../helpers/scrolling';
  25. import { mockIssue } from '../../../../helpers/testMocks';
  26. import SnippetViewer from '../SnippetViewer';
  27. jest.mock('../../../../helpers/scrolling', () => ({
  28. scrollHorizontally: jest.fn()
  29. }));
  30. beforeEach(() => {
  31. jest.clearAllMocks();
  32. });
  33. it('should render correctly', () => {
  34. const snippet = range(5, 8).map(line => mockSourceLine({ line }));
  35. const wrapper = shallowRender({
  36. snippet
  37. });
  38. expect(wrapper).toMatchSnapshot();
  39. });
  40. it('should render correctly with no SCM', () => {
  41. const snippet = range(5, 8).map(line => mockSourceLine({ line }));
  42. const wrapper = shallowRender({
  43. displaySCM: false,
  44. snippet
  45. });
  46. expect(wrapper).toMatchSnapshot();
  47. });
  48. it('should render additional child in line', () => {
  49. const sourceline = mockSourceLine({ line: 42 });
  50. const child = <div>child</div>;
  51. const renderAdditionalChildInLine = jest.fn().mockReturnValue(child);
  52. const wrapper = shallowRender({ renderAdditionalChildInLine, snippet: [sourceline] });
  53. wrapper.instance().renderLine({
  54. displayDuplications: false,
  55. index: 1,
  56. issuesForLine: [],
  57. issueLocations: [],
  58. line: sourceline,
  59. snippet: [sourceline],
  60. symbols: [],
  61. verticalBuffer: 5
  62. });
  63. expect(renderAdditionalChildInLine).toBeCalledWith(sourceline);
  64. });
  65. it('should render correctly when at the top of the file', () => {
  66. const snippet = range(1, 8).map(line => mockSourceLine({ line }));
  67. const wrapper = shallowRender({
  68. snippet
  69. });
  70. expect(wrapper).toMatchSnapshot();
  71. });
  72. it('should render correctly when at the bottom of the file', () => {
  73. const component = mockSourceViewerFile('foo/bar.ts', 'my-project', { measures: { lines: '14' } });
  74. const snippet = range(10, 14).map(line => mockSourceLine({ line }));
  75. const wrapper = shallowRender({
  76. component,
  77. snippet
  78. });
  79. expect(wrapper).toMatchSnapshot();
  80. });
  81. it('should correctly handle expansion', () => {
  82. const snippet = range(5, 8).map(line => mockSourceLine({ line }));
  83. const expandBlock = jest.fn(() => Promise.resolve());
  84. const wrapper = shallowRender({
  85. expandBlock,
  86. index: 2,
  87. snippet
  88. });
  89. wrapper
  90. .find('.expand-block-above button')
  91. .first()
  92. .simulate('click');
  93. expect(expandBlock).toHaveBeenCalledWith(2, 'up');
  94. wrapper
  95. .find('.expand-block-below button')
  96. .first()
  97. .simulate('click');
  98. expect(expandBlock).toHaveBeenCalledWith(2, 'down');
  99. });
  100. it('should handle scrolling', () => {
  101. const scroll = jest.fn();
  102. const wrapper = mountRender({ scroll });
  103. const element = {} as HTMLElement;
  104. wrapper.instance().doScroll(element);
  105. expect(scroll).toHaveBeenCalledWith(element);
  106. expect(scrollHorizontally).toHaveBeenCalled();
  107. expect((scrollHorizontally as jest.Mock).mock.calls[0][0]).toBe(element);
  108. });
  109. it('should handle scrolling to expanded row', () => {
  110. const scroll = jest.fn();
  111. const wrapper = mountRender({ scroll });
  112. wrapper.instance().scrollToLastExpandedRow();
  113. expect(scroll).toHaveBeenCalled();
  114. });
  115. function shallowRender(props: Partial<SnippetViewer['props']> = {}) {
  116. return shallow<SnippetViewer>(
  117. <SnippetViewer
  118. component={mockSourceViewerFile()}
  119. duplications={undefined}
  120. duplicationsByLine={undefined}
  121. expandBlock={jest.fn()}
  122. handleCloseIssues={jest.fn()}
  123. handleOpenIssues={jest.fn()}
  124. handleSymbolClick={jest.fn()}
  125. highlightedLocationMessage={{ index: 0, text: '' }}
  126. highlightedSymbols={[]}
  127. index={0}
  128. issue={mockIssue()}
  129. issuesByLine={{}}
  130. lastSnippetOfLastGroup={false}
  131. loadDuplications={jest.fn()}
  132. locations={[]}
  133. locationsByLine={{}}
  134. onLocationSelect={jest.fn()}
  135. openIssuesByLine={{}}
  136. renderDuplicationPopup={jest.fn()}
  137. scroll={jest.fn()}
  138. snippet={[]}
  139. {...props}
  140. />
  141. );
  142. }
  143. function mountRender(props: Partial<SnippetViewer['props']> = {}) {
  144. return mount<SnippetViewer>(
  145. <SnippetViewer
  146. component={mockSourceViewerFile()}
  147. duplications={undefined}
  148. duplicationsByLine={undefined}
  149. expandBlock={jest.fn()}
  150. handleCloseIssues={jest.fn()}
  151. handleOpenIssues={jest.fn()}
  152. handleSymbolClick={jest.fn()}
  153. highlightedLocationMessage={{ index: 0, text: '' }}
  154. highlightedSymbols={[]}
  155. index={0}
  156. issue={mockIssue()}
  157. issuesByLine={{}}
  158. lastSnippetOfLastGroup={false}
  159. loadDuplications={jest.fn()}
  160. locations={[]}
  161. locationsByLine={{}}
  162. onLocationSelect={jest.fn()}
  163. openIssuesByLine={{}}
  164. renderDuplicationPopup={jest.fn()}
  165. scroll={jest.fn()}
  166. snippet={[mockSourceLine()]}
  167. {...props}
  168. />
  169. );
  170. }