3 * Copyright (C) 2009-2023 SonarSource SA
4 * mailto:info AT sonarsource DOT com
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.
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.
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.
20 import userEvent from '@testing-library/user-event';
21 import React from 'react';
22 import AlmSettingsServiceMock from '../../../../../api/mocks/AlmSettingsServiceMock';
23 import { renderComponent } from '../../../../../helpers/testReactTestingUtils';
24 import { byRole, byText } from '../../../../../helpers/testSelector';
25 import { AlmKeys } from '../../../../../types/alm-settings';
26 import AlmBindingDefinitionForm, {
27 AlmBindingDefinitionFormProps,
28 } from '../AlmBindingDefinitionForm';
30 jest.mock('../../../../../api/alm-settings');
32 let almSettings: AlmSettingsServiceMock;
35 almSettings = new AlmSettingsServiceMock();
43 bitbucketConfiguration: (almKey: AlmKeys.BitbucketCloud | AlmKeys.BitbucketServer) =>
44 byRole('button', { name: `alm.${almKey}.long` }),
45 configurationInput: (id: string) =>
46 byRole('textbox', { name: `settings.almintegration.form.${id}` }),
47 saveConfigurationButton: byRole('button', { name: 'settings.almintegration.form.save' }),
48 cancelButton: byRole('button', { name: 'cancel' }),
49 validationError: (text: string) => byText(text),
52 const onCancel = jest.fn();
54 it('enforceValidation enabled', async () => {
55 almSettings.setDefinitionErrorMessage('Validation Error');
56 renderAlmBindingDefinitionForm();
59 await userEvent.type(await ui.configurationInput('name.gitlab').find(), 'Name');
60 await userEvent.type(ui.configurationInput('url.gitlab').get(), 'https://api.alm.com');
61 await userEvent.type(ui.configurationInput('personal_access_token').get(), 'Access Token');
63 await userEvent.click(ui.saveConfigurationButton.get());
64 expect(ui.validationError('Validation Error').get()).toBeInTheDocument();
66 await userEvent.click(ui.cancelButton.get());
67 expect(onCancel).toHaveBeenCalled();
70 function renderAlmBindingDefinitionForm(props: Partial<AlmBindingDefinitionFormProps> = {}) {
71 return renderComponent(
72 <AlmBindingDefinitionForm
74 afterSubmit={jest.fn()}