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.

App-test.tsx 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2018 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. /* eslint-disable import/first, import/order */
  21. jest.mock('../../../api/quality-gates', () => ({
  22. associateGateWithProject: jest.fn(() => Promise.resolve()),
  23. dissociateGateWithProject: jest.fn(() => Promise.resolve()),
  24. fetchQualityGates: jest.fn(() => Promise.resolve({})),
  25. getGateForProject: jest.fn(() => Promise.resolve())
  26. }));
  27. jest.mock('../../../app/utils/addGlobalSuccessMessage', () => ({
  28. default: jest.fn()
  29. }));
  30. jest.mock('../../../app/utils/handleRequiredAuthorization', () => ({
  31. default: jest.fn()
  32. }));
  33. import * as React from 'react';
  34. import { mount } from 'enzyme';
  35. import App from '../App';
  36. import { Component } from '../../../app/types';
  37. const associateGateWithProject = require('../../../api/quality-gates')
  38. .associateGateWithProject as jest.Mock<any>;
  39. const dissociateGateWithProject = require('../../../api/quality-gates')
  40. .dissociateGateWithProject as jest.Mock<any>;
  41. const fetchQualityGates = require('../../../api/quality-gates').fetchQualityGates as jest.Mock<any>;
  42. const getGateForProject = require('../../../api/quality-gates').getGateForProject as jest.Mock<any>;
  43. const addGlobalSuccessMessage = require('../../../app/utils/addGlobalSuccessMessage')
  44. .default as jest.Mock<any>;
  45. const handleRequiredAuthorization = require('../../../app/utils/handleRequiredAuthorization')
  46. .default as jest.Mock<any>;
  47. const component = {
  48. analysisDate: '',
  49. breadcrumbs: [],
  50. configuration: { showQualityGates: true },
  51. key: 'component',
  52. name: 'component',
  53. organization: 'org',
  54. qualifier: 'TRK',
  55. version: '0.0.1'
  56. } as Component;
  57. beforeEach(() => {
  58. associateGateWithProject.mockClear();
  59. dissociateGateWithProject.mockClear();
  60. addGlobalSuccessMessage.mockClear();
  61. });
  62. it('checks permissions', () => {
  63. handleRequiredAuthorization.mockClear();
  64. mount(
  65. <App
  66. component={{ ...component, configuration: undefined } as Component}
  67. onComponentChange={jest.fn()}
  68. />
  69. );
  70. expect(handleRequiredAuthorization).toBeCalled();
  71. });
  72. it('fetches quality gates', () => {
  73. fetchQualityGates.mockClear();
  74. getGateForProject.mockClear();
  75. mount(<App component={component} onComponentChange={jest.fn()} />);
  76. expect(fetchQualityGates).toBeCalledWith({ organization: 'org' });
  77. expect(getGateForProject).toBeCalledWith({ organization: 'org', project: 'component' });
  78. });
  79. it('changes quality gate from custom to default', () => {
  80. const gate = randomGate('foo');
  81. const allGates = [gate, randomGate('bar', true), randomGate('baz')];
  82. const wrapper = mountRender(allGates, gate);
  83. wrapper.find('Form').prop<Function>('onChange')('foo', 'bar');
  84. expect(associateGateWithProject).toBeCalledWith({
  85. gateId: 'bar',
  86. organization: 'org',
  87. projectKey: 'component'
  88. });
  89. });
  90. it('changes quality gate from custom to custom', () => {
  91. const allGates = [randomGate('foo'), randomGate('bar', true), randomGate('baz')];
  92. const wrapper = mountRender(allGates, randomGate('foo'));
  93. wrapper.find('Form').prop<Function>('onChange')('foo', 'baz');
  94. expect(associateGateWithProject).toBeCalledWith({
  95. gateId: 'baz',
  96. organization: 'org',
  97. projectKey: 'component'
  98. });
  99. });
  100. it('changes quality gate from custom to none', () => {
  101. const allGates = [randomGate('foo'), randomGate('bar'), randomGate('baz')];
  102. const wrapper = mountRender(allGates, randomGate('foo'));
  103. wrapper.find('Form').prop<Function>('onChange')('foo', undefined);
  104. expect(dissociateGateWithProject).toBeCalledWith({
  105. gateId: 'foo',
  106. organization: 'org',
  107. projectKey: 'component'
  108. });
  109. });
  110. it('changes quality gate from none to custom', () => {
  111. const allGates = [randomGate('foo'), randomGate('bar'), randomGate('baz')];
  112. const wrapper = mountRender(allGates);
  113. wrapper.find('Form').prop<Function>('onChange')(undefined, 'baz');
  114. expect(associateGateWithProject).toBeCalledWith({
  115. gateId: 'baz',
  116. organization: 'org',
  117. projectKey: 'component'
  118. });
  119. });
  120. function randomGate(id: string, isDefault = false) {
  121. return { id, isDefault, name: id };
  122. }
  123. function mountRender(allGates: any[], gate?: any) {
  124. const wrapper = mount(<App component={component} onComponentChange={jest.fn()} />);
  125. wrapper.setState({ allGates, loading: false, gate });
  126. return wrapper;
  127. }