-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #70 from RevelioStartup/feat/delete-timeline
Feat/delete timeline
- Loading branch information
Showing
6 changed files
with
214 additions
and
61 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import React from 'react' | ||
import { render, fireEvent, waitFor } from '@testing-library/react' | ||
import '@testing-library/jest-dom' | ||
import { Provider } from 'react-redux' | ||
import { store } from '@/redux/store' | ||
import TimelineDetailsModal from '@/components/elements/Timeline/TimelineDetailsModal' | ||
|
||
jest.mock('@/redux/api/timelineApi', () => ({ | ||
useDeleteTimelineMutation: jest.fn(() => [ | ||
jest.fn().mockResolvedValue({}), | ||
{ isLoading: false, isSuccess: false }, | ||
]), | ||
})) | ||
|
||
describe('TimelineDetailsModal', () => { | ||
const mockOnClose = jest.fn() | ||
|
||
it('deletes the timeline and closes the modal on successful delete', async () => { | ||
const { getByText } = render( | ||
<Provider store={store}> | ||
<TimelineDetailsModal | ||
timelineId="1" | ||
onClose={mockOnClose} | ||
showModal={true} | ||
clickedEvent={{ | ||
title: 'Setup', | ||
start: new Date('2022-05-01T09:00:00Z'), | ||
end: new Date('2022-05-01T10:00:00Z'), | ||
}} | ||
/> | ||
</Provider> | ||
) | ||
|
||
global.confirm = jest.fn().mockReturnValue(true) | ||
|
||
fireEvent.click(getByText('Delete Timeline')) | ||
|
||
await waitFor(() => { | ||
expect(mockOnClose).toHaveBeenCalled() | ||
}) | ||
}) | ||
|
||
it('does not delete the timeline if user cancels', async () => { | ||
const { getByText } = render( | ||
<Provider store={store}> | ||
<TimelineDetailsModal | ||
timelineId="1" | ||
onClose={mockOnClose} | ||
showModal={true} | ||
clickedEvent={{ | ||
title: 'Setup', | ||
start: new Date('2022-05-01T09:00:00Z'), | ||
end: new Date('2022-05-01T10:00:00Z'), | ||
}} | ||
/> | ||
</Provider> | ||
) | ||
|
||
global.confirm = jest.fn().mockReturnValue(false) | ||
|
||
fireEvent.click(getByText('Delete Timeline')) | ||
await waitFor(() => { | ||
expect(mockOnClose).not.toHaveBeenCalled() | ||
}) | ||
}) | ||
}) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import React from 'react'; | ||
import { useDeleteTimelineMutation } from '@/redux/api/timelineApi'; | ||
import Modal from '@mui/material/Modal'; | ||
import Box from '@mui/material/Box'; | ||
import Typography from '@mui/material/Typography'; | ||
import Button from '@mui/material/Button'; | ||
|
||
interface TimelineDetailsModalProps { | ||
timelineId: string; | ||
onClose: () => void; | ||
showModal: boolean; | ||
clickedEvent: { | ||
title: string; | ||
start: Date; | ||
end: Date; | ||
}; | ||
} | ||
|
||
const style = { | ||
position: 'absolute' as 'absolute', | ||
top: '50%', | ||
left: '50%', | ||
transform: 'translate(-50%, -50%)', | ||
width: 400, | ||
bgcolor: 'background.paper', | ||
border: '2px solid #000', | ||
boxShadow: 24, | ||
p: 4, | ||
}; | ||
|
||
const TimelineDetailsModal: React.FC<TimelineDetailsModalProps> = ({ timelineId, onClose, showModal, clickedEvent }) => { | ||
const [deleteTimeline, { isLoading, isSuccess}] = useDeleteTimelineMutation(); | ||
|
||
const handleDelete = async () => { | ||
if (window.confirm('Are you sure you want to delete this timeline?')) { | ||
try { | ||
await deleteTimeline({ id: timelineId }); | ||
onClose(); | ||
} catch (error) { | ||
console.error('Failed to delete the timeline:', error); | ||
alert('There was a problem deleting the timeline.'); | ||
} | ||
} | ||
}; | ||
|
||
React.useEffect(() => { | ||
if (isSuccess) { | ||
onClose(); | ||
} | ||
}, [isSuccess, onClose]); | ||
|
||
|
||
return ( | ||
<Modal | ||
open={showModal} | ||
onClose={onClose} | ||
aria-labelledby="modal-modal-title" | ||
aria-describedby="modal-modal-description" | ||
> | ||
<Box sx={style}> | ||
<Typography id="modal-modal-title" variant="h6" component="h2"> | ||
Task Details | ||
</Typography> | ||
<Typography id="modal-modal-description" sx={{ mt: 2 }}> | ||
Task title: {clickedEvent?.title} | ||
</Typography> | ||
<Typography id="modal-modal-description" sx={{ mt: 2 }}> | ||
Start date: {clickedEvent?.start.toLocaleTimeString()} | ||
</Typography> | ||
<Typography id="modal-modal-description" sx={{ mt: 2 }}> | ||
End date: {clickedEvent?.end.toLocaleTimeString()} | ||
</Typography> | ||
<div style={{ marginTop: 20 }}> | ||
<Button variant="outlined" color="error" onClick={handleDelete} disabled={isLoading}> | ||
Delete Timeline | ||
</Button> | ||
<Button variant="outlined" style={{ marginLeft: 10 }} onClick={onClose}> | ||
Close | ||
</Button> | ||
</div> | ||
</Box> | ||
</Modal> | ||
); | ||
}; | ||
|
||
export default TimelineDetailsModal; |
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