-
Notifications
You must be signed in to change notification settings - Fork 308
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(content-explorer): Migrate deleteConfirmationDialog
- Loading branch information
1 parent
a426f57
commit 92eb0e3
Showing
9 changed files
with
186 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
62 changes: 62 additions & 0 deletions
62
src/elements/content-explorer/DeleteConfirmationDialog.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import * as React from 'react'; | ||
import { FormattedMessage, useIntl } from 'react-intl'; | ||
import Modal from 'react-modal'; | ||
import { Button } from '@box/blueprint-web'; | ||
import type { BoxItem } from '../../common/types/core'; | ||
|
||
import { CLASS_MODAL_CONTENT, CLASS_MODAL_OVERLAY, CLASS_MODAL, TYPE_FOLDER } from '../../constants'; | ||
|
||
import messages from '../common/messages'; | ||
|
||
export interface DeleteConfirmationDialogProps { | ||
appElement: HTMLElement; | ||
isLoading: boolean; | ||
isOpen: boolean; | ||
item: BoxItem; | ||
onCancel: () => void; | ||
onDelete: () => void; | ||
parentElement: HTMLElement; | ||
} | ||
|
||
const DeleteConfirmationDialog = ({ | ||
appElement, | ||
isLoading, | ||
isOpen, | ||
item, | ||
onCancel, | ||
onDelete, | ||
parentElement, | ||
}: DeleteConfirmationDialogProps) => { | ||
const { formatMessage } = useIntl(); | ||
const message = item.type === TYPE_FOLDER ? messages.deleteDialogFolderText : messages.deleteDialogFileText; | ||
return ( | ||
<Modal | ||
appElement={appElement} | ||
className={CLASS_MODAL_CONTENT} | ||
contentLabel={formatMessage(messages.deleteDialogLabel)} | ||
isOpen={isOpen} | ||
onRequestClose={onCancel} | ||
overlayClassName={CLASS_MODAL_OVERLAY} | ||
parentSelector={() => parentElement} | ||
portalClassName={`${CLASS_MODAL} be-modal-delete`} | ||
> | ||
<FormattedMessage {...message} values={{ name: item.name }} /> | ||
<div className="be-modal-btns"> | ||
<Button | ||
loading={isLoading} | ||
loadingAriaLabel={formatMessage(messages.loading)} | ||
onClick={onDelete} | ||
size="large" | ||
variant="primary" | ||
> | ||
{formatMessage(messages.delete)} | ||
</Button> | ||
<Button autoFocus disabled={isLoading} onClick={onCancel} size="large" variant="secondary"> | ||
{formatMessage(messages.cancel)} | ||
</Button> | ||
</div> | ||
</Modal> | ||
); | ||
}; | ||
|
||
export default DeleteConfirmationDialog; |
70 changes: 70 additions & 0 deletions
70
src/elements/content-explorer/__tests__/DeleteConfirmationDialog.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import * as React from 'react'; | ||
import userEvent from '@testing-library/user-event'; | ||
import { render, screen } from '../../../test-utils/testing-library'; | ||
import DeleteConfirmationDialog, { DeleteConfirmationDialogProps } from '../DeleteConfirmationDialog'; | ||
|
||
import { TYPE_FOLDER } from '../../../constants'; | ||
|
||
const mockItem = { type: 'pdf', name: 'Test File' }; | ||
const mockFolderItem = { type: TYPE_FOLDER, name: 'Test Folder' }; | ||
|
||
jest.mock('react-modal', () => { | ||
return jest.fn(({ children }) => <div aria-label="Delete">{children}</div>); | ||
}); | ||
|
||
describe('elements/content-explorer/DeleteConfirmationDialog', () => { | ||
const defaultProps = { | ||
appElement: document.body, | ||
errorCode: '', | ||
isLoading: false, | ||
isOpen: false, | ||
item: mockItem, | ||
onCancel: jest.fn(), | ||
onDelete: jest.fn(), | ||
parentElement: document.createElement('div'), | ||
}; | ||
|
||
const renderComponent = (props: Partial<DeleteConfirmationDialogProps>) => | ||
render(<DeleteConfirmationDialog {...defaultProps} {...props} />); | ||
|
||
test('should render the dialog with file message', async () => { | ||
renderComponent({ isOpen: true }); | ||
|
||
expect(await screen.findByText('Are you sure you want to delete Test File?')).toBeInTheDocument(); | ||
}); | ||
|
||
test('should render the dialog with folder message', async () => { | ||
renderComponent({ isOpen: true, item: mockFolderItem }); | ||
|
||
expect( | ||
await screen.findByText('Are you sure you want to delete Test Folder and all its contents?'), | ||
).toBeInTheDocument(); | ||
}); | ||
|
||
test('should call onCancel when cancel button is clicked', async () => { | ||
const mockOnCancel = jest.fn(); | ||
renderComponent({ isOpen: true, onCancel: mockOnCancel }); | ||
|
||
const cancelButton = screen.getByRole('button', { name: 'Cancel' }); | ||
await userEvent.click(cancelButton); | ||
expect(mockOnCancel).toBeCalledTimes(1); | ||
}); | ||
|
||
test('should call onDelete when delete button is clicked', async () => { | ||
const mockOnDelete = jest.fn(); | ||
renderComponent({ isOpen: true, onDelete: mockOnDelete }); | ||
|
||
const deleteButton = screen.getByRole('button', { name: 'Delete' }); | ||
await userEvent.click(deleteButton); | ||
expect(mockOnDelete).toBeCalledTimes(1); | ||
}); | ||
|
||
test('should disable buttons when isLoading is true', () => { | ||
renderComponent({ isOpen: true, isLoading: true }); | ||
|
||
const cancelButton = screen.getByRole('button', { name: 'Cancel' }); | ||
expect(cancelButton).toBeDisabled(); | ||
const loadingIndicator = screen.getByRole('status', { name: 'Loading' }); | ||
expect(loadingIndicator).toBeInTheDocument(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters