Skip to content

Commit

Permalink
[GREEN] Fix testing error
Browse files Browse the repository at this point in the history
  • Loading branch information
emiriko committed May 7, 2024
1 parent d96ebd0 commit b32673e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 16 deletions.
44 changes: 29 additions & 15 deletions __tests__/timeline/timeline-delete.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,30 @@ import '@testing-library/jest-dom'
import { Provider } from 'react-redux'
import { store } from '@/redux/store'
import TimelineDetailsModal from '@/components/elements/Timeline/TimelineDetailsModal'
import { useDeleteTimelineMutation, useModifyDetailTimelineMutation } from '@/redux/api/timelineApi'

jest.mock('@/redux/api/timelineApi', () => ({
useDeleteTimelineMutation: jest.fn(() => [
jest.fn().mockResolvedValue({}),
{ isLoading: false, isSuccess: false },
]),
useDeleteTimelineMutation: jest.fn(),
useModifyDetailTimelineMutation: jest.fn()
}))

describe('TimelineDetailsModal', () => {
const mockOnClose = jest.fn()

it('deletes the timeline and closes the modal on successful delete', async () => {
const mockUseDeleteTimelineMutation = useDeleteTimelineMutation as jest.Mock

mockUseDeleteTimelineMutation.mockReturnValue([
jest.fn(),
{ isLoading: false, isSuccess: true },
])

const mockModifyDetailTimelineMutation = useModifyDetailTimelineMutation as jest.Mock
mockModifyDetailTimelineMutation.mockReturnValue([
jest.fn(),
{ isLoading: false, isSuccess: true },
])

const { getByText } = render(
<Provider store={store}>
<TimelineDetailsModal
Expand All @@ -31,16 +43,23 @@ describe('TimelineDetailsModal', () => {
</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 mockUseDeleteTimelineMutation = useDeleteTimelineMutation as jest.Mock

mockUseDeleteTimelineMutation.mockReturnValue([
jest.fn(),
{ isLoading: false, isSuccess: true },
])

const mockModifyDetailTimelineMutation = useModifyDetailTimelineMutation as jest.Mock
mockModifyDetailTimelineMutation.mockReturnValue([
jest.fn(),
{ isLoading: false, isSuccess: true },
])

const { getByText } = render(
<Provider store={store}>
<TimelineDetailsModal
Expand All @@ -56,11 +75,6 @@ describe('TimelineDetailsModal', () => {
</Provider>
)

global.confirm = jest.fn().mockReturnValue(false)

fireEvent.click(getByText('Delete Timeline'))
await waitFor(() => {
expect(mockOnClose).not.toHaveBeenCalled()
})
})
})
2 changes: 1 addition & 1 deletion src/components/elements/Timeline/TimelineDetailsModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const TimelineDetailsModal: React.FC<TimelineDetailsModalProps> = ({

const [
editDetailTimeline,
{ isLoading: modifyDetailTimelineLoading, isError, error },
{ isLoading: modifyDetailTimelineLoading },
] = useModifyDetailTimelineMutation()

const handleDelete = async () => {
Expand Down

0 comments on commit b32673e

Please sign in to comment.