/** *******************************************************************************************************************
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License").
You may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. *
******************************************************************************************************************** */
import { render, screen, cleanup, act, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import wrapper from '@cloudscape-design/components/test-utils/dom';
import DeleteConfirmationDialog from '.';
describe('DeleteConfirmationDialog', () => {
afterEach(cleanup);
it('should renders title and children', () => {
render(information);
expect(screen.getByText('dialog title')).toBeInTheDocument();
expect(screen.getByText('information')).toBeInTheDocument();
});
it('should be invisible by default', async () => {
render(
information
);
const element = await screen.findByTestId('testId');
const modal = wrapper(element).findModal();
expect(modal?.isVisible).toBeFalsy();
});
it('should be visible when visible is true', () => {
render(
information
);
expect(screen.getByText('dialog title')).toBeVisible();
expect(screen.getByText('information')).toBeVisible();
expect(screen.getByText('custom label')).toBeVisible();
expect(screen.getByText('custom hint text')).toBeVisible();
expect(screen.getByPlaceholderText('delete')).toBeInTheDocument();
});
it('should trigger onCancelClicked when clicking the close icon button', async () => {
const handleClose = jest.fn();
render(
information
);
await act(async () => {
await userEvent.click(screen.getAllByRole('button')[0]);
});
expect(handleClose).toHaveBeenCalled();
});
it('should trigger onCancelClicked when clicking the Cancel button', async () => {
const handleClose = jest.fn();
render(
information
);
await act(async () => {
await userEvent.click(screen.getByText('Cancel'));
});
expect(handleClose).toHaveBeenCalled();
});
it('should trigger onDeleteClicked when clicking the Delete button', async () => {
const handleClose = jest.fn();
const handleDelete = jest.fn();
render(
information
);
expect(screen.getByText('Delete').parentNode).toBeEnabled();
await act(async () => {
await userEvent.click(screen.getByText('Delete'));
});
expect(handleDelete).toHaveBeenCalled();
});
it('should enable the Delete button when the confirmation text is typed', async () => {
const handleClose = jest.fn();
const handleDelete = jest.fn();
render(
information
);
expect(screen.getByText('Delete').parentNode).toBeDisabled();
await act(async () => {
await userEvent.type(screen.getByPlaceholderText('delete'), 'del');
});
expect(screen.getByText('Delete').parentNode).toBeDisabled();
await act(async () => {
await userEvent.type(screen.getByPlaceholderText('delete'), 'ete');
});
await waitFor(() => expect(screen.getByText('Delete').parentNode).toBeEnabled());
await act(async () => {
await userEvent.click(screen.getByText('Delete'));
});
expect(handleDelete).toHaveBeenCalled();
});
it('should use custom confirmation text', async () => {
const handleClose = jest.fn();
const handleDelete = jest.fn();
const customConfirmationText = 'order id';
render(
information
);
expect(screen.getByText('Delete').parentNode).toBeDisabled();
await act(async () => {
await userEvent.type(screen.getByPlaceholderText(customConfirmationText), customConfirmationText);
});
expect(screen.getByText('Delete').parentNode).toBeEnabled();
await act(async () => {
await userEvent.click(screen.getByText('Delete'));
});
expect(handleDelete).toHaveBeenCalled();
});
it('should render loading state when loading is true', async () => {
render(
information
);
await act(async () => {
await userEvent.type(screen.getByPlaceholderText('delete'), 'delete');
});
expect(screen.getByText('Delete').parentNode).toBeDisabled();
const element = await screen.findByTestId('testId');
const button = wrapper(element).findButton('[aria-label="delete"]');
expect(button?.findLoadingIndicator()).not.toBeNull();
});
it('should render custom Delete button text', () => {
const { getByText } = render(
information
);
expect(getByText('Confirm')).toBeInTheDocument();
});
it('should render Delete button disabled when enabled is false', () => {
const handleClose = jest.fn();
const handleDelete = jest.fn();
const { getByText } = render(
information
);
expect(getByText('Delete').parentNode).toBeDisabled();
});
});