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.

CreateWebhookForm-it.tsx 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 userEvent from '@testing-library/user-event';
  21. import * as React from 'react';
  22. import { byLabelText, byRole } from 'testing-library-selector';
  23. import { renderComponent } from '../../../../helpers/testReactTestingUtils';
  24. import CreateWebhookForm from '../CreateWebhookForm';
  25. const ui = {
  26. nameInput: byRole('textbox', { name: 'webhooks.name field_required' }),
  27. urlInput: byRole('textbox', { name: 'webhooks.url field_required' }),
  28. secretInput: byLabelText('webhooks.secret'),
  29. secretInputMaskButton: byRole('button', { name: 'webhooks.secret.field_mask.link' }),
  30. createButton: byRole('button', { name: 'create' }),
  31. updateButton: byRole('button', { name: 'update_verb' }),
  32. };
  33. describe('Webhook form', () => {
  34. it('should correctly submit creation form', async () => {
  35. const user = userEvent.setup();
  36. const webhook = {
  37. name: 'foo',
  38. url: 'http://bar',
  39. secret: '',
  40. };
  41. const onDone = jest.fn();
  42. renderCreateWebhookForm({ onDone });
  43. expect(ui.nameInput.get()).toHaveValue('');
  44. expect(ui.urlInput.get()).toHaveValue('');
  45. expect(ui.secretInput.get()).toHaveValue('');
  46. expect(ui.createButton.get()).toBeDisabled();
  47. await user.type(ui.nameInput.get(), webhook.name);
  48. await user.type(ui.urlInput.get(), webhook.url);
  49. expect(ui.createButton.get()).toBeEnabled();
  50. await user.click(ui.createButton.get());
  51. expect(onDone).toHaveBeenCalledWith(webhook);
  52. });
  53. it('should correctly submit update form', async () => {
  54. const user = userEvent.setup();
  55. const webhook = {
  56. hasSecret: false,
  57. key: 'test-webhook-key',
  58. name: 'foo',
  59. url: 'http://bar',
  60. };
  61. const nameExtension = 'bar';
  62. const url = 'http://bar';
  63. const onDone = jest.fn();
  64. renderCreateWebhookForm({ onDone, webhook });
  65. expect(ui.nameInput.get()).toHaveValue(webhook.name);
  66. expect(ui.urlInput.get()).toHaveValue(webhook.url);
  67. expect(ui.secretInput.query()).not.toBeInTheDocument();
  68. expect(ui.secretInputMaskButton.get()).toBeInTheDocument();
  69. expect(ui.updateButton.get()).toBeDisabled();
  70. await user.type(ui.nameInput.get(), nameExtension);
  71. await user.clear(ui.urlInput.get());
  72. await user.type(ui.urlInput.get(), url);
  73. expect(ui.updateButton.get()).toBeEnabled();
  74. await user.click(ui.updateButton.get());
  75. expect(onDone).toHaveBeenCalledWith({
  76. name: `${webhook.name}${nameExtension}`,
  77. url,
  78. secret: undefined,
  79. });
  80. });
  81. it('should correctly submit update form with empty secret', async () => {
  82. const user = userEvent.setup();
  83. const webhook = {
  84. hasSecret: false,
  85. key: 'test-webhook-key',
  86. name: 'foo',
  87. url: 'http://bar',
  88. };
  89. const onDone = jest.fn();
  90. renderCreateWebhookForm({ onDone, webhook });
  91. await user.click(ui.secretInputMaskButton.get());
  92. expect(ui.updateButton.get()).toBeEnabled();
  93. await user.click(ui.updateButton.get());
  94. expect(onDone).toHaveBeenCalledWith({
  95. name: webhook.name,
  96. url: webhook.url,
  97. secret: '',
  98. });
  99. });
  100. it('should correctly submit update form with updated secret', async () => {
  101. const user = userEvent.setup();
  102. const webhook = {
  103. hasSecret: false,
  104. key: 'test-webhook-key',
  105. name: 'foo',
  106. url: 'http://bar',
  107. };
  108. const secret = 'test-webhook-secret';
  109. const onDone = jest.fn();
  110. renderCreateWebhookForm({ onDone, webhook });
  111. await user.click(ui.secretInputMaskButton.get());
  112. await user.type(ui.secretInput.get(), secret);
  113. await user.click(ui.updateButton.get());
  114. expect(onDone).toHaveBeenCalledWith({
  115. name: webhook.name,
  116. url: webhook.url,
  117. secret,
  118. });
  119. });
  120. });
  121. function renderCreateWebhookForm(props = {}) {
  122. return renderComponent(
  123. <CreateWebhookForm onClose={jest.fn()} onDone={jest.fn(() => Promise.resolve())} {...props} />
  124. );
  125. }