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.

Menu-test.tsx 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 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 {
  23. mockBranch,
  24. mockMainBranch,
  25. mockPullRequest
  26. } from '../../../../../helpers/mocks/branch-like';
  27. import { mockComponent } from '../../../../../helpers/mocks/component';
  28. import { ComponentQualifier } from '../../../../../types/component';
  29. import { Menu } from '../Menu';
  30. const mainBranch = mockMainBranch();
  31. const baseComponent = mockComponent({
  32. analysisDate: '2019-12-01',
  33. key: 'foo',
  34. name: 'foo'
  35. });
  36. it('should work with extensions', () => {
  37. const component = {
  38. ...baseComponent,
  39. configuration: { showSettings: true, extensions: [{ key: 'foo', name: 'Foo' }] },
  40. extensions: [{ key: 'component-foo', name: 'ComponentFoo' }]
  41. };
  42. const wrapper = shallowRender({ component });
  43. expect(wrapper.find('Dropdown[data-test="extensions"]')).toMatchSnapshot();
  44. expect(wrapper.find('Dropdown[data-test="administration"]')).toMatchSnapshot();
  45. });
  46. it('should work with multiple extensions', () => {
  47. const component = {
  48. ...baseComponent,
  49. configuration: {
  50. showSettings: true,
  51. extensions: [
  52. { key: 'foo', name: 'Foo' },
  53. { key: 'bar', name: 'Bar' }
  54. ]
  55. },
  56. extensions: [
  57. { key: 'component-foo', name: 'ComponentFoo' },
  58. { key: 'component-bar', name: 'ComponentBar' }
  59. ]
  60. };
  61. const wrapper = shallowRender({ component });
  62. expect(wrapper.find('Dropdown[data-test="extensions"]')).toMatchSnapshot();
  63. expect(wrapper.find('Dropdown[data-test="administration"]')).toMatchSnapshot();
  64. });
  65. it('should render correctly for security extensions', () => {
  66. const component = {
  67. ...baseComponent,
  68. configuration: {
  69. showSettings: true,
  70. extensions: [
  71. { key: 'securityreport/foo', name: 'Foo' },
  72. { key: 'bar', name: 'Bar' }
  73. ]
  74. },
  75. extensions: [
  76. { key: 'securityreport/foo', name: 'ComponentFoo' },
  77. { key: 'component-bar', name: 'ComponentBar' }
  78. ]
  79. };
  80. const wrapper = shallowRender({ component });
  81. expect(wrapper.find('Dropdown[data-test="extensions"]')).toMatchSnapshot();
  82. expect(wrapper.find('Dropdown[data-test="security"]')).toMatchSnapshot();
  83. });
  84. it('should work for a branch', () => {
  85. const branchLike = mockBranch({
  86. name: 'release'
  87. });
  88. [true, false].forEach(showSettings =>
  89. expect(
  90. shallowRender({
  91. branchLike,
  92. component: {
  93. ...baseComponent,
  94. configuration: { showSettings },
  95. extensions: [{ key: 'component-foo', name: 'ComponentFoo' }]
  96. }
  97. })
  98. ).toMatchSnapshot()
  99. );
  100. });
  101. it('should work for pull requests', () => {
  102. [true, false].forEach(showSettings =>
  103. expect(
  104. shallowRender({
  105. branchLike: mockPullRequest(),
  106. component: {
  107. ...baseComponent,
  108. configuration: { showSettings },
  109. extensions: [{ key: 'component-foo', name: 'ComponentFoo' }]
  110. }
  111. })
  112. ).toMatchSnapshot()
  113. );
  114. });
  115. it('should work for all qualifiers', () => {
  116. [
  117. ComponentQualifier.Project,
  118. ComponentQualifier.Portfolio,
  119. ComponentQualifier.SubPortfolio,
  120. ComponentQualifier.Application
  121. ].forEach(checkWithQualifier);
  122. expect.assertions(4);
  123. function checkWithQualifier(qualifier: string) {
  124. const component = {
  125. ...baseComponent,
  126. canBrowseAllChildProjects: true,
  127. configuration: { showSettings: true },
  128. qualifier
  129. };
  130. expect(shallowRender({ component })).toMatchSnapshot();
  131. }
  132. });
  133. it('should disable links if no analysis has been done', () => {
  134. expect(
  135. shallowRender({
  136. component: {
  137. ...baseComponent,
  138. analysisDate: undefined
  139. }
  140. })
  141. ).toMatchSnapshot();
  142. });
  143. it('should disable links if application has inaccessible projects', () => {
  144. expect(
  145. shallowRender({
  146. component: {
  147. ...baseComponent,
  148. qualifier: ComponentQualifier.Application,
  149. canBrowseAllChildProjects: false,
  150. configuration: { showSettings: true }
  151. }
  152. })
  153. ).toMatchSnapshot();
  154. });
  155. function shallowRender(props: Partial<Menu['props']>) {
  156. return shallow<Menu>(
  157. <Menu
  158. appState={{ branchesEnabled: true }}
  159. branchLike={mainBranch}
  160. branchLikes={[mainBranch]}
  161. component={baseComponent}
  162. isInProgress={false}
  163. isPending={false}
  164. onToggleProjectInfo={jest.fn()}
  165. {...props}
  166. />
  167. );
  168. }