Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

App-test.tsx 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 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 { addSideBarClass, removeSideBarClass } from 'sonar-ui-common/helpers/pages';
  23. import { request } from 'sonar-ui-common/helpers/request';
  24. import { waitAndUpdate } from 'sonar-ui-common/helpers/testUtils';
  25. import { isSonarCloud } from '../../../../helpers/system';
  26. import getPages from '../../pages';
  27. import App from '../App';
  28. jest.mock('../../../../components/common/ScreenPositionHelper', () => ({
  29. default: class ScreenPositionHelper extends React.Component<{
  30. children: (pos: { top: number }) => React.ReactNode;
  31. }> {
  32. static displayName = 'ScreenPositionHelper';
  33. render() {
  34. return this.props.children({ top: 0 });
  35. }
  36. }
  37. }));
  38. jest.mock('../../../../helpers/system', () => ({
  39. isSonarCloud: jest.fn().mockReturnValue(false)
  40. }));
  41. jest.mock('Docs/../static/SonarQubeNavigationTree.json', () => ({
  42. default: [
  43. {
  44. title: 'SonarQube',
  45. children: [
  46. '/lorem/ipsum/',
  47. '/analysis/languages/csharp/',
  48. {
  49. title: 'Child category',
  50. children: [
  51. '/lorem/ipsum/dolor',
  52. {
  53. title: 'Grandchild category',
  54. children: ['/lorem/ipsum/sit']
  55. },
  56. '/lorem/ipsum/amet'
  57. ]
  58. }
  59. ]
  60. }
  61. ]
  62. }));
  63. jest.mock('Docs/../static/SonarCloudNavigationTree.json', () => ({
  64. default: [
  65. {
  66. title: 'SonarCloud',
  67. children: [
  68. '/lorem/ipsum/',
  69. {
  70. title: 'Child category',
  71. children: [
  72. '/lorem/ipsum/dolor',
  73. {
  74. title: 'Grandchild category',
  75. children: ['/lorem/ipsum/sit']
  76. },
  77. '/lorem/ipsum/amet'
  78. ]
  79. }
  80. ]
  81. }
  82. ]
  83. }));
  84. jest.mock('sonar-ui-common/helpers/pages', () => ({
  85. addSideBarClass: jest.fn(),
  86. removeSideBarClass: jest.fn()
  87. }));
  88. jest.mock('sonar-ui-common/helpers/request', () => {
  89. const { mockDocumentationMarkdown } = require.requireActual('../../../../helpers/testMocks');
  90. return {
  91. request: jest.fn(() => ({
  92. submit: jest.fn().mockResolvedValue({
  93. status: 200,
  94. text: jest.fn().mockResolvedValue(mockDocumentationMarkdown({ key: 'csharp' }))
  95. })
  96. }))
  97. };
  98. });
  99. jest.mock('../../pages', () => {
  100. const { mockDocumentationEntry } = require.requireActual('../../../../helpers/testMocks');
  101. return {
  102. default: jest
  103. .fn()
  104. .mockReturnValue([
  105. mockDocumentationEntry(),
  106. mockDocumentationEntry({ url: '/analysis/languages/csharp/' })
  107. ])
  108. };
  109. });
  110. jest.mock('../../../../api/plugins', () => ({
  111. getInstalledPlugins: jest
  112. .fn()
  113. .mockResolvedValue([
  114. { key: 'csharp', documentationPath: 'static/documentation.md' },
  115. { key: 'vbnet', documentationPath: 'Sstatic/documentation.md' },
  116. { key: 'vbnett', documentationPath: undefined }
  117. ])
  118. }));
  119. beforeEach(() => {
  120. jest.clearAllMocks();
  121. });
  122. it('should render correctly for SonarQube', async () => {
  123. const wrapper = shallowRender();
  124. expect(wrapper.find('DeferredSpinner').exists()).toBe(true);
  125. expect(addSideBarClass).toBeCalled();
  126. await waitAndUpdate(wrapper);
  127. expect(wrapper).toMatchSnapshot();
  128. expect(wrapper.find('ScreenPositionHelper').dive()).toMatchSnapshot();
  129. wrapper.unmount();
  130. expect(removeSideBarClass).toBeCalled();
  131. });
  132. it('should render correctly for SonarCloud', async () => {
  133. (isSonarCloud as jest.Mock).mockReturnValue(true);
  134. const wrapper = shallowRender();
  135. await waitAndUpdate(wrapper);
  136. expect(wrapper).toMatchSnapshot();
  137. });
  138. it("should show a 404 if the page doesn't exist", async () => {
  139. const wrapper = shallowRender({ params: { splat: 'unknown' } });
  140. await waitAndUpdate(wrapper);
  141. expect(wrapper).toMatchSnapshot();
  142. });
  143. it('should try to fetch language plugin documentation if documentationPath matches', async () => {
  144. (isSonarCloud as jest.Mock).mockReturnValue(false);
  145. const wrapper = shallowRender();
  146. await waitAndUpdate(wrapper);
  147. expect(request).toHaveBeenCalledWith('/static/csharp/documentation.md');
  148. expect(request).not.toHaveBeenCalledWith('/static/vbnet/documentation.md');
  149. expect(request).not.toHaveBeenCalledWith('/static/vbnett/documentation.md');
  150. expect(getPages).toHaveBeenCalledWith(
  151. expect.objectContaining({
  152. 'analysis/languages/csharp': expect.any(Object)
  153. })
  154. );
  155. });
  156. function shallowRender(props: Partial<App['props']> = {}) {
  157. return shallow(<App params={{ splat: 'lorem/ipsum' }} {...props} />);
  158. }