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 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2023 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. createWebhook,
  24. deleteWebhook,
  25. searchWebhooks,
  26. updateWebhook,
  27. } from '../../../../api/webhooks';
  28. import { mockComponent } from '../../../../helpers/mocks/component';
  29. import { App } from '../App';
  30. jest.mock('../../../../api/webhooks', () => ({
  31. createWebhook: jest.fn(() =>
  32. Promise.resolve({ webhook: { key: '3', name: 'baz', url: 'http://baz', hasSecret: false } })
  33. ),
  34. deleteWebhook: jest.fn(() => Promise.resolve()),
  35. searchWebhooks: jest.fn(() =>
  36. Promise.resolve({
  37. webhooks: [
  38. { key: '1', name: 'foo', url: 'http://foo', hasSecret: false },
  39. { key: '2', name: 'bar', url: 'http://bar', hasSecret: false },
  40. ],
  41. })
  42. ),
  43. updateWebhook: jest.fn(() => Promise.resolve()),
  44. }));
  45. const component = mockComponent({ key: 'bar', qualifier: 'TRK' });
  46. beforeEach(() => {
  47. (createWebhook as jest.Mock<any>).mockClear();
  48. (deleteWebhook as jest.Mock<any>).mockClear();
  49. (searchWebhooks as jest.Mock<any>).mockClear();
  50. (updateWebhook as jest.Mock<any>).mockClear();
  51. });
  52. it('should be in loading status', () => {
  53. expect(shallow(<App />)).toMatchSnapshot();
  54. });
  55. it('should fetch webhooks and display them', async () => {
  56. const wrapper = shallow(<App />);
  57. expect(wrapper.state('loading')).toBe(true);
  58. await new Promise(setImmediate);
  59. expect(searchWebhooks).toHaveBeenCalledWith({});
  60. wrapper.update();
  61. expect(wrapper).toMatchSnapshot();
  62. });
  63. describe('should correctly fetch webhooks when', () => {
  64. it('on global scope', async () => {
  65. shallow(<App />);
  66. await new Promise(setImmediate);
  67. expect(searchWebhooks).toHaveBeenCalledWith({ projects: undefined });
  68. });
  69. it('on project scope', async () => {
  70. shallow(<App component={component} />);
  71. await new Promise(setImmediate);
  72. expect(searchWebhooks).toHaveBeenCalledWith({
  73. project: component.key,
  74. });
  75. });
  76. });
  77. it('should correctly handle webhook creation', async () => {
  78. const webhook = { name: 'baz', url: 'http://baz' };
  79. const wrapper = shallow(<App />);
  80. (wrapper.instance() as App).handleCreate({ ...webhook });
  81. expect(createWebhook).toHaveBeenLastCalledWith({
  82. ...webhook,
  83. project: undefined,
  84. });
  85. await new Promise(setImmediate);
  86. wrapper.update();
  87. expect(wrapper.state('webhooks')).toEqual([
  88. { key: '1', name: 'foo', url: 'http://foo', hasSecret: false },
  89. { key: '2', name: 'bar', url: 'http://bar', hasSecret: false },
  90. { key: '3', name: 'baz', url: 'http://baz', hasSecret: false },
  91. ]);
  92. });
  93. it('should correctly handle webhook deletion', async () => {
  94. const wrapper = shallow(<App />);
  95. (wrapper.instance() as App).handleDelete('2');
  96. expect(deleteWebhook).toHaveBeenLastCalledWith({ webhook: '2' });
  97. await new Promise(setImmediate);
  98. wrapper.update();
  99. expect(wrapper.state('webhooks')).toEqual([
  100. { key: '1', name: 'foo', url: 'http://foo', hasSecret: false },
  101. ]);
  102. });
  103. it('should correctly handle webhook update', async () => {
  104. const newValues = { webhook: '1', name: 'Cfoo', url: 'http://cfoo', secret: undefined };
  105. const wrapper = shallow(<App />);
  106. (wrapper.instance() as App).handleUpdate(newValues);
  107. expect(updateWebhook).toHaveBeenLastCalledWith(newValues);
  108. await new Promise(setImmediate);
  109. wrapper.update();
  110. expect(wrapper.state('webhooks')).toEqual([
  111. { key: '1', name: 'Cfoo', url: 'http://cfoo', hasSecret: false },
  112. { key: '2', name: 'bar', url: 'http://bar', hasSecret: false },
  113. ]);
  114. });
  115. it('should correctly handle webhook secret update', async () => {
  116. const newValuesWithSecret = { webhook: '2', name: 'bar', url: 'http://bar', secret: 'secret' };
  117. const newValuesWithoutSecret = {
  118. webhook: '2',
  119. name: 'bar',
  120. url: 'http://bar',
  121. secret: undefined,
  122. };
  123. const newValuesWithEmptySecret = { webhook: '2', name: 'bar', url: 'http://bar', secret: '' };
  124. const wrapper = shallow(<App />);
  125. // With secret
  126. (wrapper.instance() as App).handleUpdate(newValuesWithSecret);
  127. expect(updateWebhook).toHaveBeenLastCalledWith(newValuesWithSecret);
  128. await new Promise(setImmediate);
  129. wrapper.update();
  130. expect(wrapper.state('webhooks')).toEqual([
  131. { key: '1', name: 'foo', url: 'http://foo', hasSecret: false },
  132. { key: '2', name: 'bar', url: 'http://bar', hasSecret: true },
  133. ]);
  134. // Without secret
  135. (wrapper.instance() as App).handleUpdate(newValuesWithoutSecret);
  136. expect(updateWebhook).toHaveBeenLastCalledWith(newValuesWithoutSecret);
  137. await new Promise(setImmediate);
  138. wrapper.update();
  139. expect(wrapper.state('webhooks')).toEqual([
  140. { key: '1', name: 'foo', url: 'http://foo', hasSecret: false },
  141. { key: '2', name: 'bar', url: 'http://bar', hasSecret: true },
  142. ]);
  143. // With empty secret
  144. (wrapper.instance() as App).handleUpdate(newValuesWithEmptySecret);
  145. expect(updateWebhook).toHaveBeenLastCalledWith(newValuesWithEmptySecret);
  146. await new Promise(setImmediate);
  147. wrapper.update();
  148. expect(wrapper.state('webhooks')).toEqual([
  149. { key: '1', name: 'foo', url: 'http://foo', hasSecret: false },
  150. { key: '2', name: 'bar', url: 'http://bar', hasSecret: false },
  151. ]);
  152. });