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.

PRDecorationBindingRenderer-test.tsx 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2020 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 Select from 'sonar-ui-common/components/controls/Select';
  23. import { waitAndUpdate } from 'sonar-ui-common/helpers/testUtils';
  24. import { AlmKeys } from '../../../../../types/alm-settings';
  25. import PRDecorationBindingRenderer, {
  26. PRDecorationBindingRendererProps
  27. } from '../PRDecorationBindingRenderer';
  28. it('should render correctly', () => {
  29. expect(shallowRender()).toMatchSnapshot();
  30. expect(shallowRender({ loading: false })).toMatchSnapshot();
  31. });
  32. it('should render single instance correctly', () => {
  33. const singleInstance = {
  34. key: 'single',
  35. url: 'http://single.url',
  36. alm: AlmKeys.GitHub
  37. };
  38. expect(
  39. shallowRender({
  40. loading: false,
  41. instances: [singleInstance]
  42. })
  43. ).toMatchSnapshot();
  44. });
  45. it('should render multiple instances correctly', () => {
  46. const urls = ['http://github.enterprise.com', 'http://bbs.enterprise.com'];
  47. const instances = [
  48. {
  49. alm: AlmKeys.GitHub,
  50. key: 'i1',
  51. url: urls[0]
  52. },
  53. {
  54. alm: AlmKeys.GitHub,
  55. key: 'i2',
  56. url: urls[0]
  57. },
  58. {
  59. alm: AlmKeys.Bitbucket,
  60. key: 'i3',
  61. url: urls[1]
  62. },
  63. {
  64. alm: AlmKeys.Azure,
  65. key: 'i4'
  66. }
  67. ];
  68. //unfilled
  69. expect(
  70. shallowRender({
  71. instances,
  72. loading: false
  73. })
  74. ).toMatchSnapshot();
  75. // filled
  76. expect(
  77. shallowRender({
  78. formData: {
  79. key: 'i1',
  80. repository: 'account/repo'
  81. },
  82. instances,
  83. loading: false,
  84. originalData: {
  85. key: 'i1',
  86. repository: 'account/repo'
  87. }
  88. })
  89. ).toMatchSnapshot();
  90. });
  91. it('should display action state correctly', () => {
  92. const urls = ['http://url.com'];
  93. const instances = [{ key: 'key', url: urls[0], alm: AlmKeys.GitHub }];
  94. expect(shallowRender({ instances, loading: false, saving: true })).toMatchSnapshot();
  95. expect(shallowRender({ instances, loading: false, success: true })).toMatchSnapshot();
  96. expect(
  97. shallowRender({
  98. instances,
  99. isValid: true,
  100. loading: false
  101. })
  102. ).toMatchSnapshot();
  103. });
  104. it('should render select options correctly', async () => {
  105. const instances = [
  106. {
  107. alm: AlmKeys.Azure,
  108. key: 'azure'
  109. },
  110. {
  111. alm: AlmKeys.GitHub,
  112. key: 'github',
  113. url: 'gh.url.com'
  114. }
  115. ];
  116. const wrapper = shallowRender({ loading: false, instances });
  117. await waitAndUpdate(wrapper);
  118. const optionRenderer = wrapper.find(Select).prop('optionRenderer');
  119. expect(optionRenderer!(instances[0])).toMatchSnapshot();
  120. expect(optionRenderer!(instances[1])).toMatchSnapshot();
  121. });
  122. function shallowRender(props: Partial<PRDecorationBindingRendererProps> = {}) {
  123. return shallow(
  124. <PRDecorationBindingRenderer
  125. formData={{
  126. key: '',
  127. repository: ''
  128. }}
  129. instances={[]}
  130. isValid={false}
  131. loading={true}
  132. onFieldChange={jest.fn()}
  133. onReset={jest.fn()}
  134. onSubmit={jest.fn()}
  135. originalData={undefined}
  136. saving={false}
  137. success={false}
  138. {...props}
  139. />
  140. );
  141. }