diff --git a/src/components/OrgPostCard/DeletePostModal.spec.tsx b/src/components/OrgPostCard/DeletePostModal.spec.tsx new file mode 100644 index 0000000000..2a4ed220b2 --- /dev/null +++ b/src/components/OrgPostCard/DeletePostModal.spec.tsx @@ -0,0 +1,77 @@ +import React from 'react'; +import { render, screen, fireEvent } from '@testing-library/react'; +import { I18nextProvider } from 'react-i18next'; +import { vi } from 'vitest'; + +import DeletePostModal from './DeletePostModal'; +import i18nForTest from 'utils/i18nForTest'; + +describe('DeletePostModal', () => { + it('renders the delete modal with correct text', () => { + const mockOnHide = vi.fn(); + const mockOnDelete = vi.fn(); + + render( + + + , + ); + + expect(screen.getByTestId('delete-post-modal')).toBeInTheDocument(); + expect(screen.getByText(/delete post/i)).toBeInTheDocument(); + expect( + screen.getByText(/do you want to remove this post\?/i), + ).toBeInTheDocument(); + }); + + it('hides the modal when "No" is clicked', () => { + const mockOnHide = vi.fn(); + const mockOnDelete = vi.fn(); + + render( + + + , + ); + + fireEvent.click(screen.getByTestId('deleteModalNoBtn')); + expect(mockOnHide).toHaveBeenCalledTimes(1); + expect(mockOnDelete).not.toHaveBeenCalled(); + }); + + it('calls onDelete and onHide when "Yes" is clicked', () => { + const mockOnHide = vi.fn(); + const mockOnDelete = vi.fn(); + + render( + + + , + ); + + fireEvent.click(screen.getByTestId('deletePostBtn')); + expect(mockOnDelete).toHaveBeenCalledTimes(1); + expect(mockOnHide).toHaveBeenCalledTimes(1); + }); + + it('does not render the modal when show is false', () => { + const { queryByTestId } = render( + + + , + ); + expect(queryByTestId('delete-post-modal')).not.toBeInTheDocument(); + }); +}); diff --git a/src/components/OrgPostCard/DeletePostModal.tsx b/src/components/OrgPostCard/DeletePostModal.tsx new file mode 100644 index 0000000000..7e5fdc0d4b --- /dev/null +++ b/src/components/OrgPostCard/DeletePostModal.tsx @@ -0,0 +1,56 @@ +import React, { type FC } from 'react'; +import { Modal, Button } from 'react-bootstrap'; +import { useTranslation } from 'react-i18next'; + +interface InterfaceDeletePostModalProps { + show: boolean; + onHide: () => void; + onDelete: () => void; +} + +const DeletePostModal: FC = ({ + show, + onHide, + onDelete, +}) => { + const { t } = useTranslation('translation', { + keyPrefix: 'orgPostCard', + }); + const { t: tCommon } = useTranslation('common'); + + const handleConfirmDelete = (): void => { + onDelete(); + onHide(); + }; + + return ( + + +
{t('deletePost')}
+ +
+ {t('deletePostMsg')} + + + + +
+ ); +}; + +export default DeletePostModal; diff --git a/src/components/OrgPostCard/OrgPostCard.tsx b/src/components/OrgPostCard/OrgPostCard.tsx index f392c6ad1c..5bc9ca1407 100644 --- a/src/components/OrgPostCard/OrgPostCard.tsx +++ b/src/components/OrgPostCard/OrgPostCard.tsx @@ -15,6 +15,7 @@ import convertToBase64 from 'utils/convertToBase64'; import { errorHandler } from 'utils/errorHandler'; import type { InterfacePostForm } from 'utils/interfaces'; import styles from '../../style/app.module.css'; +import DeletePostModal from './DeletePostModal'; interface InterfaceOrgPostCardProps { postID: string; id: string; @@ -57,11 +58,7 @@ export default function OrgPostCard( const [toggle] = useMutation(TOGGLE_PINNED_POST); const togglePostPin = async (id: string, pinned: boolean): Promise => { try { - const { data } = await toggle({ - variables: { - id, - }, - }); + const { data } = await toggle({ variables: { id } }); if (data) { setModalVisible(false); setMenuVisible(false); @@ -72,7 +69,6 @@ export default function OrgPostCard( } } catch (error: unknown) { if (error instanceof Error) { - /* istanbul ignore next */ errorHandler(t, error); } } @@ -106,10 +102,7 @@ export default function OrgPostCard( setMenuVisible(true); }; const clearImageInput = (): void => { - setPostFormState({ - ...postformState, - postphoto: '', - }); + setPostFormState({ ...postformState, postphoto: '' }); setPostPhotoUpdated(true); const fileInput = document.getElementById( 'postImageUrl', @@ -172,9 +165,7 @@ export default function OrgPostCard( const deletePost = async (): Promise => { try { const { data } = await deletePostMutation({ - variables: { - id, - }, + variables: { id }, }); if (data) { toast.success(t('postDeleted') as string); @@ -441,28 +432,11 @@ export default function OrgPostCard( )} - - -
{t('deletePost')}
- -
- {t('deletePostMsg')} - - - - -
+ deletePost()} + />