mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
ConfirmModal: Ignore case for confirmation text (#69000)
* ignore case for confirmation text, add a couple of unit tests * just have 1 test * fix related unit tests
This commit is contained in:
parent
3647392b40
commit
fd7f2d4a5e
@ -1,4 +1,5 @@
|
|||||||
import { render, screen } from '@testing-library/react';
|
import { render, screen } from '@testing-library/react';
|
||||||
|
import userEvent from '@testing-library/user-event';
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
|
|
||||||
import { ConfirmModal } from './ConfirmModal';
|
import { ConfirmModal } from './ConfirmModal';
|
||||||
@ -44,4 +45,47 @@ describe('ConfirmModal', () => {
|
|||||||
expect(screen.queryByRole('button', { name: 'Alternative Text' })).not.toBeInTheDocument();
|
expect(screen.queryByRole('button', { name: 'Alternative Text' })).not.toBeInTheDocument();
|
||||||
expect(screen.queryByRole('button', { name: 'Confirm' })).not.toBeInTheDocument();
|
expect(screen.queryByRole('button', { name: 'Confirm' })).not.toBeInTheDocument();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('disables the confirm button initially when confirmation text is present', () => {
|
||||||
|
render(
|
||||||
|
<ConfirmModal
|
||||||
|
title="Some Title"
|
||||||
|
body="Some Body"
|
||||||
|
confirmText="Please Confirm"
|
||||||
|
alternativeText="Alternative Text"
|
||||||
|
dismissText="Dismiss Text"
|
||||||
|
isOpen={true}
|
||||||
|
confirmationText="My confirmation text"
|
||||||
|
onConfirm={() => {}}
|
||||||
|
onDismiss={() => {}}
|
||||||
|
onAlternative={() => {}}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(screen.getByRole('button', { name: 'Please Confirm' })).toBeInTheDocument();
|
||||||
|
expect(screen.getByRole('button', { name: 'Please Confirm' })).toBeDisabled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('typing the confirmation text should enable the confirm button regardless of case', async () => {
|
||||||
|
render(
|
||||||
|
<ConfirmModal
|
||||||
|
title="Some Title"
|
||||||
|
body="Some Body"
|
||||||
|
confirmText="Please Confirm"
|
||||||
|
alternativeText="Alternative Text"
|
||||||
|
dismissText="Dismiss Text"
|
||||||
|
isOpen={true}
|
||||||
|
confirmationText="My confirmation text"
|
||||||
|
onConfirm={() => {}}
|
||||||
|
onDismiss={() => {}}
|
||||||
|
onAlternative={() => {}}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(screen.getByRole('button', { name: 'Please Confirm' })).toBeInTheDocument();
|
||||||
|
expect(screen.getByRole('button', { name: 'Please Confirm' })).toBeDisabled();
|
||||||
|
|
||||||
|
await userEvent.type(screen.getByPlaceholderText('Type "My confirmation text" to confirm'), 'mY CoNfIrMaTiOn TeXt');
|
||||||
|
expect(screen.getByRole('button', { name: 'Please Confirm' })).not.toBeDisabled();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
@ -67,7 +67,7 @@ export const ConfirmModal = ({
|
|||||||
const styles = useStyles2(getStyles);
|
const styles = useStyles2(getStyles);
|
||||||
const buttonRef = useRef<HTMLButtonElement>(null);
|
const buttonRef = useRef<HTMLButtonElement>(null);
|
||||||
const onConfirmationTextChange = (event: React.FormEvent<HTMLInputElement>) => {
|
const onConfirmationTextChange = (event: React.FormEvent<HTMLInputElement>) => {
|
||||||
setDisabled(confirmationText?.localeCompare(event.currentTarget.value) !== 0);
|
setDisabled(confirmationText?.toLowerCase().localeCompare(event.currentTarget.value.toLowerCase()) !== 0);
|
||||||
};
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@ -91,7 +91,7 @@ export const ConfirmModal = ({
|
|||||||
{confirmationText ? (
|
{confirmationText ? (
|
||||||
<div className={styles.modalConfirmationInput}>
|
<div className={styles.modalConfirmationInput}>
|
||||||
<HorizontalGroup>
|
<HorizontalGroup>
|
||||||
<Input placeholder={`Type ${confirmationText} to confirm`} onChange={onConfirmationTextChange} />
|
<Input placeholder={`Type "${confirmationText}" to confirm`} onChange={onConfirmationTextChange} />
|
||||||
</HorizontalGroup>
|
</HorizontalGroup>
|
||||||
</div>
|
</div>
|
||||||
) : null}
|
) : null}
|
||||||
|
@ -46,7 +46,7 @@ describe('browse-dashboards DeleteModal', () => {
|
|||||||
it('only enables the `Delete` button if the confirmation text is typed', async () => {
|
it('only enables the `Delete` button if the confirmation text is typed', async () => {
|
||||||
render(<DeleteModal {...defaultProps} />);
|
render(<DeleteModal {...defaultProps} />);
|
||||||
|
|
||||||
const confirmationInput = await screen.findByPlaceholderText('Type Delete to confirm');
|
const confirmationInput = await screen.findByPlaceholderText('Type "Delete" to confirm');
|
||||||
await userEvent.type(confirmationInput, 'Delete');
|
await userEvent.type(confirmationInput, 'Delete');
|
||||||
|
|
||||||
expect(await screen.findByRole('button', { name: 'Delete' })).toBeEnabled();
|
expect(await screen.findByRole('button', { name: 'Delete' })).toBeEnabled();
|
||||||
@ -55,7 +55,7 @@ describe('browse-dashboards DeleteModal', () => {
|
|||||||
it('calls onConfirm when clicking the `Delete` button', async () => {
|
it('calls onConfirm when clicking the `Delete` button', async () => {
|
||||||
render(<DeleteModal {...defaultProps} />);
|
render(<DeleteModal {...defaultProps} />);
|
||||||
|
|
||||||
const confirmationInput = await screen.findByPlaceholderText('Type Delete to confirm');
|
const confirmationInput = await screen.findByPlaceholderText('Type "Delete" to confirm');
|
||||||
await userEvent.type(confirmationInput, 'Delete');
|
await userEvent.type(confirmationInput, 'Delete');
|
||||||
|
|
||||||
await userEvent.click(await screen.findByRole('button', { name: 'Delete' }));
|
await userEvent.click(await screen.findByRole('button', { name: 'Delete' }));
|
||||||
|
@ -16,7 +16,7 @@ describe('ConfirmModal', () => {
|
|||||||
expect(screen.getByRole('button', { name: 'Cancel' })).toBeInTheDocument();
|
expect(screen.getByRole('button', { name: 'Cancel' })).toBeInTheDocument();
|
||||||
expect(screen.getByRole('button', { name: 'Delete' })).toBeInTheDocument();
|
expect(screen.getByRole('button', { name: 'Delete' })).toBeInTheDocument();
|
||||||
|
|
||||||
expect(screen.queryByPlaceholderText('Type delete to confirm')).not.toBeInTheDocument();
|
expect(screen.queryByPlaceholderText('Type "delete" to confirm')).not.toBeInTheDocument();
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('with nestedFolders feature flag', () => {
|
describe('with nestedFolders feature flag', () => {
|
||||||
@ -39,7 +39,7 @@ describe('ConfirmModal', () => {
|
|||||||
|
|
||||||
render(<ConfirmDeleteModal onDeleteItems={() => {}} results={selectedItems} onDismiss={() => {}} />);
|
render(<ConfirmDeleteModal onDeleteItems={() => {}} results={selectedItems} onDismiss={() => {}} />);
|
||||||
|
|
||||||
expect(screen.getByPlaceholderText('Type delete to confirm')).toBeInTheDocument();
|
expect(screen.getByPlaceholderText('Type "delete" to confirm')).toBeInTheDocument();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user