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 { screen } from '@testing-library/react';
21 import userEvent from '@testing-library/user-event';
22 import * as React from 'react';
23 import { mockSetting } from '../../../../../helpers/mocks/settings';
24 import { renderComponent } from '../../../../../helpers/testReactTestingUtils';
25 import { DefaultSpecializedInputProps } from '../../../utils';
26 import InputForFormattedText from '../InputForFormattedText';
28 it('should render correctly with no value for login message', () => {
29 renderInputForFormattedText();
30 expect(screen.getByRole('textbox')).toBeInTheDocument();
33 it('should render correctly with a value for login message', () => {
34 renderInputForFormattedText({
35 setting: mockSetting({ values: ['*text*', 'text'], hasValue: true }),
37 expect(screen.getByRole('button', { name: 'edit' })).toBeInTheDocument();
38 expect(screen.getByText('text')).toBeInTheDocument();
41 it('should render correctly with a value for login message if hasValue is set', () => {
42 renderInputForFormattedText({
43 setting: mockSetting({ hasValue: true }),
45 expect(screen.getByRole('button', { name: 'edit' })).toBeInTheDocument();
48 it('should render editMode when value is empty', () => {
49 renderInputForFormattedText({
52 expect(screen.getByRole('textbox')).toBeInTheDocument();
53 expect(screen.queryByRole('button', { name: 'edit' })).not.toBeInTheDocument();
56 it('should render correctly if in editMode', async () => {
57 const user = userEvent.setup();
58 const onChange = jest.fn();
60 renderInputForFormattedText({
61 setting: mockSetting({ values: ['*text*', 'text'], hasValue: true }),
65 expect(screen.getByRole('textbox')).toBeInTheDocument();
66 expect(screen.queryByRole('button', { name: 'edit' })).not.toBeInTheDocument();
68 await user.click(screen.getByRole('textbox'));
69 await user.keyboard('N');
70 expect(onChange).toHaveBeenCalledTimes(1);
73 function renderInputForFormattedText(props: Partial<DefaultSpecializedInputProps> = {}) {
75 <InputForFormattedText
81 setting={mockSetting({ value: undefined, hasValue: false })}