-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add confirmation box on exit button click
- Loading branch information
Showing
7 changed files
with
161 additions
and
42 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import React from 'react'; | ||
import { render, fireEvent, screen } from '@testing-library/react'; | ||
import '@testing-library/jest-dom'; | ||
import AlertDialog from './index'; | ||
|
||
describe('AlertDialog Component', () => { | ||
const handleClose = jest.fn(); | ||
const handleExit = jest.fn(); | ||
const title = "Are you sure you want to exit?"; | ||
const description = "Do you really want to exit? This action will clear the storage and all data will be lost."; | ||
const exitConfirm = "Agree"; | ||
const exitCancel = "Cancel"; | ||
|
||
beforeEach(() => { | ||
render( | ||
<AlertDialog | ||
open={true} | ||
handleClose={handleClose} | ||
handleExit={handleExit} | ||
title={title} | ||
description={description} | ||
exitConfirm={exitConfirm} | ||
exitCancel={exitCancel} | ||
/> | ||
); | ||
}); | ||
|
||
test('renders the alert dialog with correct title and description', () => { | ||
expect(screen.getByTestId('alert-dialog')).toBeInTheDocument(); | ||
expect(screen.getByTestId('alert-dialog-title')).toHaveTextContent(title); | ||
expect(screen.getByTestId('alert-dialog-description')).toHaveTextContent(description); | ||
}); | ||
|
||
test('calls handleClose when cancel button is clicked', () => { | ||
fireEvent.click(screen.getByTestId('disagree-button')); | ||
expect(handleClose).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
test('calls handleExit when agree button is clicked', () => { | ||
fireEvent.click(screen.getByTestId('agree-button')); | ||
expect(handleExit).toHaveBeenCalledTimes(1); | ||
}); | ||
}); |
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,38 @@ | ||
import * as React from 'react'; | ||
import Button from '@mui/material/Button'; | ||
import Dialog from '@mui/material/Dialog'; | ||
import DialogActions from '@mui/material/DialogActions'; | ||
import DialogContent from '@mui/material/DialogContent'; | ||
import DialogContentText from '@mui/material/DialogContentText'; | ||
import DialogTitle from '@mui/material/DialogTitle'; | ||
|
||
export const AlertDialog = (props) => { | ||
const { open, handleClose, handleExit, title, description, exitConfirm, exitCancel } = props; | ||
|
||
return ( | ||
<Dialog | ||
open={open} | ||
onClose={handleClose} | ||
aria-labelledby="alert-dialog-title" | ||
aria-describedby="alert-dialog-description" | ||
data-testid="alert-dialog" | ||
> | ||
<DialogTitle id="alert-dialog-title" data-testid="alert-dialog-title"> | ||
{title} | ||
</DialogTitle> | ||
<DialogContent data-testid="alert-dialog-content"> | ||
<DialogContentText id="alert-dialog-description" data-testid="alert-dialog-description"> | ||
{description} | ||
</DialogContentText> | ||
</DialogContent> | ||
<DialogActions data-testid="alert-dialog-actions"> | ||
<Button onClick={handleClose} data-testid="disagree-button">{exitCancel}</Button> | ||
<Button onClick={handleExit} autoFocus data-testid="agree-button"> | ||
{exitConfirm} | ||
</Button> | ||
</DialogActions> | ||
</Dialog> | ||
); | ||
} | ||
|
||
export default AlertDialog; |
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 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